Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0313eb2

Browse files
authoredJan 31, 2024
Rollup merge of #120469 - estebank:issue-40120, r=TaKO8Ki
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.
2 parents a7d5382 + 6efddac commit 0313eb2

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
|

0 commit comments

Comments
 (0)
Please sign in to comment.