Skip to content

Commit 59a5fa3

Browse files
committed
ast_lowering: rm separate def_id_parent
no longer necessary as we now always create a ` DefId` for anon-consts
1 parent 93e188c commit 59a5fa3

File tree

3 files changed

+62
-96
lines changed

3 files changed

+62
-96
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
220220
};
221221

222222
// Wrap the expression in an AnonConst.
223-
let parent_def_id = self.current_def_id_parent;
223+
let parent_def_id = self.current_hir_id_owner.def_id;
224224
let node_id = self.next_node_id();
225225
self.create_def(
226226
parent_def_id,

compiler/rustc_ast_lowering/src/expr.rs

+47-55
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
109109
hir::ConstBlock {
110110
def_id,
111111
hir_id: this.lower_node_id(c.id),
112-
body: this.with_def_id_parent(def_id, |this| {
113-
this.lower_const_body(c.value.span, Some(&c.value))
114-
}),
112+
body: this.lower_const_body(c.value.span, Some(&c.value)),
115113
}
116114
});
117115
hir::ExprKind::ConstBlock(c)
@@ -452,7 +450,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
452450
let mut generic_args = ThinVec::new();
453451
for (idx, arg) in args.iter().cloned().enumerate() {
454452
if legacy_args_idx.contains(&idx) {
455-
let parent_def_id = self.current_def_id_parent;
453+
let parent_def_id = self.current_hir_id_owner.def_id;
456454
let node_id = self.next_node_id();
457455
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
458456
let mut visitor = WillCreateDefIdsVisitor {};
@@ -753,19 +751,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
753751
lifetime_elision_allowed: false,
754752
});
755753

756-
let body = self.with_def_id_parent(closure_def_id, move |this| {
757-
this.lower_body(move |this| {
758-
this.coroutine_kind = Some(coroutine_kind);
754+
let body = self.lower_body(move |this| {
755+
this.coroutine_kind = Some(coroutine_kind);
759756

760-
let old_ctx = this.task_context;
761-
if task_context.is_some() {
762-
this.task_context = task_context;
763-
}
764-
let res = body(this);
765-
this.task_context = old_ctx;
757+
let old_ctx = this.task_context;
758+
if task_context.is_some() {
759+
this.task_context = task_context;
760+
}
761+
let res = body(this);
762+
this.task_context = old_ctx;
766763

767-
(params, res)
768-
})
764+
(params, res)
769765
});
770766

771767
// `static |<_task_context?>| -> <return_ty> { <body> }`:
@@ -1050,26 +1046,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
10501046
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
10511047

10521048
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
1053-
this.with_def_id_parent(closure_def_id, move |this| {
1054-
let mut coroutine_kind = if this
1055-
.attrs
1056-
.get(&closure_hir_id.local_id)
1057-
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
1058-
{
1059-
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
1060-
} else {
1061-
None
1062-
};
1063-
let body_id = this.lower_fn_body(decl, |this| {
1064-
this.coroutine_kind = coroutine_kind;
1065-
let e = this.lower_expr_mut(body);
1066-
coroutine_kind = this.coroutine_kind;
1067-
e
1068-
});
1069-
let coroutine_option =
1070-
this.closure_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
1071-
(body_id, coroutine_option)
1072-
})
1049+
let mut coroutine_kind = if this
1050+
.attrs
1051+
.get(&closure_hir_id.local_id)
1052+
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
1053+
{
1054+
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
1055+
} else {
1056+
None
1057+
};
1058+
let body_id = this.lower_fn_body(decl, |this| {
1059+
this.coroutine_kind = coroutine_kind;
1060+
let e = this.lower_expr_mut(body);
1061+
coroutine_kind = this.coroutine_kind;
1062+
e
1063+
});
1064+
let coroutine_option =
1065+
this.closure_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
1066+
(body_id, coroutine_option)
10731067
});
10741068

10751069
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
@@ -1159,28 +1153,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
11591153
);
11601154

11611155
let body = self.with_new_scopes(fn_decl_span, |this| {
1162-
this.with_def_id_parent(closure_def_id, |this| {
1163-
let inner_decl =
1164-
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1165-
1166-
// Transform `async |x: u8| -> X { ... }` into
1167-
// `|x: u8| || -> X { ... }`.
1168-
let body_id = this.lower_body(|this| {
1169-
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1170-
&inner_decl,
1171-
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
1172-
fn_decl_span,
1173-
body.span,
1174-
coroutine_kind,
1175-
hir::CoroutineSource::Closure,
1176-
);
1156+
let inner_decl =
1157+
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1158+
1159+
// Transform `async |x: u8| -> X { ... }` into
1160+
// `|x: u8| || -> X { ... }`.
1161+
let body_id = this.lower_body(|this| {
1162+
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1163+
&inner_decl,
1164+
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
1165+
fn_decl_span,
1166+
body.span,
1167+
coroutine_kind,
1168+
hir::CoroutineSource::Closure,
1169+
);
11771170

1178-
this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
1171+
this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
11791172

1180-
(parameters, expr)
1181-
});
1182-
body_id
1183-
})
1173+
(parameters, expr)
1174+
});
1175+
body_id
11841176
});
11851177

