From 6e21b7a9b7e5e63cbc744614fafeb812643ece65 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 20 Oct 2022 23:24:45 +0000 Subject: [PATCH] Do not suggest trivially false const predicates --- .../src/traits/error_reporting/mod.rs | 12 ++++++----- .../const-eval/const_raw_ptr_ops.stderr | 20 ------------------- src/test/ui/consts/issue-25826.stderr | 4 ---- src/test/ui/consts/issue-94675.stderr | 4 ---- .../assoc-type.stderr | 4 ---- .../call-const-trait-method-fail.stderr | 4 ---- .../call-generic-method-fail.stderr | 1 - .../const-default-method-bodies.stderr | 4 ---- .../cross-crate.gatednc.stderr | 4 ---- .../cross-crate.stocknc.stderr | 4 ---- ...-method-body-is-const-body-checking.stderr | 4 ---- ...-method-body-is-const-same-trait-ck.stderr | 4 ---- .../super-traits-fail.stderr | 4 ---- 13 files changed, 7 insertions(+), 66 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 b7e6a564f3953..c850b30259ed3 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -764,6 +764,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty); } + let mut unsatisfied_const = false; if trait_predicate.is_const_if_const() && obligation.param_env.is_const() { let non_const_predicate = trait_ref.without_const(); let non_const_obligation = Obligation { @@ -773,6 +774,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { recursion_depth: obligation.recursion_depth, }; if self.predicate_may_hold(&non_const_obligation) { + unsatisfied_const = true; err.span_note( span, &format!( @@ -924,7 +926,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } } } else if !trait_ref.has_non_region_infer() - && self.predicate_can_apply(obligation.param_env, trait_ref) + && self.predicate_can_apply(obligation.param_env, trait_predicate) { // If a where-clause may be useful, remind the // user that they can add it. @@ -939,7 +941,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { None, obligation.cause.body_id, ); - } else if !suggested { + } else if !suggested && !unsatisfied_const { // Can't show anything else useful, try to find similar impls. let impl_candidates = self.find_similar_impl_candidates(trait_predicate); if !self.report_similar_impl_candidates( @@ -1433,7 +1435,7 @@ trait InferCtxtPrivExt<'tcx> { fn predicate_can_apply( &self, param_env: ty::ParamEnv<'tcx>, - pred: ty::PolyTraitRef<'tcx>, + pred: ty::PolyTraitPredicate<'tcx>, ) -> bool; fn note_obligation_cause(&self, err: &mut Diagnostic, obligation: &PredicateObligation<'tcx>); @@ -2508,7 +2510,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { fn predicate_can_apply( &self, param_env: ty::ParamEnv<'tcx>, - pred: ty::PolyTraitRef<'tcx>, + pred: ty::PolyTraitPredicate<'tcx>, ) -> bool { struct ParamToVarFolder<'a, 'tcx> { infcx: &'a InferCtxt<'tcx>, @@ -2552,7 +2554,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let obligation = Obligation::new( ObligationCause::dummy(), param_env, - cleaned_pred.without_const().to_predicate(selcx.tcx()), + cleaned_pred.to_predicate(selcx.tcx()), ); self.predicate_may_hold(&obligation) diff --git a/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr b/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr index 168fa0ad0f0ed..12244450e7f8f 100644 --- a/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr +++ b/src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr @@ -10,16 +10,6 @@ note: the trait `PartialEq<_>` is implemented for `*const i32`, but that impleme | LL | const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 }; | ^^ - = help: the following other types implement trait `PartialEq`: - f32 - f64 - i128 - i16 - i32 - i64 - i8 - isize - and 6 others error[E0277]: can't compare `*const i32` with `_` in const contexts --> $DIR/const_raw_ptr_ops.rs:6:44 @@ -33,16 +23,6 @@ note: the trait `PartialEq<_>` is implemented for `*const i32`, but that impleme | LL | const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 }; | ^^ - = help: the following other types implement trait `PartialEq`: - f32 - f64 - i128 - i16 - i32 - i64 - i8 - isize - and 6 others error: aborting due to 2 previous errors diff --git a/src/test/ui/consts/issue-25826.stderr b/src/test/ui/consts/issue-25826.stderr index b80befa26f6e0..905c5ee6eb4a0 100644 --- a/src/test/ui/consts/issue-25826.stderr +++ b/src/test/ui/consts/issue-25826.stderr @@ -10,10 +10,6 @@ note: the trait `PartialOrd` is implemented for `*const ()`, but that implementa | LL | const A: bool = unsafe { id:: as *const () < id:: as *const () }; | ^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | fn main() where *const (): ~const PartialOrd { - | ++++++++++++++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/consts/issue-94675.stderr b/src/test/ui/consts/issue-94675.stderr index 7ae293ffbf85d..f4683f7f5361d 100644 --- a/src/test/ui/consts/issue-94675.stderr +++ b/src/test/ui/consts/issue-94675.stderr @@ -23,10 +23,6 @@ note: the trait `IndexMut` is implemented for `Vec`, but that impl | LL | self.bar[0] = baz.len(); | ^^^^^^^^^^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | impl<'a> Foo<'a> where Vec: ~const IndexMut { - | ++++++++++++++++++++++++++++++++++++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr index ce2425010ad28..89177b0f1ac1c 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr @@ -15,10 +15,6 @@ note: required by a bound in `Foo::Bar` | LL | type Bar: ~const std::ops::Add; | ^^^^^^^^^^^^^^^^^^^^ required by this bound in `Foo::Bar` -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | impl const Foo for NonConstAdd where NonConstAdd: ~const Add { - | +++++++++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr b/src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr index d9ad80431533b..7350909ba8e19 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr @@ -9,10 +9,6 @@ note: the trait `Plus` is implemented for `u32`, but that implementation is not | LL | a.plus(b) | ^^^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | pub const fn add_u32(a: u32, b: u32) -> u32 where u32: ~const Plus { - | ++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.stderr b/src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.stderr index 83d395dda196a..31e6dbdab2258 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.stderr @@ -9,7 +9,6 @@ note: the trait `PartialEq<_>` is implemented for `T`, but that implementation i | LL | *t == *t | ^^ - = help: the trait `PartialEq<&B>` is implemented for `&A` error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr index fddc8d37f2ff7..c64930db9bee2 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr @@ -11,10 +11,6 @@ note: the trait `ConstDefaultFn` is implemented for `NonConstImpl`, but that imp | LL | NonConstImpl.a(); | ^^^^^^^^^^^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | const fn test() where NonConstImpl: ~const ConstDefaultFn { - | +++++++++++++++++++++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate.gatednc.stderr b/src/test/ui/rfc-2632-const-trait-impl/cross-crate.gatednc.stderr index d4fa44b4bfcb5..925ae53e32499 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/cross-crate.gatednc.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate.gatednc.stderr @@ -11,10 +11,6 @@ note: the trait `cross_crate::MyTrait` is implemented for `cross_crate::NonConst | LL | NonConst.func(); | ^^^^^^^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | const fn const_context() where cross_crate::NonConst: ~const cross_crate::MyTrait { - | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate.stocknc.stderr b/src/test/ui/rfc-2632-const-trait-impl/cross-crate.stocknc.stderr index 71ecd9b06945f..11db0c2b8f290 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/cross-crate.stocknc.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate.stocknc.stderr @@ -11,10 +11,6 @@ note: the trait `cross_crate::MyTrait` is implemented for `cross_crate::NonConst | LL | NonConst.func(); | ^^^^^^^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | const fn const_context() where cross_crate::NonConst: ~const cross_crate::MyTrait { - | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-body-checking.stderr b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-body-checking.stderr index d102956cd2ec2..a244ab10cab6b 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-body-checking.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-body-checking.stderr @@ -14,10 +14,6 @@ note: required by a bound in `foo` | LL | const fn foo() where T: ~const Tr {} | ^^^^^^^^^ required by this bound in `foo` -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | pub trait Foo where (): ~const Tr { - | +++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr index 85285ba849777..c2c16921c2eb2 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr @@ -11,10 +11,6 @@ note: the trait `Tr` is implemented for `()`, but that implementation is not `co | LL | ().a() | ^^ -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | pub trait Tr where (): ~const Tr { - | +++++++++++++++++++ error: aborting due to previous error diff --git a/src/test/ui/rfc-2632-const-trait-impl/super-traits-fail.stderr b/src/test/ui/rfc-2632-const-trait-impl/super-traits-fail.stderr index 9787792ab1318..1f8f312df0128 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/super-traits-fail.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/super-traits-fail.stderr @@ -14,10 +14,6 @@ note: required by a bound in `Bar` | LL | trait Bar: ~const Foo {} | ^^^^^^^^^^ required by this bound in `Bar` -help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement - | -LL | impl const Bar for S where S: ~const Foo {} - | +++++++++++++++++++ error: aborting due to previous error