Skip to content

Commit 7e565cc

Browse files
committed
Auto merge of #133468 - lcnr:uwu4, r=BoxyUwU
always create `DefId`s for anon consts but don't use them anywhere, we intentionally don't encode them in the crate metadata. best reviewed by disabling whitespace. This pretty much reimplements #133285 while adding the tests of #133455. Fixes #133064 r? `@BoxyUwU` `@compiler-errors`
2 parents 9b4d7c6 + d401a07 commit 7e565cc

File tree

14 files changed

+485
-318
lines changed

14 files changed

+485
-318
lines changed

compiler/rustc_ast/src/ast.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1184,14 +1184,15 @@ pub struct Expr {
11841184
}
11851185

11861186
impl Expr {
1187-
/// Is this expr either `N`, or `{ N }`.
1187+
/// Could this expr be either `N`, or `{ N }`, where `N` is a const parameter.
11881188
///
11891189
/// If this is not the case, name resolution does not resolve `N` when using
11901190
/// `min_const_generics` as more complex expressions are not supported.
11911191
///
11921192
/// Does not ensure that the path resolves to a const param, the caller should check this.
1193-
pub fn is_potential_trivial_const_arg(&self, strip_identity_block: bool) -> bool {
1194-
let this = if strip_identity_block { self.maybe_unwrap_block() } else { self };
1193+
/// This also does not consider macros, so it's only correct after macro-expansion.
1194+
pub fn is_potential_trivial_const_arg(&self) -> bool {
1195+
let this = self.maybe_unwrap_block();
11951196

11961197
if let ExprKind::Path(None, path) = &this.kind
11971198
&& path.is_potential_trivial_const_arg()

compiler/rustc_ast_lowering/src/asm.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
227227
};
228228

229229
// Wrap the expression in an AnonConst.
230-
let parent_def_id = self.current_def_id_parent;
230+
let parent_def_id = self.current_hir_id_owner.def_id;
231231
let node_id = self.next_node_id();
232-
// HACK(min_generic_const_args): see lower_anon_const
233-
if !expr.is_potential_trivial_const_arg(true) {
234-
self.create_def(
235-
parent_def_id,
236-
node_id,
237-
kw::Empty,
238-
DefKind::AnonConst,
239-
*op_sp,
240-
);
241-
}
232+
self.create_def(
233+
parent_def_id,
234+
node_id,
235+
kw::Empty,
236+
DefKind::AnonConst,
237+
*op_sp,
238+
);
242239
let anon_const = AnonConst { id: node_id, value: P(expr) };
243240
hir::InlineAsmOperand::SymFn {
244241
anon_const: self.lower_anon_const_to_anon_const(&anon_const),

compiler/rustc_ast_lowering/src/expr.rs

+48-62
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,15 +450,9 @@ 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();
457-
458-
// HACK(min_generic_const_args): see lower_anon_const
459-
if !arg.is_potential_trivial_const_arg(true) {
460-
// Add a definition for the in-band const def.
461-
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
462-
}
463-
455+
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
464456
let mut visitor = WillCreateDefIdsVisitor {};
465457
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
466458
AstP(Expr {
@@ -759,19 +751,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
759751
lifetime_elision_allowed: false,
760752
});
761753

762-
let body = self.with_def_id_parent(closure_def_id, move |this| {
763-
this.lower_body(move |this| {
764-
this.coroutine_kind = Some(coroutine_kind);
754+
let body = self.lower_body(move |this| {
755+
this.coroutine_kind = Some(coroutine_kind);
765756

766-
let old_ctx = this.task_context;
767-
if task_context.is_some() {
768-
this.task_context = task_context;
769-
}
770-
let res = body(this);
771-
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;
772763

773-
(params, res)
774-
})
764+
(params, res)
775765
});
776766

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

10581048
let (body_id, closure_kind) = self.with_new_scopes(fn_decl_span, move |this| {
1059-
this.with_def_id_parent(closure_def_id, move |this| {
1060-
let mut coroutine_kind = if this
1061-
.attrs
1062-
.get(&closure_hir_id.local_id)
1063-
.is_some_and(|attrs| attrs.iter().any(|attr| attr.has_name(sym::coroutine)))
1064-
{
1065-
Some(hir::CoroutineKind::Coroutine(Movability::Movable))
1066-
} else {
1067-
None
1068-
};
1069-
let body_id = this.lower_fn_body(decl, |this| {
1070-
this.coroutine_kind = coroutine_kind;
1071-
let e = this.lower_expr_mut(body);
1072-
coroutine_kind = this.coroutine_kind;
1073-
e
1074-
});
1075-
let coroutine_option =
1076-
this.closure_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
1077-
(body_id, coroutine_option)
1078-
})
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)
10791067
});
10801068

10811069
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
@@ -1165,28 +1153,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
11651153
);
11661154

11671155
let body = self.with_new_scopes(fn_decl_span, |this| {
1168-
this.with_def_id_parent(closure_def_id, |this| {
1169-
let inner_decl =
1170-
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1171-
1172-
// Transform `async |x: u8| -> X { ... }` into
1173-
// `|x: u8| || -> X { ... }`.
1174-
let body_id = this.lower_body(|this| {
1175-
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1176-
&inner_decl,
1177-
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
1178-
fn_decl_span,
1179-
body.span,
1180-
coroutine_kind,
1181-
hir::CoroutineSource::Closure,
1182-
);
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+
);
11831170

1184-
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);
11851172

1186-
(parameters, expr)
1187-
});
1188-
body_id
1189-
})
1173+
(parameters, expr)
1174+
});
1175+
body_id
11901176
});
11911177

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