11861178
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);

compiler/rustc_ast_lowering/src/lib.rs

+14-40
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,6 @@ struct LoweringContext<'a, 'hir> {
117117
is_in_dyn_type: bool,
118118

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

@@ -161,7 +149,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
161149
attrs: SortedMap::default(),
162150
children: Vec::default(),
163151
current_hir_id_owner: hir::CRATE_OWNER_ID,
164-
current_def_id_parent: CRATE_DEF_ID,
165152
item_local_id_counter: hir::ItemLocalId::ZERO,
166153
ident_and_label_to_local_id: Default::default(),
167154
#[cfg(debug_assertions)]
@@ -565,7 +552,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
565552
debug_assert_eq!(_old, None);
566553
}
567554

568-
let item = self.with_def_id_parent(def_id, f);
555+
let item = f(self);
569556
debug_assert_eq!(def_id, item.def_id().def_id);
570557
// `f` should have consumed all the elements in these vectors when constructing `item`.
571558
debug_assert!(self.impl_trait_defs.is_empty());
@@ -590,13 +577,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
590577
self.children.push((def_id, hir::MaybeOwner::Owner(info)));
591578
}
592579

593-
fn with_def_id_parent<T>(&mut self, parent: LocalDefId, f: impl FnOnce(&mut Self) -> T) -> T {
594-
let current_def_id_parent = std::mem::replace(&mut self.current_def_id_parent, parent);
595-
let result = f(self);
596-
self.current_def_id_parent = current_def_id_parent;
597-
result
598-
}
599-
600580
fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
601581
let attrs = std::mem::take(&mut self.attrs);
602582
let mut bodies = std::mem::take(&mut self.bodies);
@@ -773,7 +753,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
773753
LifetimeRes::Fresh { param, kind, .. } => {
774754
// Late resolution delegates to us the creation of the `LocalDefId`.
775755
let _def_id = self.create_def(
776-
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
756+
self.current_hir_id_owner.def_id,
777757
param,
778758
kw::UnderscoreLifetime,
779759
DefKind::LifetimeParam,
@@ -1466,17 +1446,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14661446
let opaque_ty_hir_id = self.lower_node_id(opaque_ty_node_id);
14671447
debug!(?opaque_ty_def_id, ?opaque_ty_hir_id);
14681448

1469-
let opaque_ty_def = self.with_def_id_parent(opaque_ty_def_id, |this| {
1470-
let bounds = lower_item_bounds(this);
1471-
let opaque_ty_def = hir::OpaqueTy {
1472-
hir_id: opaque_ty_hir_id,
1473-
def_id: opaque_ty_def_id,
1474-
bounds,
1475-
origin,
1476-
span: this.lower_span(opaque_ty_span),
1477-
};
1478-
this.arena.alloc(opaque_ty_def)
1479-
});
1449+
let bounds = lower_item_bounds(self);
1450+
let opaque_ty_def = hir::OpaqueTy {
1451+
hir_id: opaque_ty_hir_id,
1452+
def_id: opaque_ty_def_id,
1453+
bounds,
1454+
origin,
1455+
span: self.lower_span(opaque_ty_span),
1456+
};
1457+
let opaque_ty_def = self.arena.alloc(opaque_ty_def);
14801458

14811459
hir::TyKind::OpaqueDef(opaque_ty_def)
14821460
}
@@ -2084,7 +2062,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20842062
} else {
20852063
// Construct an AnonConst where the expr is the "ty"'s path.
20862064

2087-
let parent_def_id = self.current_def_id_parent;
2065+
let parent_def_id = self.current_hir_id_owner.def_id;
20882066
let node_id = self.next_node_id();
20892067
let span = self.lower_span(span);
20902068

@@ -2108,9 +2086,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21082086
self.arena.alloc(hir::AnonConst {
21092087
def_id,
21102088
hir_id,
2111-
body: this.with_def_id_parent(def_id, |this| {
2112-
this.lower_const_body(path_expr.span, Some(&path_expr))
2113-
}),
2089+
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
21142090
span,
21152091
})
21162092
});
@@ -2187,9 +2163,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21872163
hir::AnonConst {
21882164
def_id,
21892165
hir_id,
2190-
body: this.with_def_id_parent(def_id, |this| {
2191-
this.lower_const_body(c.value.span, Some(&c.value))
2192-
}),
2166+
body: this.lower_const_body(c.value.span, Some(&c.value)),
21932167
span: this.lower_span(c.value.span),
21942168
}
21952169
}))

0 commit comments

Comments
 (0)