Skip to content

Commit aeb4c04

Browse files
authored
Rollup merge of #124394 - gurry:123863-ice-unexpected-region, r=lcnr
Fix ICE on invalid const param types Fixes ICE #123863 which occurs because the const param has a type which is not a `bool`, `char` or an integral type. The ICEing code path begins here in `typeck_with_fallback`: https://github.com/rust-lang/rust/blob/cb3752d20e0f5d24348062211102a08d46fbecff/compiler/rustc_hir_typeck/src/lib.rs#L167 The `fallback` invokes the `type_of` query and that eventually ends up calling `ct_infer` from the lowering code over here: https://github.com/rust-lang/rust/blob/cb3752d20e0f5d24348062211102a08d46fbecff/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs#L561 and `ct_infer` ICEs at this location: https://github.com/rust-lang/rust/blob/cb3752d20e0f5d24348062211102a08d46fbecff/compiler/rustc_hir_analysis/src/collect.rs#L392 To fix the ICE it I'm triggering a `span_delayed_bug` before we hit `ct_infer` if the type of the const param is not one of the supported types ### Edit On `@lcnr's` suggestion I've changed the approach to not let `ReStatic` region hit the `bug!` in `ct_infer` instead of triggering a `span_delayed_bug`.
2 parents 52ce43e + c62bc31 commit aeb4c04

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
386386

387387
fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
388388
let ty = self.tcx.fold_regions(ty, |r, _| match *r {
389+
rustc_type_ir::RegionKind::ReStatic => r,
390+
389391
// This is never reached in practice. If it ever is reached,
390392
// `ReErased` should be changed to `ReStatic`, and any other region
391393
// left alone.

tests/crashes/123863.rs

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fn concat_strs<const A: &'static str>() -> &'static str {
2+
//~^ ERROR &'static str` is forbidden as the type of a const generic parameter
3+
struct Inner<const A: &'static str>;
4+
//~^ ERROR &'static str` is forbidden as the type of a const generic parameter
5+
Inner::concat_strs::<"a">::A
6+
//~^ ERROR ambiguous associated type
7+
}
8+
9+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: `&'static str` is forbidden as the type of a const generic parameter
2+
--> $DIR/ice-unexpected-region-123863.rs:1:31
3+
|
4+
LL | const fn concat_strs<const A: &'static str>() -> &'static str {
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
9+
|
10+
LL + #![feature(adt_const_params)]
11+
|
12+
13+
error: `&'static str` is forbidden as the type of a const generic parameter
14+
--> $DIR/ice-unexpected-region-123863.rs:3:27
15+
|
16+
LL | struct Inner<const A: &'static str>;
17+
| ^^^^^^^^^^^^
18+
|
19+
= note: the only supported types are integers, `bool` and `char`
20+
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
21+
|
22+
LL + #![feature(adt_const_params)]
23+
|
24+
25+
error[E0223]: ambiguous associated type
26+
--> $DIR/ice-unexpected-region-123863.rs:5:5
27+
|
28+
LL | Inner::concat_strs::<"a">::A
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
help: if there were a trait named `Example` with associated type `concat_strs` implemented for `Inner<_>`, you could use the fully-qualified path
32+
|
33+
LL | <Inner<_> as Example>::concat_strs::A
34+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
36+
error: aborting due to 3 previous errors
37+
38+
For more information about this error, try `rustc --explain E0223`.

0 commit comments

Comments
 (0)