Skip to content

Commit 4a7da1a

Browse files
committed
report_not_const_evaluatable_error to avoid ICEing on ConstKind::Expr
1 parent 27794f9 commit 4a7da1a

File tree

3 files changed

+113
-13
lines changed

3 files changed

+113
-13
lines changed

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

+24-13
Original file line numberDiff line numberDiff line change
@@ -3252,20 +3252,31 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32523252
}
32533253

32543254
match obligation.predicate.kind().skip_binder() {
3255-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
3256-
let ty::ConstKind::Unevaluated(uv) = ct.kind() else {
3255+
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => match ct.kind() {
3256+
ty::ConstKind::Unevaluated(uv) => {
3257+
let mut err =
3258+
self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
3259+
let const_span = self.tcx.def_span(uv.def);
3260+
match self.tcx.sess.source_map().span_to_snippet(const_span) {
3261+
Ok(snippet) => err.help(format!(
3262+
"try adding a `where` bound using this expression: `where [(); {snippet}]:`"
3263+
)),
3264+
_ => err.help("consider adding a `where` bound using this expression"),
3265+
};
3266+
Some(err)
3267+
}
3268+
ty::ConstKind::Expr(_) => {
3269+
let mut err = self
3270+
.tcx
3271+
.sess
3272+
.struct_span_err(span, "evaluation of ConstKind::Expr is not supported");
3273+
err.emit();
3274+
None
3275+
}
3276+
_ => {
32573277
bug!("const evaluatable failed for non-unevaluated const `{ct:?}`");
3258-
};
3259-
let mut err = self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
3260-
let const_span = self.tcx.def_span(uv.def);
3261-
match self.tcx.sess.source_map().span_to_snippet(const_span) {
3262-
Ok(snippet) => err.help(format!(
3263-
"try adding a `where` bound using this expression: `where [(); {snippet}]:`"
3264-
)),
3265-
_ => err.help("consider adding a `where` bound using this expression"),
3266-
};
3267-
Some(err)
3268-
}
3278+
}
3279+
},
32693280
_ => {
32703281
span_bug!(
32713282
span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
//~^^^^^ ERROR: evaluation of ConstKind::Expr is not supported
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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: evaluation of ConstKind::Expr is not supported
56+
--> $DIR/issue_114151.rs:17:5
57+
|
58+
LL | foo::<_, L>([(); L + 1 + L]);
59+
| ^^^^^^^^^^^
60+
61+
error: aborting due to 5 previous errors
62+
63+
Some errors have detailed explanations: E0107, E0308.
64+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)