From 82ab171673d10f16c67a52d50ef1f5b197656bbd Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Wed, 13 Jul 2022 09:42:25 -0700 Subject: [PATCH 1/4] Use emit_inference_failure_err for ConstEvaluatable predicates --- .../src/traits/error_reporting/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index aa1c91362891b..8396df4a243c9 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2156,6 +2156,22 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { } } + ty::PredicateKind::ConstEvaluatable(data) => { + let subst = data.substs.iter().find(|g| g.has_infer_types_or_consts()); + if let Some(subst) = subst { + let mut err = self.emit_inference_failure_err( + body_id, + span, + subst, + ErrorCode::E0284, + true, + ); + err.note(&format!("cannot satisfy `{}`", predicate)); + err + } else { + todo!(); + } + } _ => { if self.tcx.sess.has_errors().is_some() || self.is_tainted_by_errors() { return; From b33955a0ef3b38f2c9448b1dcab82c31b097438e Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Wed, 13 Jul 2022 10:25:58 -0700 Subject: [PATCH 2/4] Add checks & fallback branch --- .../src/traits/error_reporting/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 8396df4a243c9..925e80128196f 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2157,6 +2157,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { } ty::PredicateKind::ConstEvaluatable(data) => { + if predicate.references_error() || self.is_tainted_by_errors() { + return; + } let subst = data.substs.iter().find(|g| g.has_infer_types_or_consts()); if let Some(subst) = subst { let mut err = self.emit_inference_failure_err( @@ -2169,7 +2172,16 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { err.note(&format!("cannot satisfy `{}`", predicate)); err } else { - todo!(); + // If we can't find a substitution, just print a generic error + let mut err = struct_span_err!( + self.tcx.sess, + span, + E0284, + "type annotations needed: cannot satisfy `{}`", + predicate, + ); + err.span_label(span, &format!("cannot satisfy `{}`", predicate)); + err } } _ => { From 083bd7cb1ddf02820caf6cd3e04330458d0624b8 Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Wed, 13 Jul 2022 10:27:19 -0700 Subject: [PATCH 3/4] Remove predicate note --- .../rustc_trait_selection/src/traits/error_reporting/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 925e80128196f..5f8560902f5e9 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2162,14 +2162,13 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> { } let subst = data.substs.iter().find(|g| g.has_infer_types_or_consts()); if let Some(subst) = subst { - let mut err = self.emit_inference_failure_err( + let err = self.emit_inference_failure_err( body_id, span, subst, ErrorCode::E0284, true, ); - err.note(&format!("cannot satisfy `{}`", predicate)); err } else { // If we can't find a substitution, just print a generic error From 96d34dc9ff557c189fef19cb00b36f5cd5c7ac49 Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Wed, 13 Jul 2022 17:28:11 -0700 Subject: [PATCH 4/4] Update tests --- .../generic_const_exprs/object-safety-ok-infer-err.rs | 1 - .../object-safety-ok-infer-err.stderr | 10 +++++++--- .../parent_generics_of_encoding_impl_trait.rs | 2 +- .../parent_generics_of_encoding_impl_trait.stderr | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.rs b/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.rs index c6c196db6f2a6..79e9834b54ed2 100644 --- a/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.rs +++ b/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.rs @@ -16,7 +16,6 @@ fn use_dyn(v: &dyn Foo) where [u8; N + 1]: Sized { } fn main() { - // FIXME(generic_const_exprs): Improve the error message here. use_dyn(&()); //~^ ERROR type annotations needed } diff --git a/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.stderr b/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.stderr index ce75314ada769..59e9fee1eaf41 100644 --- a/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.stderr +++ b/src/test/ui/const-generics/generic_const_exprs/object-safety-ok-infer-err.stderr @@ -1,14 +1,18 @@ -error[E0284]: type annotations needed: cannot satisfy `the constant `use_dyn::<{_: usize}>::{constant#0}` can be evaluated` - --> $DIR/object-safety-ok-infer-err.rs:20:5 +error[E0284]: type annotations needed + --> $DIR/object-safety-ok-infer-err.rs:19:5 | LL | use_dyn(&()); - | ^^^^^^^ cannot satisfy `the constant `use_dyn::<{_: usize}>::{constant#0}` can be evaluated` + | ^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `use_dyn` | note: required by a bound in `use_dyn` --> $DIR/object-safety-ok-infer-err.rs:14:55 | LL | fn use_dyn(v: &dyn Foo) where [u8; N + 1]: Sized { | ^^^^^ required by this bound in `use_dyn` +help: consider specifying the generic argument + | +LL | use_dyn::(&()); + | +++++ error: aborting due to previous error diff --git a/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.rs b/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.rs index ed81c01bb1726..7a78e0f109ca7 100644 --- a/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.rs +++ b/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.rs @@ -7,5 +7,5 @@ extern crate generics_of_parent_impl_trait; fn main() { // check for `impl Trait<{ const }>` which has a parent of a `DefKind::TyParam` generics_of_parent_impl_trait::foo([()]); - //~^ error: type annotations needed: + //~^ error: type annotations needed } diff --git a/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.stderr b/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.stderr index 99085711513d8..87ff7babe71c0 100644 --- a/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.stderr +++ b/src/test/ui/const-generics/parent_generics_of_encoding_impl_trait.stderr @@ -1,8 +1,8 @@ -error[E0284]: type annotations needed: cannot satisfy `the constant `foo::{opaque#0}::{constant#0}` can be evaluated` +error[E0284]: type annotations needed --> $DIR/parent_generics_of_encoding_impl_trait.rs:9:5 | LL | generics_of_parent_impl_trait::foo([()]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `the constant `foo::{opaque#0}::{constant#0}` can be evaluated` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `foo` | note: required by a bound in `foo` --> $DIR/auxiliary/generics_of_parent_impl_trait.rs:6:48