Skip to content

Commit 9f6cd6d

Browse files
committed
Auto merge of #151085 - Keith-Cancel:mgca2, r=BoxyUwU
Fix ICE: When Trying to check visibility of a #[type_const], check RHS instead. This PR fixes #150956 for min_const_generic_args #132980. The first part of this PR checks in `reachable.rs` if we have a type const use `visit_const_item_rhs(init);` instead of calling `const_eval_poly_to_alloc()` The second part is I noticed that if `const_eval_poly_to_alloc()` returns a `ErrorHandled::TooGeneric` then switches to `visit_const_item_rhs()`. So further up `const_eval_poly_to_alloc()` call order it eventual gets to `eval_in_interpreter()`. So I added a debug_assert in `eval_in_interpreter()` if we happen to try and run CTFE on a `type_const`. The other bit is just a test case, and some duplicated code I noticed. While the PR does get rid of the ICE for a type_const with `pub` visibility. If I try using the const though it will ICE with the following: ``` error: internal compiler error: /home/keith/GitHub/rust_kc/compiler/rustc_const_eval/src/check_consts/qualifs.rs:355:13: expected ConstKind::Param or ConstKind::Value here, found UnevaluatedConst { def: DefId(20:4 ~ colorkit[c783]::TYPE_CONST), args: [] } thread 'rustc' (819687) panicked at /home/keith/GitHub/rust_kc/compiler/rustc_const_eval/src/check_consts/qualifs.rs:355:13: ``` I suspect it is a similar issue to inherent associated consts. Since if I apply the same fix I had here: #150799 (comment) I then can use the const internally and in external crates without any issues. Although, did not include that fix since if it is a similar issue it would need to be addressed elsewhere. r? @BoxyUwU @rustbot label +F-associated_const_equality +F-min_generic_const_args
2 parents 844f131 + 4c93efa commit 9f6cd6d

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,10 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
394394
typing_env: ty::TypingEnv<'tcx>,
395395
) -> Result<R, ErrorHandled> {
396396
let def = cid.instance.def.def_id();
397-
let is_static = tcx.is_static(def);
397+
// #[type_const] don't have bodys
398+
debug_assert!(!tcx.is_type_const(def), "CTFE tried to evaluate type-const: {:?}", def);
398399

400+
let is_static = tcx.is_static(def);
399401
let mut ecx = InterpCx::new(
400402
tcx,
401403
tcx.def_span(def),

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,9 +1379,8 @@ fn should_encode_const(def_kind: DefKind) -> bool {
13791379
}
13801380

13811381
fn should_encode_const_of_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool {
1382-
matches!(def_kind, DefKind::Const | DefKind::AssocConst)
1383-
&& find_attr!(tcx.get_all_attrs(def_id), AttributeKind::TypeConst(_))
1384-
// AssocConst ==> assoc item has value
1382+
// AssocConst ==> assoc item has value
1383+
tcx.is_type_const(def_id)
13851384
&& (!matches!(def_kind, DefKind::AssocConst) || assoc_item_has_value(tcx, def_id))
13861385
}
13871386

compiler/rustc_mir_build/src/thir/cx/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub(crate) fn thir_body(
1818
tcx: TyCtxt<'_>,
1919
owner_def: LocalDefId,
2020
) -> Result<(&Steal<Thir<'_>>, ExprId), ErrorGuaranteed> {
21+
debug_assert!(!tcx.is_type_const(owner_def.to_def_id()), "thir_body queried for type_const");
22+
2123
let body = tcx.hir_body_owned_by(owner_def);
2224
let mut cx = ThirBuildCx::new(tcx, owner_def);
2325
if let Some(reported) = cx.typeck_results.tainted_by_errors {

compiler/rustc_passes/src/reachable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ impl<'tcx> ReachableContext<'tcx> {
209209
self.visit_nested_body(body);
210210
}
211211
}
212-
212+
// For #[type_const] we want to evaluate the RHS.
213+
hir::ItemKind::Const(_, _, _, init @ hir::ConstItemRhs::TypeConst(_)) => {
214+
self.visit_const_item_rhs(init);
215+
}
213216
hir::ItemKind::Const(_, _, _, init) => {
214217
// Only things actually ending up in the final constant value are reachable
215218
// for codegen. Everything else is only needed during const-eval, so even if
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-pass
2+
// This previously caused an ICE when checking reachability of a pub const item
3+
// This is because reachability also tried to evaluate the #[type_const] which
4+
// requires the item have a body. #[type_const] do not have bodies.
5+
#![expect(incomplete_features)]
6+
#![feature(min_generic_const_args)]
7+
8+
#[type_const]
9+
pub const TYPE_CONST : usize = 1;
10+
fn main() {}

0 commit comments

Comments
 (0)