compiler/rustc_ast_lowering/src/lib.rs

+18-55
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
});
@@ -2159,7 +2135,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21592135
None,
21602136
);
21612137

2162-
return ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) };
2138+
return ConstArg {
2139+
hir_id: self.lower_node_id(anon.id),
2140+
kind: hir::ConstArgKind::Path(qpath),
2141+
};
21632142
}
21642143

21652144
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
@@ -2169,29 +2148,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21692148
/// See [`hir::ConstArg`] for when to use this function vs
21702149
/// [`Self::lower_anon_const_to_const_arg`].
21712150
fn lower_anon_const_to_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2172-
if c.value.is_potential_trivial_const_arg(true) {
2173-
// HACK(min_generic_const_args): see DefCollector::visit_anon_const
2174-
// Over there, we guess if this is a bare param and only create a def if
2175-
// we think it's not. However we may can guess wrong (see there for example)
2176-
// in which case we have to create the def here.
2177-
self.create_def(
2178-
self.current_def_id_parent,
2179-
c.id,
2180-
kw::Empty,
2181-
DefKind::AnonConst,
2182-
c.value.span,
2183-
);
2184-
}
2185-
21862151
self.arena.alloc(self.with_new_scopes(c.value.span, |this| {
21872152
let def_id = this.local_def_id(c.id);
21882153
let hir_id = this.lower_node_id(c.id);
21892154
hir::AnonConst {
21902155
def_id,
21912156
hir_id,
2192-
body: this.with_def_id_parent(def_id, |this| {
2193-
this.lower_const_body(c.value.span, Some(&c.value))
2194-
}),
2157+
body: this.lower_const_body(c.value.span, Some(&c.value)),
21952158
span: this.lower_span(c.value.span),
21962159
}
21972160
}))

compiler/rustc_hir/src/def.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,16 @@ pub enum DefKind {
109109
Use,
110110
/// An `extern` block.
111111
ForeignMod,
112-
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
112+
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`.
113+
///
114+
/// Not all anon-consts are actually still relevant in the HIR. We lower
115+
/// trivial const-arguments directly to `hir::ConstArgKind::Path`, at which
116+
/// point the definition for the anon-const ends up unused and incomplete.
117+
///
118+
/// We do not provide any a `Span` for the definition and pretty much all other
119+
/// queries also ICE when using this `DefId`. Given that the `DefId` of such
120+
/// constants should only be reachable by iterating all definitions of a
121+
/// given crate, you should not have to worry about this.
113122
AnonConst,
114123
/// An inline constant, e.g. `const { 1 + 2 }`
115124
InlineConst,

compiler/rustc_metadata/src/rmeta/encoder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,20 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13831383
let def_id = local_id.to_def_id();
13841384
let def_kind = tcx.def_kind(local_id);
13851385
self.tables.def_kind.set_some(def_id.index, def_kind);
1386+
1387+
// The `DefCollector` will sometimes create unnecessary `DefId`s
1388+
// for trivial const arguments which are directly lowered to
1389+
// `ConstArgKind::Path`. We never actually access this `DefId`
1390+
// anywhere so we don't need to encode it for other crates.
1391+
if def_kind == DefKind::AnonConst
1392+
&& matches!(
1393+
tcx.hir_node_by_def_id(local_id),
1394+
hir::Node::ConstArg(hir::ConstArg { kind: hir::ConstArgKind::Path(..), .. })
1395+
)
1396+
{
1397+
continue;
1398+
}
1399+
13861400
if should_encode_span(def_kind) {
13871401
let def_span = tcx.def_span(local_id);
13881402
record!(self.tables.def_span[def_id] <- def_span);

0 commit comments

Comments
 (0)