Skip to content

Commit 71e162e

Browse files
committed
Fix diagnostics for unfulfilled obligations
1 parent d60ebe3 commit 71e162e

File tree

5 files changed

+20
-26
lines changed

5 files changed

+20
-26
lines changed

Diff for: compiler/rustc_middle/src/ty/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,14 @@ impl<'tcx> TraitPredicate<'tcx> {
829829
pub fn is_const_if_const(self) -> bool {
830830
self.constness == BoundConstness::ConstIfConst
831831
}
832+
833+
pub fn is_constness_satisfied_by(self, constness: hir::Constness) -> bool {
834+
match (self.constness, constness) {
835+
(BoundConstness::NotConst, _)
836+
| (BoundConstness::ConstIfConst, hir::Constness::Const) => true,
837+
(BoundConstness::ConstIfConst, hir::Constness::NotConst) => false,
838+
}
839+
}
832840
}
833841

834842
impl<'tcx> PolyTraitPredicate<'tcx> {

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
666666
);
667667
} else if !suggested {
668668
// Can't show anything else useful, try to find similar impls.
669-
let impl_candidates = self.find_similar_impl_candidates(trait_ref);
669+
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
670670
if !self.report_similar_impl_candidates(
671671
impl_candidates,
672672
trait_ref,
@@ -701,7 +701,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
701701
{
702702
let trait_ref = trait_pred.to_poly_trait_ref();
703703
let impl_candidates =
704-
self.find_similar_impl_candidates(trait_ref);
704+
self.find_similar_impl_candidates(trait_pred);
705705
self.report_similar_impl_candidates(
706706
impl_candidates,
707707
trait_ref,
@@ -1325,7 +1325,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
13251325

13261326
fn find_similar_impl_candidates(
13271327
&self,
1328-
trait_ref: ty::PolyTraitRef<'tcx>,
1328+
trait_pred: ty::PolyTraitPredicate<'tcx>,
13291329
) -> Vec<ImplCandidate<'tcx>>;
13301330

13311331
fn report_similar_impl_candidates(
@@ -1694,18 +1694,22 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
16941694

16951695
fn find_similar_impl_candidates(
16961696
&self,
1697-
trait_ref: ty::PolyTraitRef<'tcx>,
1697+
trait_pred: ty::PolyTraitPredicate<'tcx>,
16981698
) -> Vec<ImplCandidate<'tcx>> {
16991699
self.tcx
1700-
.all_impls(trait_ref.def_id())
1700+
.all_impls(trait_pred.def_id())
17011701
.filter_map(|def_id| {
1702-
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative {
1702+
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative
1703+
|| !trait_pred
1704+
.skip_binder()
1705+
.is_constness_satisfied_by(self.tcx.constness(def_id))
1706+
{
17031707
return None;
17041708
}
17051709

17061710
let imp = self.tcx.impl_trait_ref(def_id).unwrap();
17071711

1708-
self.fuzzy_match_tys(trait_ref.skip_binder().self_ty(), imp.self_ty(), false)
1712+
self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false)
17091713
.map(|similarity| ImplCandidate { trait_ref: imp, similarity })
17101714
})
17111715
.collect()

Diff for: src/test/ui/const-generics/issues/issue-90318.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementat
1010
|
1111
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
1212
| ^^
13-
= help: the trait `PartialEq` is implemented for `TypeId`
1413

1514
error[E0277]: can't compare `TypeId` with `_` in const contexts
1615
--> $DIR/issue-90318.rs:21:28
@@ -24,7 +23,6 @@ note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementat
2423
|
2524
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
2625
| ^^
27-
= help: the trait `PartialEq` is implemented for `TypeId`
2826

2927
error: aborting due to 2 previous errors
3028

Diff for: src/test/ui/consts/min_const_fn/cmp_fn_pointers.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ note: the trait `PartialEq<_>` is implemented for `fn()`, but that implementatio
1010
|
1111
LL | unsafe { x == y }
1212
| ^^
13-
= help: the following other types implement trait `PartialEq<Rhs>`:
14-
extern "C" fn() -> Ret
15-
extern "C" fn(A, B) -> Ret
16-
extern "C" fn(A, B, ...) -> Ret
17-
extern "C" fn(A, B, C) -> Ret
18-
extern "C" fn(A, B, C, ...) -> Ret
19-
extern "C" fn(A, B, C, D) -> Ret
20-
extern "C" fn(A, B, C, D, ...) -> Ret
21-
extern "C" fn(A, B, C, D, E) -> Ret
22-
and 68 others
2313

2414
error: aborting due to previous error
2515

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ note: the trait `PartialEq<_>` is implemented for `T`, but that implementation i
99
|
1010
LL | *t == *t
1111
| ^^
12-
= help: the following other types implement trait `PartialEq<Rhs>`:
13-
<&A as PartialEq<&B>>
14-
<&A as PartialEq<&mut B>>
15-
<&mut A as PartialEq<&B>>
16-
<&mut A as PartialEq<&mut B>>
17-
<*const T as PartialEq>
18-
<*mut T as PartialEq>
12+
= help: the trait `PartialEq<&B>` is implemented for `&A`
1913

2014
error: aborting due to previous error
2115

0 commit comments

Comments
 (0)