Skip to content

Commit 19728a9

Browse files
committed
Provide more context on derived obligation error primary label
Expand the primary span of E0277 when the immediate unmet bound is not what the user wrote: ``` error[E0277]: the trait bound `i32: Bar` is not satisfied --> f100.rs:6:6 | 6 | <i32 as Foo>::foo(); | ^^^ the trait `Bar` is not implemented for `i32`, which is required by `i32: Foo` | help: this trait has no implementations, consider adding one --> f100.rs:2:1 | 2 | trait Bar {} | ^^^^^^^^^ note: required for `i32` to implement `Foo` --> f100.rs:3:14 | 3 | impl<T: Bar> Foo for T {} | --- ^^^ ^ | | | unsatisfied trait bound introduced here ``` Fix #40120.
1 parent 6351247 commit 19728a9

File tree

196 files changed

+382
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+382
-373
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,20 +1432,18 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
14321432
) -> bool {
14331433
let span = obligation.cause.span;
14341434

1435-
let code = if let ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } =
1436-
obligation.cause.code()
1437-
{
1438-
parent_code
1439-
} else if let ObligationCauseCode::ItemObligation(_)
1440-
| ObligationCauseCode::ExprItemObligation(..) = obligation.cause.code()
1441-
{
1442-
obligation.cause.code()
1443-
} else if let ExpnKind::Desugaring(DesugaringKind::ForLoop) =
1444-
span.ctxt().outer_expn_data().kind
1445-
{
1446-
obligation.cause.code()
1447-
} else {
1448-
return false;
1435+
let code = match obligation.cause.code() {
1436+
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => parent_code,
1437+
c @ ObligationCauseCode::ItemObligation(_)
1438+
| c @ ObligationCauseCode::ExprItemObligation(..) => c,
1439+
c if matches!(
1440+
span.ctxt().outer_expn_data().kind,
1441+
ExpnKind::Desugaring(DesugaringKind::ForLoop)
1442+
) =>
1443+
{
1444+
c
1445+
}
1446+
_ => return false,
14491447
};
14501448

14511449
// List of traits for which it would be nonsensical to suggest borrowing.
@@ -4978,16 +4976,27 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
49784976
_ => None,
49794977
};
49804978

4979+
let pred = obligation.predicate;
4980+
let (_, base) = obligation.cause.code().peel_derives_with_predicate();
4981+
let post = if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
4982+
&& let ty::ClauseKind::Trait(pred) = clause
4983+
&& let Some(base) = base
4984+
&& base.skip_binder() != pred
4985+
{
4986+
format!(", which is required by `{base}`")
4987+
} else {
4988+
String::new()
4989+
};
49814990
match ty_desc {
49824991
Some(desc) => format!(
4983-
"{}the trait `{}` is not implemented for {} `{}`",
4992+
"{}the trait `{}` is not implemented for {} `{}`{post}",
49844993
pre_message,
49854994
trait_predicate.print_modifiers_and_trait_path(),
49864995
desc,
49874996
tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None),
49884997
),
49894998
None => format!(
4990-
"{}the trait `{}` is not implemented for `{}`",
4999+
"{}the trait `{}` is not implemented for `{}`{post}",
49915000
pre_message,
49925001
trait_predicate.print_modifiers_and_trait_path(),
49935002
tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None),

