Skip to content

Commit

Permalink
For a single impl candidate, try to unify it with error trait ref
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 2, 2023
1 parent 5333b87 commit 8be12f4
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 34 deletions.
78 changes: 75 additions & 3 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub enum CandidateSimilarity {
pub struct ImplCandidate<'tcx> {
pub trait_ref: ty::TraitRef<'tcx>,
pub similarity: CandidateSimilarity,
impl_def_id: DefId,
}

enum GetSafeTransmuteErrorAndReason {
Expand Down Expand Up @@ -1331,6 +1332,7 @@ trait InferCtxtPrivExt<'tcx> {
body_def_id: LocalDefId,
err: &mut Diagnostic,
other: bool,
param_env: ty::ParamEnv<'tcx>,
) -> bool;

fn report_similar_impl_candidates_for_root_obligation(
Expand Down Expand Up @@ -1918,8 +1920,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {

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

self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false)
.map(|similarity| ImplCandidate { trait_ref: imp, similarity })
self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false).map(
|similarity| ImplCandidate { trait_ref: imp, similarity, impl_def_id: def_id },
)
})
.collect();
if candidates.iter().any(|c| matches!(c.similarity, CandidateSimilarity::Exact { .. })) {
Expand All @@ -1938,7 +1941,71 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
body_def_id: LocalDefId,
err: &mut Diagnostic,
other: bool,
param_env: ty::ParamEnv<'tcx>,
) -> bool {
if let [single] = &impl_candidates {
if self.probe(|_| {
let ocx = ObligationCtxt::new(self);
let obligation_trait_ref = self.instantiate_binder_with_placeholders(trait_ref);
let impl_args = self.fresh_args_for_item(DUMMY_SP, single.impl_def_id);
let impl_trait_ref = ocx.normalize(
&ObligationCause::dummy(),
param_env,
ty::EarlyBinder::bind(single.trait_ref).instantiate(self.tcx, impl_args),
);

ocx.register_obligations(
self.tcx
.predicates_of(single.impl_def_id)
.instantiate(self.tcx, impl_args)
.into_iter()
.map(|(clause, _)| {
Obligation::new(self.tcx, ObligationCause::dummy(), param_env, clause)
}),
);
if !ocx.select_where_possible().is_empty() {
return false;
}

let mut terrs = vec![];
for (obligation_arg, impl_arg) in
std::iter::zip(obligation_trait_ref.args, impl_trait_ref.args)
{
if let Err(terr) =
ocx.eq(&ObligationCause::dummy(), param_env, obligation_arg, impl_arg)
{
terrs.push(terr);
}
if !ocx.select_where_possible().is_empty() {
return false;
}
}

// Literally nothing unified, just give up.
if terrs.len() == impl_trait_ref.args.len() {
return false;
}

let cand =
self.resolve_vars_if_possible(impl_trait_ref).fold_with(&mut BottomUpFolder {
tcx: self.tcx,
ty_op: |ty| ty,
lt_op: |lt| lt,
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
});
err.highlighted_help(vec![
(format!("the trait `{}` ", cand.print_only_trait_path()), Style::NoStyle),
("is".to_string(), Style::Highlight),
(" implemented for `".to_string(), Style::NoStyle),
(cand.self_ty().to_string(), Style::Highlight),
("`".to_string(), Style::NoStyle),
]);
true
}) {
return true;
}
}

let other = if other { "other " } else { "" };
let report = |candidates: Vec<TraitRef<'tcx>>, err: &mut Diagnostic| {
if candidates.is_empty() {
Expand Down Expand Up @@ -2062,9 +2129,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
})
.collect();
impl_candidates.sort_by_key(|cand| (cand.similarity, cand.trait_ref));
let mut impl_candidates: Vec<_> =
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect();
impl_candidates.dedup();

report(impl_candidates.into_iter().map(|cand| cand.trait_ref).collect(), err)
report(impl_candidates, err)
}

