Skip to content

Commit 0d1ac13

Browse files
author
yimiliya
committed
Use delayed bug instead of panic in mir_const_qualif for invalid const context
The issue was that `mir_const_qualif` would panic when `const_kind` returned `None`. This can happen when there are prior errors, such as attempting to declare a const fn in a trait (which is not allowed). The fix replaces the `span_bug!` with a `span_delayed_bug` and returns a default value, allowing compilation to continue and report the actual error instead of ICE-ing.
1 parent 431f257 commit 0d1ac13

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,15 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
363363
// No need to const-check a non-const `fn`.
364364
match ccx.const_kind {
365365
Some(ConstContext::Const { .. } | ConstContext::Static(_) | ConstContext::ConstFn) => {}
366-
None => span_bug!(
367-
tcx.def_span(def),
368-
"`mir_const_qualif` should only be called on const fns and const items"
369-
),
366+
None => {
367+
// This can happen when there are prior errors (e.g., const fn in trait).
368+
// Use a delayed bug instead of panicking to allow compilation to continue.
369+
tcx.dcx().span_delayed_bug(
370+
tcx.def_span(def),
371+
"`mir_const_qualif` should only be called on const fns and const items",
372+
);
373+
return Default::default();
374+
}
370375
}
371376

372377
if body.return_ty().references_error() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test for issue #153891: ICE when using const closure in trait const fn
2+
#![feature(const_closures)]
3+
4+
trait Tr {
5+
const fn test() {
6+
(const || {})()
7+
}
8+
}
9+
10+
fn main() {}

0 commit comments

Comments
 (0)