tests/ui/associated-consts/issue-58022.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0277]: the size for values of type `[u8]` cannot be known at compilation
1313
LL | fn new(slice: &[u8; Self::SIZE]) -> Self {
1414
| ^^^^ doesn't have a size known at compile-time
1515
|
16-
= help: within `Bar<[u8]>`, the trait `Sized` is not implemented for `[u8]`
16+
= help: within `Bar<[u8]>`, the trait `Sized` is not implemented for `[u8]`, which is required by `Bar<[u8]>: Sized`
1717
note: required because it appears within the type `Bar<[u8]>`
1818
--> $DIR/issue-58022.rs:8:12
1919
|

tests/ui/associated-type-bounds/return-type-notation/basic.without.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error: future cannot be sent between threads safely
1313
LL | is_send(foo::<T>());
1414
| ^^^^^^^^^^ future returned by `foo` is not `Send`
1515
|
16-
= help: within `impl Future<Output = Result<(), ()>>`, the trait `Send` is not implemented for `impl Future<Output = Result<(), ()>> { <T as Foo>::method() }`
16+
= help: within `impl Future<Output = Result<(), ()>>`, the trait `Send` is not implemented for `impl Future<Output = Result<(), ()>> { <T as Foo>::method() }`, which is required by `impl Future<Output = Result<(), ()>>: Send`
1717
note: future is not `Send` as it awaits another future which is not `Send`
1818
--> $DIR/basic.rs:13:5
1919
|

tests/ui/associated-types/defaults-suitability.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ error[E0277]: the trait bound `T: Clone` is not satisfied
3939
--> $DIR/defaults-suitability.rs:28:23
4040
|
4141
LL | type Bar: Clone = Vec<T>;
42-
| ^^^^^^ the trait `Clone` is not implemented for `T`
42+
| ^^^^^^ the trait `Clone` is not implemented for `T`, which is required by `Vec<T>: Clone`
4343
|
4444
= note: required for `Vec<T>` to implement `Clone`
4545
note: required by a bound in `Foo::Bar`
@@ -88,7 +88,7 @@ error[E0277]: the trait bound `<Self as Foo2<T>>::Baz: Clone` is not satisfied
8888
--> $DIR/defaults-suitability.rs:65:23
8989
|
9090
LL | type Bar: Clone = Vec<Self::Baz>;
91-
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Self as Foo2<T>>::Baz`
91+
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Self as Foo2<T>>::Baz`, which is required by `Vec<<Self as Foo2<T>>::Baz>: Clone`
9292
|
9393
= note: required for `Vec<<Self as Foo2<T>>::Baz>` to implement `Clone`
9494
note: required by a bound in `Foo2::Bar`
@@ -105,7 +105,7 @@ error[E0277]: the trait bound `<Self as Foo25<T>>::Baz: Clone` is not satisfied
105105
--> $DIR/defaults-suitability.rs:74:23
106106
|
107107
LL | type Bar: Clone = Vec<Self::Baz>;
108-
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Self as Foo25<T>>::Baz`
108+
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<Self as Foo25<T>>::Baz`, which is required by `Vec<<Self as Foo25<T>>::Baz>: Clone`
109109
|
110110
= note: required for `Vec<<Self as Foo25<T>>::Baz>` to implement `Clone`
111111
note: required by a bound in `Foo25::Bar`

tests/ui/associated-types/hr-associated-type-bound-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
22
--> $DIR/hr-associated-type-bound-1.rs:12:14
33
|
44
LL | type U = str;
5-
| ^^^ the trait `Clone` is not implemented for `str`
5+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <i32 as X<'b>>::U: Clone`
66
|
77
= help: the trait `Clone` is implemented for `String`
88
note: required by a bound in `X`

tests/ui/associated-types/hr-associated-type-bound-param-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
22
--> $DIR/hr-associated-type-bound-param-1.rs:14:14
33
|
44
LL | type V = str;
5-
| ^^^ the trait `Clone` is not implemented for `str`
5+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <u8 as Y<'b, u8>>::V: Clone`
66
|
77
= help: the trait `Clone` is implemented for `String`
88
note: required by a bound in `Y`

tests/ui/associated-types/hr-associated-type-bound-param-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
1818
--> $DIR/hr-associated-type-bound-param-2.rs:15:14
1919
|
2020
LL | type W = str;
21-
| ^^^ the trait `Clone` is not implemented for `str`
21+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <u16 as Z<'b, u16>>::W: Clone`
2222
|
2323
= help: the trait `Clone` is implemented for `String`
2424
note: required by a bound in `Z`

tests/ui/associated-types/hr-associated-type-bound-param-3.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
22
--> $DIR/hr-associated-type-bound-param-3.rs:13:14
33
|
44
LL | type U = str;
5-
| ^^^ the trait `Clone` is not implemented for `str`
5+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <(T,) as X<'b, (T,)>>::U: Clone`
66
|
77
= help: the trait `Clone` is implemented for `String`
88
note: required by a bound in `X`

tests/ui/associated-types/hr-associated-type-bound-param-4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
22
--> $DIR/hr-associated-type-bound-param-4.rs:13:14
33
|
44
LL | type U = str;
5-
| ^^^ the trait `Clone` is not implemented for `str`
5+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <(T,) as X<'b, T>>::U: Clone`
66
|
77
= help: the trait `Clone` is implemented for `String`
88
note: required by a bound in `X`

tests/ui/associated-types/hr-associated-type-bound-param-5.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
22
--> $DIR/hr-associated-type-bound-param-5.rs:26:14
33
|
44
LL | type U = str;
5-
| ^^^ the trait `Clone` is not implemented for `str`
5+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <<Vec<T> as Cycle>::Next as X<'b, <Vec<T> as Cycle>::Next>>::U: Clone`
66
|
77
= help: the trait `Clone` is implemented for `String`
88
note: required by a bound in `X`
@@ -18,7 +18,7 @@ error[E0277]: the trait bound `str: Clone` is not satisfied
1818
--> $DIR/hr-associated-type-bound-param-5.rs:31:14
1919
|
2020
LL | type U = str;
21-
| ^^^ the trait `Clone` is not implemented for `str`
21+
| ^^^ the trait `Clone` is not implemented for `str`, which is required by `for<'b> <<Box<T> as Cycle>::Next as X<'b, <Box<T> as Cycle>::Next>>::U: Clone`
2222
|
2323
= help: the trait `Clone` is implemented for `String`
2424
note: required by a bound in `X`

0 commit comments

Comments
 (0)