fn report_similar_impl_candidates_for_root_obligation(
Expand Down Expand Up @@ -2108,6 +2177,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
body_def_id,
err,
true,
obligation.param_env,
);
}
}
Expand Down Expand Up @@ -2316,6 +2386,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
obligation.cause.body_id,
&mut err,
false,
obligation.param_env,
);
}
}
Expand Down Expand Up @@ -3051,6 +3122,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
body_def_id,
err,
true,
obligation.param_env,
) {
self.report_similar_impl_candidates_for_root_obligation(
&obligation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `A<_>: Bar<_>` is not satisfied
LL | let _ = A;
| ^ the trait `Bar<_>` is not implemented for `A<_>`
|
= help: the trait `Bar<N>` is implemented for `A<7>`
= help: the trait `Bar<_>` is implemented for `A<7>`
note: required by a bound in `A`
--> $DIR/unused-substs-1.rs:9:11
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/issues/issue-62742.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `RawImpl<_>: Raw<_>` is not satisfied
LL | WrongImpl::foo(0i32);
| ^^^^^^^^^ the trait `Raw<_>` is not implemented for `RawImpl<_>`
|
= help: the trait `Raw<[T]>` is implemented for `RawImpl<T>`
= help: the trait `Raw<[_]>` is implemented for `RawImpl<_>`
note: required by a bound in `SafeImpl`
--> $DIR/issue-62742.rs:26:35
|
Expand Down Expand Up @@ -42,7 +42,7 @@ error[E0277]: the trait bound `RawImpl<()>: Raw<()>` is not satisfied
LL | WrongImpl::<()>::foo(0i32);
| ^^^^^^^^^^^^^^^ the trait `Raw<()>` is not implemented for `RawImpl<()>`
|
= help: the trait `Raw<[T]>` is implemented for `RawImpl<T>`
= help: the trait `Raw<[()]>` is implemented for `RawImpl<()>`
note: required by a bound in `SafeImpl`
--> $DIR/issue-62742.rs:26:35
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/indexing/index-help.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x[0i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= note: required for `Vec<{integer}>` to implement `Index<i32>`

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/indexing/indexing-requires-a-uint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | [0][0u8];
| ^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `u8`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= note: required for `[{integer}]` to implement `Index<u8>`

error[E0308]: mismatched types
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/integral-indexing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | v[3u8];
| ^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[isize]>` is not implemented for `u8`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
= note: required for `Vec<isize>` to implement `Index<u8>`

error[E0277]: the type `[isize]` cannot be indexed by `i8`
Expand All @@ -15,7 +15,7 @@ LL | v[3i8];
| ^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[isize]>` is not implemented for `i8`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
= note: required for `Vec<isize>` to implement `Index<i8>`

error[E0277]: the type `[isize]` cannot be indexed by `u32`
Expand All @@ -25,7 +25,7 @@ LL | v[3u32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[isize]>` is not implemented for `u32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
= note: required for `Vec<isize>` to implement `Index<u32>`

error[E0277]: the type `[isize]` cannot be indexed by `i32`
Expand All @@ -35,7 +35,7 @@ LL | v[3i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[isize]>` is not implemented for `i32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[isize]>` is implemented for `usize`
= note: required for `Vec<isize>` to implement `Index<i32>`

error[E0277]: the type `[u8]` cannot be indexed by `u8`
Expand All @@ -45,7 +45,7 @@ LL | s.as_bytes()[3u8];
| ^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[u8]>` is not implemented for `u8`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
= note: required for `[u8]` to implement `Index<u8>`

error[E0277]: the type `[u8]` cannot be indexed by `i8`
Expand All @@ -55,7 +55,7 @@ LL | s.as_bytes()[3i8];
| ^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[u8]>` is not implemented for `i8`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
= note: required for `[u8]` to implement `Index<i8>`

error[E0277]: the type `[u8]` cannot be indexed by `u32`
Expand All @@ -65,7 +65,7 @@ LL | s.as_bytes()[3u32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[u8]>` is not implemented for `u32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
= note: required for `[u8]` to implement `Index<u32>`

error[E0277]: the type `[u8]` cannot be indexed by `i32`
Expand All @@ -75,7 +75,7 @@ LL | s.as_bytes()[3i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[u8]>` is not implemented for `i32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[u8]>` is implemented for `usize`
= note: required for `[u8]` to implement `Index<i32>`

error: aborting due to 8 previous errors
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-34334.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_rece
| ^^^^^^^ value of type `Vec<(u32, _, _)>` cannot be built from `std::iter::Iterator<Item=()>`
|
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
= help: the trait `FromIterator<(u32, _, _)>` is implemented for `Vec<(u32, _, _)>`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-34334.rs:5:43
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let x2: Vec<f64> = x1.into_iter().collect();
| ^^^^^^^ value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
|
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-66923-show-error-for-correct-call.rs:8:27
|
Expand All @@ -25,7 +25,7 @@ LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
| required by a bound introduced by this call
|
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
= help: the trait `FromIterator<f64>` is implemented for `Vec<f64>`
note: the method call chain might not have had the expected associated types
--> $DIR/issue-66923-show-error-for-correct-call.rs:12:17
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/iterators/invalid-iterator-chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | i.collect()
| ^^^^^^^ value of type `Vec<X>` cannot be built from `std::iter::Iterator<Item=&X>`
|
= help: the trait `FromIterator<&X>` is not implemented for `Vec<X>`
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
= help: the trait `FromIterator<X>` is implemented for `Vec<X>`
note: the method call chain might not have had the expected associated types
--> $DIR/invalid-iterator-chain.rs:4:26
|
Expand Down Expand Up @@ -159,7 +159,7 @@ LL | let g: Vec<i32> = f.collect();
| ^^^^^^^ value of type `Vec<i32>` cannot be built from `std::iter::Iterator<Item=()>`
|
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
= help: the trait `FromIterator<i32>` is implemented for `Vec<i32>`
note: the method call chain might not have had the expected associated types
--> $DIR/invalid-iterator-chain.rs:44:15
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/on-unimplemented/impl-substs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | Foo::<usize>::foo((1i32, 1i32, 1i32));
| required by a bound introduced by this call
|
= help: the trait `Foo<usize>` is not implemented for `(i32, i32, i32)`
= help: the trait `Foo<A>` is implemented for `(A, B, C)`
= help: the trait `Foo<i32>` is implemented for `(i32, i32, i32)`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/on-unimplemented/slice-index.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x[1i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `i32`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[i32]>` is implemented for `usize`
= note: required for `[i32]` to implement `Index<i32>`

error[E0277]: the type `[i32]` cannot be indexed by `RangeTo<i32>`
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/str/str-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let _: u8 = s[4];
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= note: required for `str` to implement `Index<{integer}>`

error[E0277]: the type `str` cannot be indexed by `{integer}`
Expand All @@ -21,7 +21,7 @@ LL | let _ = s.get(4);
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
note: required by a bound in `core::str::<impl str>::get`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand All @@ -36,7 +36,7 @@ LL | let _ = s.get_unchecked(4);
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
note: required by a bound in `core::str::<impl str>::get_unchecked`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/str/str-mut-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LL | s[1usize] = bot();
| ^^^^^^ string indices are ranges of `usize`
|
= help: the trait `SliceIndex<str>` is not implemented for `usize`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
= note: required for `str` to implement `Index<usize>`

error[E0277]: the type `str` cannot be indexed by `{integer}`
Expand All @@ -45,7 +45,7 @@ LL | s.get_mut(1);
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
note: required by a bound in `core::str::<impl str>::get_mut`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand All @@ -60,7 +60,7 @@ LL | s.get_unchecked_mut(1);
= help: the trait `SliceIndex<str>` is not implemented for `{integer}`
= note: you can use `.chars().nth()` or `.bytes().nth()`
for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[_]>` is implemented for `usize`
note: required by a bound in `core::str::<impl str>::get_unchecked_mut`
--> $SRC_DIR/core/src/str/mod.rs:LL:COL

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/issue-101623.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | Trait::do_stuff({ fun(&mut *inner) });
| | the trait `Trait<'_>` is not implemented for `*mut ()`
| required by a bound introduced by this call
|
= help: the trait `Trait<'a>` is implemented for `()`
= help: the trait `Trait<'_>` is implemented for `()`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/suggest-dereferencing-index.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let one_item_please: i32 = [1, 2, 3][i];
| ^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize`
= help: the trait `SliceIndex<[T]>` is implemented for `usize`
= help: the trait `SliceIndex<[{integer}]>` is implemented for `usize`
= note: required for `[{integer}]` to implement `Index<&usize>`
help: dereference this index
|
Expand Down
Loading

0 comments on commit 8be12f4

Please sign in to comment.