Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ExprKind::ConstBlock(anon_const) => {
let def_id = self.local_def_id(anon_const.id);
assert_eq!(DefKind::InlineConst, self.tcx.def_kind(def_id));
assert_eq!(DefKind::AnonConst, self.tcx.def_kind(def_id));
self.lower_anon_const_to_const_arg(anon_const, span)
}
_ => overly_complex_const(self),
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
if uv.promoted.is_none() {
let tcx = self.tcx();
let def_id = uv.def;
if tcx.def_kind(def_id) == DefKind::InlineConst {
if tcx.def_kind(def_id) == DefKind::AnonConst
&& tcx.anon_const_kind(def_id) == ty::AnonConstKind::NonTypeSystemInline
{
let def_id = def_id.expect_local();
let predicates = self.prove_closure_bounds(tcx, def_id, uv.args, location);
self.normalize_and_prove_instantiated_predicates(
Expand Down Expand Up @@ -2656,7 +2658,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// length as the `typeck_root_args`.
&args[..typeck_root_args.len()]
}
DefKind::InlineConst => args.as_inline_const().parent_args(),
DefKind::AnonConst
if tcx.anon_const_kind(def_id) == ty::AnonConstKind::NonTypeSystemInline =>
{
args.as_inline_const().parent_args()
}
other => bug!("unexpected item {:?}", other),
};
let parent_args = tcx.mk_args(parent_args);
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,10 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {

BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => {
match tcx.def_kind(self.mir_def) {
DefKind::InlineConst if !tcx.is_type_system_inline_const(self.mir_def) => {
DefKind::AnonConst
if tcx.anon_const_kind(self.mir_def)
== ty::AnonConstKind::NonTypeSystemInline =>
{
// This is required for `AscribeUserType` canonical query, which will call
// `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes
// into borrowck, which is ICE #78174.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ fn setup_for_eval<'tcx>(
| DefKind::Static { .. }
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst { .. }
),
"Unexpected DefKind: {:?}",
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ pub enum DefKind {
Use,
/// An `extern` block.
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

///
/// Not all anon-consts are actually still relevant in the HIR. We lower
/// trivial const-arguments directly to `hir::ConstArgKind::Path`, at which
Expand All @@ -176,8 +177,6 @@ pub enum DefKind {
/// constants should only be reachable by iterating all definitions of a
/// given crate, you should not have to worry about this.
AnonConst,
/// An inline constant, e.g. `const { 1 + 2 }`
InlineConst,
/// Opaque type, aka `impl Trait`.
OpaqueTy,
/// A field in a struct, enum or union. e.g.
Expand Down Expand Up @@ -239,7 +238,6 @@ impl DefKind {
DefKind::Use => "import",
DefKind::ForeignMod => "foreign module",
DefKind::AnonConst => "constant expression",
DefKind::InlineConst => "inline constant",
DefKind::Field => "field",
DefKind::Impl { .. } => "implementation",
DefKind::Closure => "closure",
Expand All @@ -263,7 +261,6 @@ impl DefKind {
| DefKind::OpaqueTy
| DefKind::Impl { .. }
| DefKind::Use
| DefKind::InlineConst
| DefKind::ExternCrate => "an",
DefKind::Macro(kinds) => kinds.article(),
_ => "a",
Expand Down Expand Up @@ -296,7 +293,6 @@ impl DefKind {

// Not namespaced.
DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::ExternCrate
Expand Down Expand Up @@ -343,7 +339,6 @@ impl DefKind {
DefKind::Use => DefPathData::Use,
DefKind::ForeignMod => DefPathData::ForeignMod,
DefKind::AnonConst => DefPathData::AnonConst,
DefKind::InlineConst => DefPathData::AnonConst,
DefKind::OpaqueTy => DefPathData::OpaqueTy,
DefKind::GlobalAsm => DefPathData::GlobalAsm,
DefKind::Impl { .. } => DefPathData::Impl,
Expand Down Expand Up @@ -390,7 +385,6 @@ impl DefKind {
| DefKind::Fn
| DefKind::ForeignTy
| DefKind::Impl { .. }
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Static { .. }
| DefKind::Struct
Expand Down Expand Up @@ -443,7 +437,6 @@ impl DefKind {
| DefKind::ConstParam
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm
| DefKind::ExternCrate => false,
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),

// These have no wf checks
DefKind::AnonConst
| DefKind::InlineConst
| DefKind::ExternCrate
| DefKind::Macro(..)
| DefKind::Use
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,6 @@ fn lint_redundant_lifetimes<'tcx>(
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,29 +1635,32 @@ fn const_param_default<'tcx>(
}

fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKind {
debug_assert_matches!(tcx.def_kind(def), DefKind::AnonConst | DefKind::InlineConst);
debug_assert_matches!(tcx.def_kind(def), DefKind::AnonConst);
let hir_id = tcx.local_def_id_to_hir_id(def);
let const_arg_id = tcx.parent_hir_id(hir_id);
match tcx.hir_node(const_arg_id) {
let parent_node_id = tcx.parent_hir_id(hir_id);
match tcx.hir_node(parent_node_id) {
hir::Node::ConstArg(const_arg) => {
debug_assert_matches!(const_arg.kind, hir::ConstArgKind::Anon(hir::AnonConst { def_id, .. }) if *def_id == def);
let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
if tcx.features().generic_const_exprs() {
ty::AnonConstKind::GCE
} else if tcx.features().min_generic_const_args() {
ty::AnonConstKind::MCG
} else if let hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Repeat(_, repeat_count),
..
}) = parent_hir_node
&& repeat_count.hir_id == const_arg_id
}) = tcx.parent_hir_node(parent_node_id)
&& repeat_count.hir_id == parent_node_id
{
ty::AnonConstKind::RepeatExprCount
} else {
ty::AnonConstKind::MCG
}
}
_ => ty::AnonConstKind::NonTypeSystem,
hir::Node::Expr(hir::Expr {
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)

}
}

Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
ty::AnonConstKind::GCE => Some(parent_did),

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

{
Some(parent_did)
}
// Default to no generic parameters for other kinds of anon consts
ty::AnonConstKind::NonTypeSystem => None,
ty::AnonConstKind::NonTypeSystemAnon => None,
ty::AnonConstKind::NonTypeSystemInline => span_bug!(
tcx.def_span(def_id),
"a DefKind::AnonConst with a HIR parent of hir::Node::AnonConst should never be an AnonConstKind::NonTypeSystemInline"
),
}
}
Node::ConstBlock(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
| DefKind::ForeignTy
| DefKind::GlobalAsm
| DefKind::Impl { .. }
| DefKind::InlineConst
| DefKind::LifetimeParam
| DefKind::Macro(_)
| DefKind::Mod
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let tcx = self.tcx();
let parent_def_id = self.item_def_id();
if let Res::Def(DefKind::ConstParam, _) = res
&& matches!(tcx.def_kind(parent_def_id), DefKind::AnonConst | DefKind::InlineConst)
&& tcx.def_kind(parent_def_id) == DefKind::AnonConst
&& let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id)
{
let folder = ForbidParamUsesFolder {
Expand All @@ -507,22 +507,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
/// contains params). Those cases are handled by `check_param_uses_if_mcg`.
fn anon_const_forbids_generic_params(&self) -> Option<ForbidParamContext> {
let tcx = self.tcx();
let parent_def_id = self.item_def_id();
let item_def_id = self.item_def_id();

// Inline consts and closures can be nested inside anon consts that forbid generic
// params (e.g. an enum discriminant). Walk up the def parent chain to find the
// nearest enclosing AnonConst and use that to determine the context.
let parent_def_id = tcx.typeck_root_def_id(parent_def_id.into());
let anon_const_def_id = tcx.typeck_root_def_id_local(item_def_id);

let anon_const_def_id = match tcx.def_kind(parent_def_id) {
DefKind::AnonConst => parent_def_id,
DefKind::InlineConst if tcx.is_type_system_inline_const(parent_def_id) => parent_def_id,
_ => return None,
};
if tcx.def_kind(anon_const_def_id) != DefKind::AnonConst {
return None;
}

match tcx.anon_const_kind(anon_const_def_id) {
ty::AnonConstKind::MCG => Some(ForbidParamContext::ConstArgument),
ty::AnonConstKind::NonTypeSystem => {
ty::AnonConstKind::NonTypeSystemAnon => {
// NonTypeSystem anon consts only have accessible generic parameters in specific
// positions (ty patterns and field defaults — see `generics_of`). In all other
// positions (e.g. enum discriminants) generic parameters are not in scope.
Expand All @@ -532,7 +530,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
None
}
}
ty::AnonConstKind::GCE | ty::AnonConstKind::RepeatExprCount => None,
ty::AnonConstKind::NonTypeSystemInline
| ty::AnonConstKind::GCE
| ty::AnonConstKind::RepeatExprCount => None,
}
}

Expand Down Expand Up @@ -2973,7 +2973,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field
| DefKind::Impl { .. }
| DefKind::Closure
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
// `feed_anon_const_type`.
// 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

|| tcx.is_typeck_child(item_def_id.to_def_id()))
{
tcx.ensure_ok().typeck(item_def_id);
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_hir_typeck/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir
CheckLoopVisitor { tcx, cx_stack: vec![Normal], block_breaks: Default::default() };
let cx = match tcx.def_kind(def_id) {
DefKind::AnonConst => AnonConst,
DefKind::InlineConst => {
// only type system inline consts are typeck roots
debug_assert!(tcx.is_type_system_inline_const(def_id));
ConstBlock
}
_ => Fn,
};
check.with_context(cx, |v| v.visit_body(body));
Expand Down
22 changes: 3 additions & 19 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,6 @@ fn should_encode_span(def_kind: DefKind) -> bool {
| DefKind::ExternCrate
| DefKind::Use
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::Impl { .. }
Expand Down Expand Up @@ -962,7 +961,6 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
| DefKind::ExternCrate
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::LifetimeParam
| DefKind::Static { nested: true, .. }
Expand Down Expand Up @@ -996,7 +994,6 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool {
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam
Expand Down Expand Up @@ -1032,7 +1029,6 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
| DefKind::ConstParam
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Static { nested: true, .. }
| DefKind::OpaqueTy
| DefKind::GlobalAsm
Expand Down Expand Up @@ -1071,7 +1067,6 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
DefKind::Use
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm
| DefKind::Closure
| DefKind::ExternCrate
Expand Down Expand Up @@ -1108,10 +1103,7 @@ fn should_encode_mir(
// instance_mir uses mir_for_ctfe rather than optimized_mir for constructors
DefKind::Ctor(_, _) => (true, false),
// Constants
DefKind::AnonConst { .. }
| DefKind::InlineConst
| DefKind::AssocConst { .. }
| DefKind::Const { .. } => (true, false),
DefKind::AnonConst | DefKind::AssocConst { .. } | DefKind::Const { .. } => (true, false),
// Coroutines require optimized MIR to compute layout.
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
DefKind::SyntheticCoroutineBody => (false, true),
Expand Down Expand Up @@ -1165,7 +1157,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
| DefKind::Use
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm
| DefKind::Closure
| DefKind::ExternCrate
Expand All @@ -1191,7 +1182,6 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
| DefKind::AssocFn
| DefKind::AssocConst { .. }
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Impl { .. }
| DefKind::Field
Expand Down Expand Up @@ -1228,7 +1218,6 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
| DefKind::Closure
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::SyntheticCoroutineBody => true,

DefKind::OpaqueTy => {
Expand Down Expand Up @@ -1290,7 +1279,6 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
| DefKind::Closure
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::Trait
Expand Down Expand Up @@ -1327,7 +1315,6 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
| DefKind::Impl { .. }
| DefKind::ForeignTy
| DefKind::ConstParam
| DefKind::InlineConst
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::Trait
Expand All @@ -1348,10 +1335,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
fn should_encode_const(def_kind: DefKind) -> bool {
match def_kind {
// FIXME(mgca): should we remove Const and AssocConst here?
DefKind::Const { .. }
| DefKind::AssocConst { .. }
| DefKind::AnonConst
| DefKind::InlineConst => true,
DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::AnonConst => true,

DefKind::Struct
| DefKind::Union
Expand Down Expand Up @@ -1624,7 +1608,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
<- tcx.explicit_implied_const_bounds(def_id).skip_binder());
}
}
if let DefKind::AnonConst | DefKind::InlineConst = def_kind {
if let DefKind::AnonConst = def_kind {
record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
}
if should_encode_const_of_item(self.tcx, def_id, def_kind) {
Expand Down
Loading
Loading