Skip to content

Commit 39936fd

Browse files
authored
Rollup merge of #99222 - atsuzaki:generic_const_err, r=lcnr
Better error message for generic_const_exprs inference failure Fixes #90531 This code: ```rs #![feature(generic_const_exprs)] fn foo<const N: usize>(_arr: [u64; N + 1]) where [u64; N + 1]: {} fn main() { let arr = [5; 5]; foo(arr); } ``` Will now emit the following error: ```rs warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes --> test.rs:1:12 | 1 | #![feature(generic_const_exprs)] | ^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(incomplete_features)]` on by default = note: see issue #76560 <#76560> for more information error[E0284]: type annotations needed --> test.rs:8:7 | 8 | foo(arr); | ^^^ cannot infer the value of the const parameter `N` declared on the function `foo` | note: required by a bound in `foo` --> test.rs:3:56 | 3 | fn foo<const N: usize>(_arr: [u64; N + 1]) where [u64; N + 1]: {} | ^^^^^ required by this bound in `foo` help: consider specifying the generic argument | 8 | foo::<N>(arr); | +++++ error: aborting due to previous error; 1 warning emitted ``` cc: `@lcnr` thanks a lot again for the help on this
2 parents d3a1aa0 + 96d34dc commit 39936fd

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,33 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21792179
}
21802180
}
21812181

2182+
ty::PredicateKind::ConstEvaluatable(data) => {
2183+
if predicate.references_error() || self.is_tainted_by_errors() {
2184+
return;
2185+
}
2186+
let subst = data.substs.iter().find(|g| g.has_infer_types_or_consts());
2187+
if let Some(subst) = subst {
2188+
let err = self.emit_inference_failure_err(
2189+
body_id,
2190+
span,
2191+
subst,
2192+
ErrorCode::E0284,
2193+
true,
2194+
);
2195+
err
2196+
} else {
2197+
// If we can't find a substitution, just print a generic error
2198+
let mut err = struct_span_err!(
2199+
self.tcx.sess,
2200+
span,
2201+
E0284,
2202+
"type annotations needed: cannot satisfy `{}`",
2203+
predicate,
2204+
);
2205+
err.span_label(span, &format!("cannot satisfy `{}`", predicate));
2206+
err
2207+
}
2208+
}
21822209
_ => {
21832210
if self.tcx.sess.has_errors().is_some() || self.is_tainted_by_errors() {
21842211
return;

src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
1616
}
1717

1818
fn main() {
19-
// FIXME(generic_const_exprs): Improve the error message here.
2019
use_dyn(&());
2120
//~^ ERROR type annotations needed
2221
}

src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
error[E0284]: type annotations needed: cannot satisfy `the constant `use_dyn::<{_: usize}>::{constant#0}` can be evaluated`
2-
--> $DIR/object-safety-ok-infer-err.rs:20:5
1+
error[E0284]: type annotations needed
2+
--> $DIR/object-safety-ok-infer-err.rs:19:5
33
|
44
LL | use_dyn(&());
5-
| ^^^^^^^ cannot satisfy `the constant `use_dyn::<{_: usize}>::{constant#0}` can be evaluated`
5+
| ^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `use_dyn`
66
|
77
note: required by a bound in `use_dyn`
88
--> $DIR/object-safety-ok-infer-err.rs:14:55
99
|
1010
LL | fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
1111
| ^^^^^ required by this bound in `use_dyn`
12+
help: consider specifying the generic argument
13+
|
14+
LL | use_dyn::<N>(&());
15+
| +++++
1216

1317
error: aborting due to previous error
1418

src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ extern crate generics_of_parent_impl_trait;
77
fn main() {
88
// check for `impl Trait<{ const }>` which has a parent of a `DefKind::TyParam`
99
generics_of_parent_impl_trait::foo([()]);
10-
//~^ error: type annotations needed:
10+
//~^ error: type annotations needed
1111
}

src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0284]: type annotations needed: cannot satisfy `the constant `foo::{opaque#0}::{constant#0}` can be evaluated`
1+
error[E0284]: type annotations needed
22
--> $DIR/parent_generics_of_encoding_impl_trait.rs:9:5
33
|
44
LL | generics_of_parent_impl_trait::foo([()]);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `the constant `foo::{opaque#0}::{constant#0}` can be evaluated`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `foo`
66
|
77
note: required by a bound in `foo`
88
--> $DIR/auxiliary/generics_of_parent_impl_trait.rs:6:48

0 commit comments

Comments
 (0)