Rollup merge of #103328 - compiler-errors:trivial-false-const-sugg, r=jackh726

Do not suggest trivially false const predicates

Pass through constness to `predicate_can_apply` and don't suggest other impls if it's satisfied but not const.

Fixes #103267
This commit is contained in:
Matthias Krüger 2022-10-23 08:14:30 +02:00 committed by GitHub
commit 1a077420f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 7 additions and 66 deletions

View File

@ -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(
@ -1436,7 +1438,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>);
@ -2511,7 +2513,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>,
@ -2555,7 +2557,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)

View File

@ -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<Rhs>`:
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<Rhs>`:
f32
f64
i128
i16
i32
i64
i8
isize
and 6 others
error: aborting due to 2 previous errors

View File

@ -10,10 +10,6 @@ note: the trait `PartialOrd` is implemented for `*const ()`, but that implementa
|
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> 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

View File

@ -23,10 +23,6 @@ note: the trait `IndexMut<usize>` is implemented for `Vec<usize>`, 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<usize>: ~const IndexMut<usize> {
| ++++++++++++++++++++++++++++++++++++++++
error: aborting due to 2 previous errors

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -14,10 +14,6 @@ note: required by a bound in `foo`
|
LL | const fn foo<T>() 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

View File

@ -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

View File

@ -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