Skip to content

merge DefKind::InlineConst into AnonConst#158767

Open
khyperia wants to merge 1 commit into
rust-lang:mainfrom
khyperia:merge-inlineconst
Open

merge DefKind::InlineConst into AnonConst#158767
khyperia wants to merge 1 commit into
rust-lang:mainfrom
khyperia:merge-inlineconst

Conversation

@khyperia

@khyperia khyperia commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

This is a closely related followup to #158375 (a condition of merging that PR was doing this as a followup)

This merge conflicts with #158617 ; prefer merging that one first please~

This PR is within the general goal of const generics / generic_const_args and perhaps specifically rust-lang/project-const-generics#108 but is mostly a code cleanup rather than implementing any particular feature


Anon consts (the 1 + 2 in let x: [u8; 1 + 2];) are closely related to inline consts (the const {} in let x = const { 1 + 2 };). They are especially related now that both can be used in the type system (by the above PR) and should be treated identically in that scenario. In general, they should be treated the same, as evidenced by the number of matches touched in this PR that just match on both. This PR merges the two DefKinds into one, mostly arbitrarily and bikesheddily choosing to use the name AnonConst for the merged concept (e.g. both DefKind::InlineConst and DefKind::AnonConst used ast::AnonConst, so ast::AnonConst should probably be named whatever the merged DefKind concept is named).

When you absolute must distinguish between the two, tcx.anon_const_kind allows you to do so - however, anon consts/inline consts in the type system intentionally appear identically under anon_const_kind. Indeed, the places where we distinguish between the two raised my eyebrows a few times when doing this PR, and being a little more explicit and loud about it is helpful for code clarity, IMO.

r? @BoxyUwU

@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

HIR ty lowering was modified

cc @fmease

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. labels Jul 4, 2026
@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 4, 2026
@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

BoxyUwU is currently at their maximum review capacity.
They may take a while to respond.

kind: hir::ExprKind::ConstBlock(..) | hir::ExprKind::InlineAsm(..),
..
}) => ty::AnonConstKind::NonTypeSystemInline,
_ => ty::AnonConstKind::NonTypeSystemAnon,

@khyperia khyperia Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure about this catch-all, tbh. There's three options I see:

  • leave it as a catch-all
  • explicitly list which parents are allowed to have an AnonConst child, panic if there's an unexpected parent
    • afaik it's just Node::Field and Node::Variant
  • add variants to AnonConstKind that actually list which specific kind of anon const this non-type-system anon const is - for example, having the AnonConstKind variants of ConstBlock, AsmConstBlock, FieldAnonConst, VariantAnonConst, and then perhaps a fn AnonConstKind::is_type_system() -> bool or somethin'
    • there's a few callers that we match on the hir parent again to figure out which type of non-type-system anon const it is, so just returning it here seems useful

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make sense to make this NonTypeSystem(NonTypeSystemAnonConstKind) (better name pending) 🤔 unsure :3 I like the option of specifying which kind of anon const it is if that makes a lot of callers simpler (and it's weird to only do it for inline vs non inline lol)

// Field defaults are allowed to use generic parameters, e.g. `field: u32 = /*defid: N + 1*/`
ty::AnonConstKind::NonTypeSystem
ty::AnonConstKind::NonTypeSystemAnon
if matches!(tcx.parent_hir_node(hir_id), Node::TyPat(_) | Node::Field(_)) =>

@khyperia khyperia Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node::TyPat(_) appears to be dead code here - or at the very least, it's untested, replacing it with a panic and ./x test tests/ui does not fire. Was added in #136284 and code has been heavily refactored since then - haven't fully investigated yet.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah I wouldn't expect pattern types to contain non type system anon consts? that feels weird :3

Comment on lines -463 to -473
ExprKind::ConstBlock(constant) => {
for attr in &expr.attrs {
visit::walk_attribute(self, attr);
}

let def = self
.create_def(constant.id, None, DefKind::InlineConst, constant.value.span)
.def_id();
// use specifically walk_anon_const, not walk_expr, to skip self.visit_anon_const
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
}

@khyperia khyperia Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yippee, less def_collector jank!

View changes since the review

ForeignMod,
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`.
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`, or an inline
/// constant, e.g. `const { 1 + 2 }`.

@BoxyUwU BoxyUwU Jul 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to be more explicit that there are other kinds of anon consts too (e.g. enum discriminants)

View changes since the review

// Field defaults are allowed to use generic parameters, e.g. `field: u32 = /*defid: N + 1*/`
ty::AnonConstKind::NonTypeSystem
ty::AnonConstKind::NonTypeSystemAnon
if matches!(tcx.parent_hir_node(hir_id), Node::TyPat(_) | Node::Field(_)) =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah I wouldn't expect pattern types to contain non type system anon consts? that feels weird :3

// Also skip items for which typeck forwards to parent typeck.
if !(def_kind == DefKind::AnonConst
|| def_kind == DefKind::InlineConst && tcx.is_type_system_inline_const(item_def_id)
&& tcx.anon_const_kind(item_def_id) != ty::AnonConstKind::NonTypeSystemInline

@BoxyUwU BoxyUwU Jul 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now allows feeding type_of for NonTypeSystemAnon I guess? I think that's unintentional?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was previously allowed! This is one of the cases where my eyebrows were raised:

Indeed, the places where we distinguish between the two raised my eyebrows a few times when doing this PR, and being a little more explicit and loud about it is helpful for code clarity, IMO.

With this PR, before this PR, as well as before #158375 we blindly accepted all DefKind::AnonConsts here, including non type system ones. I tried to keep the existing behavior as close as possible, with the intent of making wonky behavior more apparent. If you think the behavior should change, I'll look into it!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's quite fun, I would expect we never feed non type system anon consts lol. cc @oli-obk

fine to keep behaviour the same for this PR

Comment thread compiler/rustc_resolve/src/def_collector.rs
DefKind::InlineConst,
anon_const.value.span,
)
.create_def(anon_const.id, None, DefKind::AnonConst, anon_const.value.span)

@BoxyUwU BoxyUwU Jul 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did not previously know we used DefKind::InlineConst for inline asm stuff lol 😅

View changes since the review

// the interesting parts are solely in the signature of the containing item's opaque type
// or dyn type.
DefKind::InlineConst | DefKind::Closure | DefKind::SyntheticCoroutineBody => {}
DefKind::AnonConst | DefKind::Closure | DefKind::SyntheticCoroutineBody => {}

@BoxyUwU BoxyUwU Jul 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we actually put the DefKind::AnonConst handling up next to the other arm about AnonConst. rn for this arm to make sense and not give alarm bells you need to have the context of the other arm :3

View changes since the review

kind: hir::ExprKind::ConstBlock(..) | hir::ExprKind::InlineAsm(..),
..
}) => ty::AnonConstKind::NonTypeSystemInline,
_ => ty::AnonConstKind::NonTypeSystemAnon,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make sense to make this NonTypeSystem(NonTypeSystemAnonConstKind) (better name pending) 🤔 unsure :3 I like the option of specifying which kind of anon const it is if that makes a lot of callers simpler (and it's weird to only do it for inline vs non inline lol)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants