Skip to content
Merged
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
4 changes: 3 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 @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_passes/src/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/mgca/type_const-pub.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading