Skip to content

Commit 9d4ab9d

Browse files
committed
report_not_const_evaluatable_error to avoid ICEing on ConstKind::Expr
1 parent 8de6f99 commit 9d4ab9d

File tree

3 files changed

+99
-13
lines changed

3 files changed

+99
-13
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -3216,20 +3216,24 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32163216
}
32173217

32183218
match obligation.predicate.kind().skip_binder() {
3219-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
3220-
let ty::ConstKind::Unevaluated(uv) = ct.kind() else {
3219+
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => match ct.kind() {
3220+
ty::ConstKind::Unevaluated(uv) => {
3221+
let mut err =
3222+
self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
3223+
let const_span = self.tcx.def_span(uv.def);
3224+
match self.tcx.sess.source_map().span_to_snippet(const_span) {
3225+
Ok(snippet) => err.help(format!(
3226+
"try adding a `where` bound using this expression: `where [(); {snippet}]:`"
3227+
)),
3228+
_ => err.help("consider adding a `where` bound using this expression"),
3229+
};
3230+
Some(err)
3231+
}
3232+
ty::ConstKind::Expr(_) => None,
3233+
_ => {
32213234
bug!("const evaluatable failed for non-unevaluated const `{ct:?}`");
3222-
};
3223-
let mut err = self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
3224-
let const_span = self.tcx.def_span(uv.def);
3225-
match self.tcx.sess.source_map().span_to_snippet(const_span) {
3226-
Ok(snippet) => err.help(format!(
3227-
"try adding a `where` bound using this expression: `where [(); {snippet}]:`"
3228-
)),
3229-
_ => err.help("consider adding a `where` bound using this expression"),
3230-
};
3231-
Some(err)
3232-
}
3235+
}
3236+
},
32333237
_ => {
32343238
span_bug!(
32353239
span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
3+
4+
fn foo<const N: usize>(
5+
_: [u8; {
6+
{
7+
N
8+
}
9+
}],
10+
) {
11+
}
12+
13+
fn ice<const L: usize>()
14+
where
15+
[(); (L - 1) + 1 + L]:,
16+
{
17+
foo::<_, L>([(); L + 1 + L]);
18+
//~^ ERROR: mismatched types
19+
//~^^ ERROR: unconstrained generic constant
20+
//~^^^ ERROR: function takes 1 generic argument but 2 generic arguments were supplied
21+
//~^^^^ ERROR: unconstrained generic constant
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0107]: function takes 1 generic argument but 2 generic arguments were supplied
2+
--> $DIR/issue_114151.rs:17:5
3+
|
4+
LL | foo::<_, L>([(); L + 1 + L]);
5+
| ^^^ - help: remove this generic argument
6+
| |
7+
| expected 1 generic argument
8+
|
9+
note: function defined here, with 1 generic parameter: `N`
10+
--> $DIR/issue_114151.rs:4:4
11+
|
12+
LL | fn foo<const N: usize>(
13+
| ^^^ --------------
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/issue_114151.rs:17:18
17+
|
18+
LL | foo::<_, L>([(); L + 1 + L]);
19+
| ^^ expected `u8`, found `()`
20+
21+
error: unconstrained generic constant
22+
--> $DIR/issue_114151.rs:17:22
23+
|
24+
LL | foo::<_, L>([(); L + 1 + L]);
25+
| ^^^^^^^^^
26+
|
27+
= help: try adding a `where` bound using this expression: `where [(); L + 1 + L]:`
28+
29+
error: unconstrained generic constant
30+
--> $DIR/issue_114151.rs:17:17
31+
|
32+
LL | foo::<_, L>([(); L + 1 + L]);
33+
| ----------- ^^^^^^^^^^^^^^^
34+
| |
35+
| required by a bound introduced by this call
36+
|
37+
= help: try adding a `where` bound using this expression: `where [(); {
38+
{
39+
N
40+
}
41+
}]:`
42+
note: required by a bound in `foo`
43+
--> $DIR/issue_114151.rs:5:13
44+
|
45+
LL | fn foo<const N: usize>(
46+
| --- required by a bound in this function
47+
LL | _: [u8; {
48+
| _____________^
49+
LL | | {
50+
LL | | N
51+
LL | | }
52+
LL | | }],
53+
| |_____^ required by this bound in `foo`
54+
55+
error: aborting due to 4 previous errors
56+
57+
Some errors have detailed explanations: E0107, E0308.
58+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)