Skip to content

Commit bc6a2c1

Browse files
committed
Leave the responsibility to create Fresh lifetimes to lowering.
1 parent dc614b9 commit bc6a2c1

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

compiler/rustc_ast_lowering/src/item.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1386,16 +1386,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
13861386

13871387
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> =
13881388
self.lower_generic_params_mut(&generics.params).collect();
1389+
1390+
// Introduce extra lifetimes if late resolution tells us to.
1391+
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
1392+
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1393+
self.lifetime_res_to_generic_param(ident, node_id, res)
1394+
}));
1395+
13891396
let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
13901397
let where_clause_span = self.lower_span(generics.where_clause.span);
13911398
let span = self.lower_span(generics.span);
13921399
let res = f(self);
13931400

1394-
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
13951401
let impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
1396-
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1397-
self.lifetime_res_to_generic_param(ident, node_id, res)
1398-
}));
13991402
params.extend(impl_trait_defs.into_iter());
14001403

14011404
let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);

compiler/rustc_ast_lowering/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
731731
LifetimeRes::Param { .. } => {
732732
(hir::ParamName::Plain(ident), hir::LifetimeParamKind::Explicit)
733733
}
734-
LifetimeRes::Fresh { .. } => (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided),
734+
LifetimeRes::Fresh { param, .. } => {
735+
// Late resolution delegates to us the creation of the `LocalDefId`.
736+
let _def_id = self.create_def(
737+
self.current_hir_id_owner,
738+
param,
739+
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
740+
);
741+
debug!(?_def_id);
742+
743+
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
744+
}
735745
LifetimeRes::Static | LifetimeRes::Error => return None,
736746
res => panic!(
737747
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
@@ -1814,8 +1824,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18141824
}
18151825
hir::LifetimeName::Param(param, p_name)
18161826
}
1817-
LifetimeRes::Fresh { mut param, binder } => {
1827+
LifetimeRes::Fresh { param, binder } => {
18181828
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
1829+
let mut param = self.local_def_id(param);
18191830
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
18201831
if !captured_lifetimes.binders_to_ignore.contains(&binder) {
18211832
match captured_lifetimes.captures.entry(param) {

compiler/rustc_hir/src/def.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,9 @@ pub enum LifetimeRes {
732732
/// Created a generic parameter for an anonymous lifetime.
733733
Fresh {
734734
/// Id of the generic parameter that introduced it.
735-
param: LocalDefId,
735+
///
736+
/// Creating the associated `LocalDefId` is the responsibility of lowering.
737+
param: NodeId,
736738
/// Id of the introducing place. See `Param`.
737739
binder: NodeId,
738740
},

compiler/rustc_resolve/src/late.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_errors::DiagnosticId;
2020
use rustc_hir::def::Namespace::{self, *};
2121
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
2222
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
23-
use rustc_hir::definitions::DefPathData;
2423
use rustc_hir::{PrimTy, TraitCandidate};
2524
use rustc_index::vec::Idx;
2625
use rustc_middle::ty::DefIdTree;
@@ -1418,31 +1417,20 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14181417
}
14191418

14201419
#[tracing::instrument(level = "debug", skip(self))]
1421-
fn create_fresh_lifetime(
1422-
&mut self,
1423-
id: NodeId,
1424-
ident: Ident,
1425-
item_node_id: NodeId,
1426-
) -> LifetimeRes {
1420+
fn create_fresh_lifetime(&mut self, id: NodeId, ident: Ident, binder: NodeId) -> LifetimeRes {
14271421
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
14281422
debug!(?ident.span);
1429-
let item_def_id = self.r.local_def_id(item_node_id);
1430-
let def_node_id = self.r.next_node_id();
1431-
let def_id = self.r.create_def(
1432-
item_def_id,
1433-
def_node_id,
1434-
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1435-
self.parent_scope.expansion.to_expn_id(),
1436-
ident.span,
1437-
);
1438-
debug!(?def_id);
14391423

1440-
let res = LifetimeRes::Fresh { param: def_id, binder: item_node_id };
1441-
self.r.extra_lifetime_params_map.entry(item_node_id).or_insert_with(Vec::new).push((
1442-
ident,
1443-
def_node_id,
1444-
res,
1445-
));
1424+
// Leave the responsibility to create the `LocalDefId` to lowering.
1425+
let param = self.r.next_node_id();
1426+
let res = LifetimeRes::Fresh { param, binder };
1427+
1428+
// Record the created lifetime parameter so lowering can pick it up and add it to HIR.
1429+
self.r
1430+
.extra_lifetime_params_map
1431+
.entry(binder)
1432+
.or_insert_with(Vec::new)
1433+
.push((ident, param, res));
14461434
res
14471435
}
14481436

0 commit comments

Comments
 (0)