Skip to content

Commit 71f8aed

Browse files
committed
Add current_def_id_parent to LoweringContext
This is needed to track anon const parents properly once we implement `ConstArgKind::Path` (which requires moving anon const def-creation outside of `DefCollector`): Why do we need this in addition to [`Self::current_hir_id_owner`]? Currently (as of June 2024), anonymous constants are not HIR owners; however, they do get their own DefIds. Some of these DefIds have to be created during AST lowering, rather than def collection, because we can't tell until after name resolution whether an anonymous constant will end up instead being a [`rustc_hir::ConstArgKind::Path`]. However, to compute which generics are available to an anonymous constant nested inside another, we need to make sure that the parent is recorded as the parent anon const, not the enclosing item. So we need to track parent defs differently from HIR owners, since they will be finer-grained in the case of anon consts.
1 parent 7d356eb commit 71f8aed

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
222222
};
223223

224224
// Wrap the expression in an AnonConst.
225-
let parent_def_id = self.current_hir_id_owner;
225+
let parent_def_id = self.current_def_id_parent;
226226
let node_id = self.next_node_id();
227227
self.create_def(
228-
parent_def_id.def_id,
228+
parent_def_id,
229229
node_id,
230230
kw::Empty,
231231
DefKind::AnonConst,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
377377
let mut generic_args = ThinVec::new();
378378
for (idx, arg) in args.into_iter().enumerate() {
379379
if legacy_args_idx.contains(&idx) {
380-
let parent_def_id = self.current_hir_id_owner;
380+
let parent_def_id = self.current_def_id_parent;
381381
let node_id = self.next_node_id();
382382

383383
// Add a definition for the in-band const def.
384-
self.create_def(
385-
parent_def_id.def_id,
386-
node_id,
387-
kw::Empty,
388-
DefKind::AnonConst,
389-
f.span,
390-
);
384+
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
391385

392386
let anon_const = AnonConst { id: node_id, value: arg };
393387
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ struct LoweringContext<'a, 'hir> {
120120
is_in_dyn_type: bool,
121121

122122
current_hir_id_owner: hir::OwnerId,
123+
/// Why do we need this in addition to [`Self::current_hir_id_owner`]?
124+
///
125+
/// Currently (as of June 2024), anonymous constants are not HIR owners; however,
126+
/// they do get their own DefIds. Some of these DefIds have to be created during
127+
/// AST lowering, rather than def collection, because we can't tell until after
128+
/// name resolution whether an anonymous constant will end up instead being a
129+
/// [`rustc_hir::ConstArgKind::Path`]. However, to compute which generics are
130+
/// available to an anonymous constant nested inside another, we need to make
131+
/// sure that the parent is recorded as the parent anon const, not the enclosing
132+
/// item. So we need to track parent defs differently from HIR owners, since they
133+
/// will be finer-grained in the case of anon consts.
134+
current_def_id_parent: LocalDefId,
123135
item_local_id_counter: hir::ItemLocalId,
124136
trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
125137

@@ -162,6 +174,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
162174
attrs: SortedMap::default(),
163175
children: Vec::default(),
164176
current_hir_id_owner: hir::CRATE_OWNER_ID,
177+
current_def_id_parent: CRATE_DEF_ID,
165178
item_local_id_counter: hir::ItemLocalId::ZERO,
166179
node_id_to_local_id: Default::default(),
167180
trait_map: Default::default(),
@@ -592,7 +605,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
592605
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
593606
debug_assert_eq!(_old, None);
594607

595-
let item = f(self);
608+
let item = self.with_def_id_parent(def_id, f);
596609
debug_assert_eq!(def_id, item.def_id().def_id);
597610
// `f` should have consumed all the elements in these vectors when constructing `item`.
598611
debug_assert!(self.impl_trait_defs.is_empty());
@@ -612,6 +625,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
612625
self.children.push((def_id, hir::MaybeOwner::Owner(info)));
613626
}
614627

628+
fn with_def_id_parent<T>(&mut self, parent: LocalDefId, f: impl FnOnce(&mut Self) -> T) -> T {
629+
let current_def_id_parent = std::mem::replace(&mut self.current_def_id_parent, parent);
630+
let result = f(self);
631+
self.current_def_id_parent = current_def_id_parent;
632+
result
633+
}
634+
615635
/// Installs the remapping `remap` in scope while `f` is being executed.
616636
/// This causes references to the `LocalDefId` keys to be changed to
617637
/// refer to the values instead.
@@ -806,7 +826,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
806826
LifetimeRes::Fresh { param, kind, .. } => {
807827
// Late resolution delegates to us the creation of the `LocalDefId`.
808828
let _def_id = self.create_def(
809-
self.current_hir_id_owner.def_id,
829+
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
810830
param,
811831
kw::UnderscoreLifetime,
812832
DefKind::LifetimeParam,
@@ -1152,13 +1172,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11521172

11531173
// Construct an AnonConst where the expr is the "ty"'s path.
11541174

1155-
let parent_def_id = self.current_hir_id_owner;
1175+
let parent_def_id = self.current_def_id_parent;
11561176
let node_id = self.next_node_id();
11571177
let span = self.lower_span(ty.span);
11581178

11591179
// Add a definition for the in-band const def.
11601180
let def_id = self.create_def(
1161-
parent_def_id.def_id,
1181+
parent_def_id,
11621182
node_id,
11631183
kw::Empty,
11641184
DefKind::AnonConst,
@@ -1429,7 +1449,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14291449
);
14301450

14311451
self.create_def(
1432-
self.current_hir_id_owner.def_id,
1452+
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
14331453
*def_node_id,
14341454
ident.name,
14351455
DefKind::TyParam,
@@ -1637,7 +1657,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16371657
lower_item_bounds: impl FnOnce(&mut Self) -> &'hir [hir::GenericBound<'hir>],
16381658
) -> hir::TyKind<'hir> {
16391659
let opaque_ty_def_id = self.create_def(
1640-
self.current_hir_id_owner.def_id,
1660+
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
16411661
opaque_ty_node_id,
16421662
kw::Empty,
16431663
DefKind::OpaqueTy,

0 commit comments

Comments
 (0)