Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2326,18 +2326,71 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
{
return false;
}
let mut failing_obligations = Vec::new();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd like to move the declaration of multi_span here and move failing_obligations into the arm of the bellow match.

Then we just need to modify the multi_span in the arm, and don't need to make failing_obligations mutable. We can write

let failing_obligations = if .. { self.probe(..) } else { vec![] };

for error in &failing_obligations { ... }

let (desc, mention_castable) =
match (cand.self_ty().kind(), trait_pred.self_ty().skip_binder().kind()) {
(ty::FnPtr(..), ty::FnDef(..)) => {
(" implemented for fn pointer `", ", cast using `as`")
}
(ty::FnPtr(..), _) => (" implemented for fn pointer `", ""),
_ => (" implemented for `", ""),
_ => {
if !self.tcx.predicates_of(def_id).predicates.is_empty() {
failing_obligations = self.probe(|_| {
let ocx = ObligationCtxt::new_with_diagnostics(self);
self.enter_forall(trait_pred, |obligation_trait_ref| {
let impl_args = self.fresh_args_for_item(DUMMY_SP, def_id);
let impl_trait_ref = ocx.normalize(
&ObligationCause::dummy(),
param_env,
ty::EarlyBinder::bind(self.tcx, cand)
.instantiate(self.tcx, impl_args),
);
if ocx
.eq(
&ObligationCause::dummy(),
param_env,
obligation_trait_ref.trait_ref,
impl_trait_ref,
)
.is_err()
{
return Vec::new();
}
ocx.register_obligations(
self.tcx
.predicates_of(def_id)
.instantiate(self.tcx, impl_args)
.into_iter()
.map(|(clause, span)| {
Obligation::new(
self.tcx,
ObligationCause::dummy_with_span(span),
param_env,
clause.skip_norm_wip(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clause.skip_norm_wip(),
clause.skip_normalization(),

)
}),
);
ocx.try_evaluate_obligations()
})
});
}

if !failing_obligations.is_empty() {
(" conditionally implemented for `", "")
} else {
(" implemented for `", "")
}
}
};
let trait_ = self.tcx.short_string(cand.print_trait_sugared(), err.long_ty_path());
let self_ty = self.tcx.short_string(cand.self_ty(), err.long_ty_path());
let mut multi_span = MultiSpan::from_span(self.tcx.def_span(def_id));
for error in failing_obligations {
multi_span
.push_span_label(error.obligation.cause.span, "unsatisfied trait bound");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.push_span_label(error.obligation.cause.span, "unsatisfied trait bound");
.push_span_label(error.root_obligation.cause.span, "unsatisfied trait bound");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think we can print root_obligation.predicate here, because this span will only point to the bound self.

}
err.highlighted_span_help(
self.tcx.def_span(def_id),
multi_span,
vec![
StringPart::normal(format!("the trait `{trait_}` ")),
StringPart::highlighted("is"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ LL | println!("{:?}", S(X));
| required by this formatting parameter
|
= help: the trait `Debug` is not implemented for `X`
help: the trait `Debug` is implemented for `S<T>`
help: the trait `Debug` is conditionally implemented for `S<T>`
--> $DIR/redundant-derive-note-on-unimplemented.rs:8:10
|
LL | #[derive(Debug)]
| ^^^^^
LL | struct S<T>(T);
| - unsatisfied trait bound
note: required for `S<X>` to implement `Debug`
--> $DIR/redundant-derive-note-on-unimplemented.rs:9:8
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ LL | want(Some(()));
| required by a bound introduced by this call
|
= help: the trait `Iterator` is not implemented for `()`
help: the trait `T1` is implemented for `Option<It>`
help: the trait `T1` is conditionally implemented for `Option<It>`
--> $DIR/blame-trait-error.rs:21:1
|
LL | impl<It: Iterator> T1 for Option<It> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^--------^^^^^^^^^^^^^^^^^^^
| |
| unsatisfied trait bound
note: required for `Option<()>` to implement `T1`
--> $DIR/blame-trait-error.rs:21:20
|
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/impl-trait/nested_impl_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| |
| the trait `From<impl Into<u32>>` is not implemented for `impl Debug`
|
help: the trait `Into<U>` is implemented for `T`
help: the trait `Into<U>` is conditionally implemented for `T`
--> $SRC_DIR/core/src/convert/mod.rs:LL:COL
::: $SRC_DIR/core/src/convert/mod.rs:LL:COL
|
= note: unsatisfied trait bound
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`

error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
Expand All @@ -62,8 +65,11 @@ LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| |
| the trait `From<impl Into<u32>>` is not implemented for `impl Debug`
|
help: the trait `Into<U>` is implemented for `T`
help: the trait `Into<U>` is conditionally implemented for `T`
--> $SRC_DIR/core/src/convert/mod.rs:LL:COL
::: $SRC_DIR/core/src/convert/mod.rs:LL:COL
|
= note: unsatisfied trait bound
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`

error: aborting due to 7 previous errors
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/copy-bounds-impl-type-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ error[E0277]: the trait bound `String: Copy` is not satisfied
LL | let a = t as Box<dyn Gettable<String>>;
| ^ the trait `Copy` is not implemented for `String`
|
help: the trait `Gettable<T>` is implemented for `S<T>`
help: the trait `Gettable<T>` is conditionally implemented for `S<T>`
--> $DIR/copy-bounds-impl-type-params.rs:14:1
|
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
Expand All @@ -100,7 +100,7 @@ error[E0277]: the trait bound `Foo: Copy` is not satisfied
LL | let a: Box<dyn Gettable<Foo>> = t;
| ^ the trait `Copy` is not implemented for `Foo`
|
help: the trait `Gettable<T>` is implemented for `S<T>`
help: the trait `Gettable<T>` is conditionally implemented for `S<T>`
--> $DIR/copy-bounds-impl-type-params.rs:14:1
|
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct Test(i32, i64);
trait Foo<'a> {
type Assoc;
}

trait SimpleFoo {
type SimpleAssoc;
}
impl<'a, T> Foo<'a> for T where T: SimpleFoo {
type Assoc = T::SimpleAssoc;
}

impl SimpleFoo for i32 {
type SimpleAssoc = i32;
}
impl SimpleFoo for i64 {
type SimpleAssoc = i32;
}

impl<'a> Foo<'a> for Test where i32: Foo<'a, Assoc = i32>, i64: Foo<'a, Assoc = i64> {
type Assoc = Test;
}

fn process<'a, T: Foo<'a>>(_input: T) {}
fn test() { process(Test(0, 1)) }
//~^ ERROR the trait bound `Test: Foo<'_>` is not satisfied

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0277]: the trait bound `Test: Foo<'_>` is not satisfied
--> $DIR/conditionally-implemented-trait-158423.rs:25:21
|
LL | fn test() { process(Test(0, 1)) }
| ------- ^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
help: the trait `Foo<'_>` is not implemented for `Test`
--> $DIR/conditionally-implemented-trait-158423.rs:1:1
|
LL | struct Test(i32, i64);
| ^^^^^^^^^^^
help: the trait `Foo<'_>` is conditionally implemented for `Test`
--> $DIR/conditionally-implemented-trait-158423.rs:20:1
|
LL | impl<'a> Foo<'a> for Test where i32: Foo<'a, Assoc = i32>, i64: Foo<'a, Assoc = i64> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^
| |
| unsatisfied trait bound
note: required by a bound in `process`
--> $DIR/conditionally-implemented-trait-158423.rs:24:19
|
LL | fn process<'a, T: Foo<'a>>(_input: T) {}
| ^^^^^^^ required by this bound in `process`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ help: the trait `Trait<_, _, _>` is not implemented for `A<X>`
|
LL | struct A<T>(*const T);
| ^^^^^^^^^^^
help: the trait `Trait<U, V, D>` is implemented for `A<T>`
help: the trait `Trait<U, V, D>` is conditionally implemented for `A<T>`
--> $DIR/incompleteness-unstable-result.rs:34:1
|
LL | / impl<T, U, V, D> Trait<U, V, D> for A<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ help: the trait `Trait<_, _, _>` is not implemented for `A<X>`
|
LL | struct A<T>(*const T);
| ^^^^^^^^^^^
help: the trait `Trait<U, V, D>` is implemented for `A<T>`
help: the trait `Trait<U, V, D>` is conditionally implemented for `A<T>`
--> $DIR/incompleteness-unstable-result.rs:34:1
|
LL | / impl<T, U, V, D> Trait<U, V, D> for A<T>
Expand Down
Loading