Skip to content

Commit 5f05a18

Browse files
committed
def_collector: rework trivial const-arg handling
see the comment on `handle_lazy_anon_const_def`
1 parent 0b1bf71 commit 5f05a18

File tree

11 files changed

+527
-197
lines changed

11 files changed

+527
-197
lines changed

compiler/rustc_ast/src/ast.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1187,14 +1187,15 @@ pub struct Expr {
11871187
}
11881188

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

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

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
223223
let parent_def_id = self.current_def_id_parent;
224224
let node_id = self.next_node_id();
225225
// HACK(min_generic_const_args): see lower_anon_const
226-
if !expr.is_potential_trivial_const_arg(true) {
226+
if !expr.is_potential_trivial_const_arg() {
227227
self.create_def(
228228
parent_def_id,
229229
node_id,

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
456456
let node_id = self.next_node_id();
457457

458458
// HACK(min_generic_const_args): see lower_anon_const
459-
if !arg.is_potential_trivial_const_arg(true) {
459+
if !arg.is_potential_trivial_const_arg() {
460460
// Add a definition for the in-band const def.
461461
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
462462
}

compiler/rustc_ast_lowering/src/lib.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
509509
def_id
510510
}
511511

512+
// HACK: See `DefCollector::handle_lazy_anon_const_def` for why this is necessary.
513+
fn create_or_reuse_anon_const_def(
514+
&mut self,
515+
parent: LocalDefId,
516+
node_id: ast::NodeId,
517+
name: Symbol,
518+
def_kind: DefKind,
519+
span: Span,
520+
) -> LocalDefId {
521+
debug_assert_eq!(def_kind, DefKind::AnonConst);
522+
if let Some(def_id) = self.opt_local_def_id(node_id) {
523+
def_id
524+
} else {
525+
self.create_def(parent, node_id, name, def_kind, span)
526+
}
527+
}
528+
512529
fn next_node_id(&mut self) -> NodeId {
513530
let start = self.resolver.next_node_id;
514531
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
@@ -2181,19 +2198,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21812198
/// See [`hir::ConstArg`] for when to use this function vs
21822199
/// [`Self::lower_anon_const_to_const_arg`].
21832200
fn lower_anon_const_to_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2184-
if c.value.is_potential_trivial_const_arg(true) {
2185-
// HACK(min_generic_const_args): see DefCollector::visit_anon_const
2186-
// Over there, we guess if this is a bare param and only create a def if
2187-
// we think it's not. However we may can guess wrong (see there for example)
2188-
// in which case we have to create the def here.
2189-
self.create_def(
2190-
self.current_def_id_parent,
2191-
c.id,
2192-
kw::Empty,
2193-
DefKind::AnonConst,
2194-
c.value.span,
2195-
);
2196-
}
2201+
self.create_or_reuse_anon_const_def(
2202+
self.current_def_id_parent,
2203+
c.id,
2204+
kw::Empty,
2205+
DefKind::AnonConst,
2206+
c.value.span,
2207+
);
21972208

21982209
self.arena.alloc(self.with_new_scopes(c.value.span, |this| {
21992210
let def_id = this.local_def_id(c.id);

0 commit comments

Comments
 (0)