Skip to content

Commit 1a07742

Browse files
authored
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
2 parents 5290d5e + 6e21b7a commit 1a07742

13 files changed

+7
-66
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
764764
self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty);
765765
}
766766

767+
let mut unsatisfied_const = false;
767768
if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
768769
let non_const_predicate = trait_ref.without_const();
769770
let non_const_obligation = Obligation {
@@ -773,6 +774,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
773774
recursion_depth: obligation.recursion_depth,
774775
};
775776
if self.predicate_may_hold(&non_const_obligation) {
777+
unsatisfied_const = true;
776778
err.span_note(
777779
span,
778780
&format!(
@@ -924,7 +926,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
924926
}
925927
}
926928
} else if !trait_ref.has_non_region_infer()
927-
&& self.predicate_can_apply(obligation.param_env, trait_ref)
929+
&& self.predicate_can_apply(obligation.param_env, trait_predicate)
928930
{
929931
// If a where-clause may be useful, remind the
930932
// user that they can add it.
@@ -939,7 +941,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
939941
None,
940942
obligation.cause.body_id,
941943
);
942-
} else if !suggested {
944+
} else if !suggested && !unsatisfied_const {
943945
// Can't show anything else useful, try to find similar impls.
944946
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
945947
if !self.report_similar_impl_candidates(
@@ -1436,7 +1438,7 @@ trait InferCtxtPrivExt<'tcx> {
14361438
fn predicate_can_apply(
14371439
&self,
14381440
param_env: ty::ParamEnv<'tcx>,
1439-
pred: ty::PolyTraitRef<'tcx>,
1441+
pred: ty::PolyTraitPredicate<'tcx>,
14401442
) -> bool;
14411443

14421444
fn note_obligation_cause(&self, err: &mut Diagnostic, obligation: &PredicateObligation<'tcx>);
@@ -2511,7 +2513,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25112513
fn predicate_can_apply(
25122514
&self,
25132515
param_env: ty::ParamEnv<'tcx>,
2514-
pred: ty::PolyTraitRef<'tcx>,
2516+
pred: ty::PolyTraitPredicate<'tcx>,
25152517
) -> bool {
25162518
struct ParamToVarFolder<'a, 'tcx> {
25172519
infcx: &'a InferCtxt<'tcx>,
@@ -2555,7 +2557,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25552557
let obligation = Obligation::new(
25562558
ObligationCause::dummy(),
25572559
param_env,
2558-
cleaned_pred.without_const().to_predicate(selcx.tcx()),
2560+
cleaned_pred.to_predicate(selcx.tcx()),
25592561
);
25602562

25612563
self.predicate_may_hold(&obligation)

src/test/ui/consts/const-eval/const_raw_ptr_ops.stderr

-20
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ note: the trait `PartialEq<_>` is implemented for `*const i32`, but that impleme
1010
|
1111
LL | const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 };
1212
| ^^
13-
= help: the following other types implement trait `PartialEq<Rhs>`:
14-
f32
15-
f64
16-
i128
17-
i16
18-
i32
19-
i64
20-
i8
21-
isize
22-
and 6 others
2313

2414
error[E0277]: can't compare `*const i32` with `_` in const contexts
2515
--> $DIR/const_raw_ptr_ops.rs:6:44
@@ -33,16 +23,6 @@ note: the trait `PartialEq<_>` is implemented for `*const i32`, but that impleme
3323
|
3424
LL | const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 };
3525
| ^^
36-
= help: the following other types implement trait `PartialEq<Rhs>`:
37-
f32
38-
f64
39-
i128
40-
i16
41-
i32
42-
i64
43-
i8
44-
isize
45-
and 6 others
4626

4727
error: aborting due to 2 previous errors
4828

src/test/ui/consts/issue-25826.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ note: the trait `PartialOrd` is implemented for `*const ()`, but that implementa
1010
|
1111
LL | const A: bool = unsafe { id::<u8> as *const () < id::<u16> as *const () };
1212
| ^
13-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
14-
|
15-
LL | fn main() where *const (): ~const PartialOrd {
16-
| ++++++++++++++++++++++++++++++++++
1713

1814
error: aborting due to previous error
1915

src/test/ui/consts/issue-94675.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ note: the trait `IndexMut<usize>` is implemented for `Vec<usize>`, but that impl
2323
|
2424
LL | self.bar[0] = baz.len();
2525
| ^^^^^^^^^^^
26-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
27-
|
28-
LL | impl<'a> Foo<'a> where Vec<usize>: ~const IndexMut<usize> {
29-
| ++++++++++++++++++++++++++++++++++++++++
3026

3127
error: aborting due to 2 previous errors
3228

src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ note: required by a bound in `Foo::Bar`
1515
|
1616
LL | type Bar: ~const std::ops::Add;
1717
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `Foo::Bar`
18-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
19-
|
20-
LL | impl const Foo for NonConstAdd where NonConstAdd: ~const Add {
21-
| +++++++++++++++++++++++++++++
2218

2319
error: aborting due to previous error
2420

src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ note: the trait `Plus` is implemented for `u32`, but that implementation is not
99
|
1010
LL | a.plus(b)
1111
| ^^^^
12-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
13-
|
14-
LL | pub const fn add_u32(a: u32, b: u32) -> u32 where u32: ~const Plus {
15-
| ++++++++++++++++++++++
1612

1713
error: aborting due to previous error
1814

src/test/ui/rfc-2632-const-trait-impl/call-generic-method-fail.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ note: the trait `PartialEq<_>` is implemented for `T`, but that implementation i
99
|
1010
LL | *t == *t
1111
| ^^
12-
= help: the trait `PartialEq<&B>` is implemented for `&A`
1312

1413
error: aborting due to previous error
1514

src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: the trait `ConstDefaultFn` is implemented for `NonConstImpl`, but that imp
1111
|
1212
LL | NonConstImpl.a();
1313
| ^^^^^^^^^^^^
14-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
15-
|
16-
LL | const fn test() where NonConstImpl: ~const ConstDefaultFn {
17-
| +++++++++++++++++++++++++++++++++++++++++
1814

1915
error: aborting due to previous error
2016

src/test/ui/rfc-2632-const-trait-impl/cross-crate.gatednc.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: the trait `cross_crate::MyTrait` is implemented for `cross_crate::NonConst
1111
|
1212
LL | NonConst.func();
1313
| ^^^^^^^^
14-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
15-
|
16-
LL | const fn const_context() where cross_crate::NonConst: ~const cross_crate::MyTrait {
17-
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1814

1915
error: aborting due to previous error
2016

src/test/ui/rfc-2632-const-trait-impl/cross-crate.stocknc.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: the trait `cross_crate::MyTrait` is implemented for `cross_crate::NonConst
1111
|
1212
LL | NonConst.func();
1313
| ^^^^^^^^
14-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
15-
|
16-
LL | const fn const_context() where cross_crate::NonConst: ~const cross_crate::MyTrait {
17-
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1814

1915
error: aborting due to previous error
2016

src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-body-checking.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ note: required by a bound in `foo`
1414
|
1515
LL | const fn foo<T>() where T: ~const Tr {}
1616
| ^^^^^^^^^ required by this bound in `foo`
17-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
18-
|
19-
LL | pub trait Foo where (): ~const Tr {
20-
| +++++++++++++++++++
2117

2218
error: aborting due to previous error
2319

src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: the trait `Tr` is implemented for `()`, but that implementation is not `co
1111
|
1212
LL | ().a()
1313
| ^^
14-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
15-
|
16-
LL | pub trait Tr where (): ~const Tr {
17-
| +++++++++++++++++++
1814

1915
error: aborting due to previous error
2016

src/test/ui/rfc-2632-const-trait-impl/super-traits-fail.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ note: required by a bound in `Bar`
1414
|
1515
LL | trait Bar: ~const Foo {}
1616
| ^^^^^^^^^^ required by this bound in `Bar`
17-
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
18-
|
19-
LL | impl const Bar for S where S: ~const Foo {}
20-
| +++++++++++++++++++
2117

2218
error: aborting due to previous error
2319

0 commit comments

Comments
 (0)