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

// 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

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

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