Skip to content

Commit f6b4dd1

Browse files
committed
Create new_mapping local structure and avoid checking def_ids on captures
1 parent 6c6a81e commit f6b4dd1

File tree

1 file changed

+48
-34
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+48
-34
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13271327
let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
13281328

13291329
let mut collected_lifetimes = FxHashMap::default();
1330+
let mut new_remapping = FxHashMap::default();
1331+
13301332
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
13311333
let hir_bounds = if origin == hir::OpaqueTyOrigin::TyAlias {
13321334
lctx.lower_param_bounds(bounds, itctx)
@@ -1344,7 +1346,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13441346
lifetime_collector::lifetimes_in_bounds(&lctx.resolver, bounds);
13451347
debug!(?lifetimes_in_bounds);
13461348

1347-
lctx.create_and_capture_lifetime_defs(opaque_ty_def_id, &lifetimes_in_bounds);
1349+
lctx.create_and_capture_lifetime_defs(
1350+
opaque_ty_def_id,
1351+
&lifetimes_in_bounds,
1352+
&mut new_remapping,
1353+
);
13481354

13491355
let ret = lctx.lower_param_bounds(bounds, itctx);
13501356

@@ -1437,6 +1443,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14371443
&mut self,
14381444
parent_def_id: LocalDefId,
14391445
lifetimes_in_bounds: &[Lifetime],
1446+
remapping: &mut FxHashMap<LocalDefId, LocalDefId>,
14401447
) {
14411448
for lifetime in lifetimes_in_bounds {
14421449
let ident = lifetime.ident;
@@ -1447,42 +1454,42 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14471454

14481455
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
14491456
match res {
1450-
LifetimeRes::Param { param, binder: _ } => {
1451-
match captured_lifetimes.captures.entry(param) {
1452-
Entry::Occupied(_) => {}
1453-
Entry::Vacant(v) => {
1454-
let node_id = self.next_node_id();
1455-
let name = ParamName::Plain(ident);
1456-
1457-
self.create_def(
1458-
parent_def_id,
1459-
node_id,
1460-
DefPathData::LifetimeNs(name.ident().name),
1461-
);
1462-
1463-
v.insert((span, node_id, name, res));
1464-
}
1457+
LifetimeRes::Param { param: old_def_id, binder: _ } => {
1458+
if remapping.get(&old_def_id).is_none() {
1459+
let node_id = self.next_node_id();
1460+
let name = ParamName::Plain(ident);
1461+
1462+
let new_def_id = self.create_def(
1463+
parent_def_id,
1464+
node_id,
1465+
DefPathData::LifetimeNs(name.ident().name),
1466+
);
1467+
1468+
remapping.insert(old_def_id, new_def_id);
1469+
captured_lifetimes
1470+
.captures
1471+
.insert(old_def_id, (span, node_id, name, res));
14651472
}
14661473
}
14671474

14681475
LifetimeRes::Fresh { param, binder: _ } => {
14691476
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1470-
let param = self.local_def_id(param);
1471-
match captured_lifetimes.captures.entry(param) {
1472-
Entry::Occupied(_) => {}
1473-
Entry::Vacant(v) => {
1474-
let node_id = self.next_node_id();
1475-
1476-
let name = ParamName::Fresh;
1477-
1478-
self.create_def(
1479-
parent_def_id,
1480-
node_id,
1481-
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1482-
);
1483-
1484-
v.insert((span, node_id, name, res));
1485-
}
1477+
let old_def_id = self.local_def_id(param);
1478+
if remapping.get(&old_def_id).is_none() {
1479+
let node_id = self.next_node_id();
1480+
1481+
let name = ParamName::Fresh;
1482+
1483+
let new_def_id = self.create_def(
1484+
parent_def_id,
1485+
node_id,
1486+
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1487+
);
1488+
1489+
remapping.insert(old_def_id, new_def_id);
1490+
captured_lifetimes
1491+
.captures
1492+
.insert(old_def_id, (span, node_id, name, res));
14861493
}
14871494
}
14881495

@@ -1691,6 +1698,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16911698
// lifetime parameters, including those defined in-band.
16921699

16931700
let mut captures = FxHashMap::default();
1701+
let mut new_remapping = FxHashMap::default();
16941702

16951703
let extra_lifetime_params = self.resolver.take_extra_lifetime_params(opaque_ty_node_id);
16961704
debug!(?extra_lifetime_params);
@@ -1700,7 +1708,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17001708
let inner_node_id = self.next_node_id();
17011709

17021710
// Add a definition for the in scope lifetime def.
1703-
self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));
1711+
let inner_def_id =
1712+
self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));
1713+
new_remapping.insert(outer_def_id, inner_def_id);
17041714

17051715
let (p_name, inner_res) = match outer_res {
17061716
// Input lifetime like `'a`:
@@ -1732,7 +1742,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17321742
lifetime_collector::lifetimes_in_ret_ty(&this.resolver, output);
17331743
debug!(?lifetimes_in_bounds);
17341744

1735-
this.create_and_capture_lifetime_defs(opaque_ty_def_id, &lifetimes_in_bounds);
1745+
this.create_and_capture_lifetime_defs(
1746+
opaque_ty_def_id,
1747+
&lifetimes_in_bounds,
1748+
&mut new_remapping,
1749+
);
17361750

17371751
// We have to be careful to get elision right here. The
17381752
// idea is that we create a lifetime parameter for each

0 commit comments

Comments
 (0)