Skip to content

Commit 2d826e2

Browse files
committed
Capture things as Lifetime object to simplify things
1 parent f6b4dd1 commit 2d826e2

File tree

1 file changed

+59
-49
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+59
-49
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ struct LifetimeCaptureContext {
140140
captures: FxHashMap<
141141
LocalDefId, // original parameter id
142142
(
143-
Span, // Span
144-
NodeId, // synthetized parameter id
145-
ParamName, // parameter name
143+
Lifetime, // Lifetime parameter
146144
LifetimeRes, // original resolution
147145
),
148146
>,
@@ -1363,20 +1361,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13631361
debug!(?collected_lifetimes);
13641362

13651363
let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
1366-
|(_, &(span, p_id, p_name, _))| {
1367-
let hir_id = lctx.lower_node_id(p_id);
1368-
debug_assert_ne!(lctx.opt_local_def_id(p_id), None);
1364+
|(_, &(lifetime, _))| {
1365+
let hir_id = lctx.lower_node_id(lifetime.id);
1366+
debug_assert_ne!(lctx.opt_local_def_id(lifetime.id), None);
13691367

1370-
let kind = if p_name.ident().name == kw::UnderscoreLifetime {
1371-
hir::LifetimeParamKind::Elided
1368+
let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
1369+
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
13721370
} else {
1373-
hir::LifetimeParamKind::Explicit
1371+
(hir::ParamName::Plain(lifetime.ident), hir::LifetimeParamKind::Explicit)
13741372
};
13751373

13761374
hir::GenericParam {
13771375
hir_id,
1378-
name: p_name,
1379-
span,
1376+
name,
1377+
span: lifetime.ident.span,
13801378
pure_wrt_drop: false,
13811379
kind: hir::GenericParamKind::Lifetime { kind },
13821380
colon_span: None,
@@ -1403,9 +1401,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14031401
});
14041402

14051403
let lifetimes = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(
1406-
|(_, (span, _, p_name, res))| {
1404+
|(_, (lifetime, res))| {
14071405
let id = self.next_node_id();
1408-
let ident = Ident::new(p_name.ident().name, span);
1406+
let span = lifetime.ident.span;
1407+
1408+
let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
1409+
Ident::with_dummy_span(kw::UnderscoreLifetime)
1410+
} else {
1411+
lifetime.ident
1412+
};
1413+
14091414
let l = self.new_named_lifetime_with_res(id, span, ident, res);
14101415
hir::GenericArg::Lifetime(l)
14111416
},
@@ -1446,9 +1451,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14461451
remapping: &mut FxHashMap<LocalDefId, LocalDefId>,
14471452
) {
14481453
for lifetime in lifetimes_in_bounds {
1449-
let ident = lifetime.ident;
1450-
let span = ident.span;
1451-
14521454
let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
14531455
debug!(?res);
14541456

@@ -1457,39 +1459,34 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14571459
LifetimeRes::Param { param: old_def_id, binder: _ } => {
14581460
if remapping.get(&old_def_id).is_none() {
14591461
let node_id = self.next_node_id();
1460-
let name = ParamName::Plain(ident);
14611462

14621463
let new_def_id = self.create_def(
14631464
parent_def_id,
14641465
node_id,
1465-
DefPathData::LifetimeNs(name.ident().name),
1466+
DefPathData::LifetimeNs(lifetime.ident.name),
14661467
);
1467-
14681468
remapping.insert(old_def_id, new_def_id);
1469-
captured_lifetimes
1470-
.captures
1471-
.insert(old_def_id, (span, node_id, name, res));
1469+
1470+
let new_lifetime = Lifetime { id: node_id, ident: lifetime.ident };
1471+
captured_lifetimes.captures.insert(old_def_id, (new_lifetime, res));
14721472
}
14731473
}
14741474

14751475
LifetimeRes::Fresh { param, binder: _ } => {
1476-
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1476+
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
14771477
let old_def_id = self.local_def_id(param);
14781478
if remapping.get(&old_def_id).is_none() {
14791479
let node_id = self.next_node_id();
14801480

1481-
let name = ParamName::Fresh;
1482-
14831481
let new_def_id = self.create_def(
14841482
parent_def_id,
14851483
node_id,
14861484
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
14871485
);
1488-
14891486
remapping.insert(old_def_id, new_def_id);
1490-
captured_lifetimes
1491-
.captures
1492-
.insert(old_def_id, (span, node_id, name, res));
1487+
1488+
let new_lifetime = Lifetime { id: node_id, ident: lifetime.ident };
1489+
captured_lifetimes.captures.insert(old_def_id, (new_lifetime, res));
14931490
}
14941491
}
14951492

@@ -1703,31 +1700,37 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17031700
let extra_lifetime_params = self.resolver.take_extra_lifetime_params(opaque_ty_node_id);
17041701
debug!(?extra_lifetime_params);
17051702
for (ident, outer_node_id, outer_res) in extra_lifetime_params {
1706-
let Ident { name, span } = ident;
17071703
let outer_def_id = self.local_def_id(outer_node_id);
17081704
let inner_node_id = self.next_node_id();
17091705

17101706
// Add a definition for the in scope lifetime def.
1711-
let inner_def_id =
1712-
self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));
1707+
let inner_def_id = self.create_def(
1708+
opaque_ty_def_id,
1709+
inner_node_id,
1710+
DefPathData::LifetimeNs(ident.name),
1711+
);
17131712
new_remapping.insert(outer_def_id, inner_def_id);
17141713

1715-
let (p_name, inner_res) = match outer_res {
1714+
let inner_res = match outer_res {
17161715
// Input lifetime like `'a`:
17171716
LifetimeRes::Param { param, .. } => {
1718-
(hir::ParamName::Plain(ident), LifetimeRes::Param { param, binder: fn_node_id })
1717+
LifetimeRes::Param { param, binder: fn_node_id }
17191718
}
17201719
// Input lifetime like `'1`:
17211720
LifetimeRes::Fresh { param, .. } => {
1722-
(hir::ParamName::Fresh, LifetimeRes::Fresh { param, binder: fn_node_id })
1721+
LifetimeRes::Fresh { param, binder: fn_node_id }
17231722
}
17241723
LifetimeRes::Static | LifetimeRes::Error => continue,
17251724
res => {
1726-
panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span)
1725+
panic!(
1726+
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
1727+
res, ident, ident.span
1728+
)
17271729
}
17281730
};
17291731

1730-
captures.insert(outer_def_id, (span, inner_node_id, p_name, inner_res));
1732+
let new_lifetime = Lifetime { id: inner_node_id, ident };
1733+
captures.insert(outer_def_id, (new_lifetime, inner_res));
17311734
}
17321735

17331736
debug!(?captures);
@@ -1765,20 +1768,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17651768
let future_bound = ret;
17661769

17671770
let generic_params =
1768-
this.arena.alloc_from_iter(captures.iter().map(|(_, &(span, p_id, p_name, _))| {
1769-
let hir_id = this.lower_node_id(p_id);
1770-
debug_assert_ne!(this.opt_local_def_id(p_id), None);
1771+
this.arena.alloc_from_iter(captures.iter().map(|(_, &(lifetime, _))| {
1772+
let hir_id = this.lower_node_id(lifetime.id);
1773+
debug_assert_ne!(this.opt_local_def_id(lifetime.id), None);
17711774

1772-
let kind = if p_name.ident().name == kw::UnderscoreLifetime {
1773-
hir::LifetimeParamKind::Elided
1775+
let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
1776+
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
17741777
} else {
1775-
hir::LifetimeParamKind::Explicit
1778+
(hir::ParamName::Plain(lifetime.ident), hir::LifetimeParamKind::Explicit)
17761779
};
17771780

17781781
hir::GenericParam {
17791782
hir_id,
1780-
name: p_name,
1781-
span,
1783+
name,
1784+
span: lifetime.ident.span,
17821785
pure_wrt_drop: false,
17831786
kind: hir::GenericParamKind::Lifetime { kind },
17841787
colon_span: None,
@@ -1818,9 +1821,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18181821
// For the "output" lifetime parameters, we just want to
18191822
// generate `'_`.
18201823
let generic_args =
1821-
self.arena.alloc_from_iter(captures.into_iter().map(|(_, (span, _, p_name, res))| {
1824+
self.arena.alloc_from_iter(captures.into_iter().map(|(_, (lifetime, res))| {
18221825
let id = self.next_node_id();
1823-
let ident = Ident::new(p_name.ident().name, span);
1826+
let span = lifetime.ident.span;
1827+
1828+
let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
1829+
Ident::with_dummy_span(kw::UnderscoreLifetime)
1830+
} else {
1831+
lifetime.ident
1832+
};
1833+
18241834
let l = self.new_named_lifetime_with_res(id, span, ident, res);
18251835
hir::GenericArg::Lifetime(l)
18261836
}));
@@ -1912,7 +1922,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19121922
let p_name = ParamName::Plain(ident);
19131923
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
19141924
if let Entry::Occupied(o) = captured_lifetimes.captures.entry(param) {
1915-
param = self.local_def_id(o.get().1);
1925+
param = self.local_def_id(o.get().0.id);
19161926
}
19171927

19181928
self.captured_lifetimes = Some(captured_lifetimes);
@@ -1926,7 +1936,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19261936
let mut param = self.local_def_id(param);
19271937
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
19281938
if let Entry::Occupied(o) = captured_lifetimes.captures.entry(param) {
1929-
param = self.local_def_id(o.get().1);
1939+
param = self.local_def_id(o.get().0.id);
19301940
}
19311941

19321942
self.captured_lifetimes = Some(captured_lifetimes);

0 commit comments

Comments
 (0)