Fix ICE: When Trying to check visibility of a #[type_const], check RHS instead.

We want to evaluate the rhs of a type_const.

Also added an early return/guard in eval_in_interpreter which is used in functions like `eval_to_allocation_raw_provider`

Lastly add a debug assert to `thir_body()` if we have gotten there with a type_const something as gone wrong.

Get rid of a call to is_type_const() and instead use a match arm.

Change this is_type_const() check to a debug_assert!()

Change to use an if else statment instead.

Update type_const-pub.rs

Fix formatting.

Noticed that this is the same check as is_type_const() centralize it.

Add test case for pub type_const.
This commit is contained in:
Keith-Cancel 2026-01-13 13:02:33 -08:00
parent 1a9f1686b2
commit 4c93efae2b
5 changed files with 21 additions and 5 deletions

View file

@ -394,8 +394,10 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
typing_env: ty::TypingEnv<'tcx>,
) -> Result<R, ErrorHandled> {
let def = cid.instance.def.def_id();
let is_static = tcx.is_static(def);
// #[type_const] don't have bodys
debug_assert!(!tcx.is_type_const(def), "CTFE tried to evaluate type-const: {:?}", def);
let is_static = tcx.is_static(def);
let mut ecx = InterpCx::new(
tcx,
tcx.def_span(def),

View file

@ -1379,9 +1379,8 @@ fn should_encode_const(def_kind: DefKind) -> bool {
}
fn should_encode_const_of_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool {
matches!(def_kind, DefKind::Const | DefKind::AssocConst)
&& find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
// AssocConst ==> assoc item has value
// AssocConst ==> assoc item has value
tcx.is_type_const(def_id)
&& (!matches!(def_kind, DefKind::AssocConst) || assoc_item_has_value(tcx, def_id))
}

View file

@ -18,6 +18,8 @@ pub(crate) fn thir_body(
tcx: TyCtxt<'_>,
owner_def: LocalDefId,
) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> {
debug_assert!(!tcx.is_type_const(owner_def.to_def_id()), "thir_body queried for type_const");
let body = tcx.hir_body_owned_by(owner_def);
let mut cx = ThirBuildCx::new(tcx, owner_def);
if let Some(reported) = cx.typeck_results.tainted_by_errors {

View file

@ -209,7 +209,10 @@ impl<'tcx> ReachableContext<'tcx> {
self.visit_nested_body(body);
}
}
// For #[type_const] we want to evaluate the RHS.
hir::ItemKind::Const(_, _, _, init @ hir::ConstItemRhs::TypeConst(_)) => {
self.visit_const_item_rhs(init);
}
hir::ItemKind::Const(_, _, _, init) => {
// Only things actually ending up in the final constant value are reachable
// for codegen. Everything else is only needed during const-eval, so even if

View file

@ -0,0 +1,10 @@
//@ check-pass
// This previously caused an ICE when checking reachability of a pub const item
// This is because reachability also tried to evaluate the #[type_const] which
// requires the item have a body. #[type_const] do not have bodies.
#![expect(incomplete_features)]
#![feature(min_generic_const_args)]
#[type_const]
pub const TYPE_CONST : usize = 1;
fn main() {}