Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8bc5581

Browse files
committed
Point at impl and type defs introducing requirements on E0277
1 parent d326c21 commit 8bc5581

File tree

55 files changed

+621
-110
lines changed

Some content is hidden

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

55 files changed

+621
-110
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ impl<'tcx> TyCtxt<'tcx> {
15981598
.filter(|item| item.kind == AssocKind::Fn && item.defaultness.has_value())
15991599
}
16001600

1601-
fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
1601+
pub fn item_name_from_hir(self, def_id: DefId) -> Option<Ident> {
16021602
self.hir().get_if_local(def_id).and_then(|node| node.ident())
16031603
}
16041604

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20702070

20712071
// Don't print the tuple of capture types
20722072
if !is_upvar_tys_infer_tuple {
2073-
err.note(&format!("required because it appears within the type `{}`", ty));
2073+
let msg = format!("required because it appears within the type `{}`", ty);
2074+
match ty.kind() {
2075+
ty::Adt(def, _) => match self.tcx.item_name_from_hir(def.did) {
2076+
Some(ident) => err.span_note(ident.span, &msg),
2077+
None => err.note(&msg),
2078+
},
2079+
_ => err.note(&msg),
2080+
};
20742081
}
20752082

20762083
obligated_types.push(ty);
@@ -2092,11 +2099,36 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20922099
ObligationCauseCode::ImplDerivedObligation(ref data) => {
20932100
let mut parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_ref);
20942101
let parent_def_id = parent_trait_ref.def_id();
2095-
err.note(&format!(
2102+
let msg = format!(
20962103
"required because of the requirements on the impl of `{}` for `{}`",
20972104
parent_trait_ref.print_only_trait_path(),
20982105
parent_trait_ref.skip_binder().self_ty()
2099-
));
2106+
);
2107+
let mut candidates = vec![];
2108+
self.tcx.for_each_relevant_impl(
2109+
parent_def_id,
2110+
parent_trait_ref.self_ty().skip_binder(),
2111+
|impl_def_id| {
2112+
candidates.push(impl_def_id);
2113+
},
2114+
);
2115+
match &candidates[..] {
2116+
[def_id] => match self.tcx.hir().get_if_local(*def_id) {
2117+
Some(Node::Item(hir::Item {
2118+
kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
2119+
..
2120+
})) => {
2121+
let mut spans = Vec::with_capacity(2);
2122+
if let Some(trait_ref) = of_trait {
2123+
spans.push(trait_ref.path.span);
2124+
}
2125+
spans.push(self_ty.span);
2126+
err.span_note(spans, &msg)
2127+
}
2128+
_ => err.note(&msg),
2129+
},
2130+
_ => err.note(&msg),
2131+
};
21002132

21012133
let mut parent_predicate = parent_trait_ref.without_const().to_predicate(tcx);
21022134
let mut data = data;

src/test/ui/associated-types/impl-wf-cycle-1.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ LL | |
1010
LL | | }
1111
| |_^
1212
|
13-
= note: required because of the requirements on the impl of `Grault` for `(T,)`
13+
note: required because of the requirements on the impl of `Grault` for `(T,)`
14+
--> $DIR/impl-wf-cycle-1.rs:15:17
15+
|
16+
LL | impl<T: Grault> Grault for (T,)
17+
| ^^^^^^ ^^^^
1418
= note: 1 redundant requirements hidden
1519
= note: required because of the requirements on the impl of `Grault` for `(T,)`
1620

@@ -20,7 +24,11 @@ error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
2024
LL | type A = ();
2125
| ^^^^^^^^^^^^
2226
|
23-
= note: required because of the requirements on the impl of `Grault` for `(T,)`
27+
note: required because of the requirements on the impl of `Grault` for `(T,)`
28+
--> $DIR/impl-wf-cycle-1.rs:15:17
29+
|
30+
LL | impl<T: Grault> Grault for (T,)
31+
| ^^^^^^ ^^^^
2432
= note: 1 redundant requirements hidden
2533
= note: required because of the requirements on the impl of `Grault` for `(T,)`
2634

@@ -30,7 +38,11 @@ error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
3038
LL | type B = bool;
3139
| ^^^^^^^^^^^^^^
3240
|
33-
= note: required because of the requirements on the impl of `Grault` for `(T,)`
41+
note: required because of the requirements on the impl of `Grault` for `(T,)`
42+
--> $DIR/impl-wf-cycle-1.rs:15:17
43+
|
44+
LL | impl<T: Grault> Grault for (T,)
45+
| ^^^^^^ ^^^^
3446
= note: 1 redundant requirements hidden
3547
= note: required because of the requirements on the impl of `Grault` for `(T,)`
3648

src/test/ui/associated-types/impl-wf-cycle-2.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ LL | |
1010
LL | | }
1111
| |_^
1212
|
13-
= note: required because of the requirements on the impl of `Grault` for `(T,)`
13+
note: required because of the requirements on the impl of `Grault` for `(T,)`
14+
--> $DIR/impl-wf-cycle-2.rs:7:17
15+
|
16+
LL | impl<T: Grault> Grault for (T,)
17+
| ^^^^^^ ^^^^
1418

1519
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
1620
--> $DIR/impl-wf-cycle-2.rs:11:5
1721
|
1822
LL | type A = ();
1923
| ^^^^^^^^^^^^
2024
|
21-
= note: required because of the requirements on the impl of `Grault` for `(T,)`
25+
note: required because of the requirements on the impl of `Grault` for `(T,)`
26+
--> $DIR/impl-wf-cycle-2.rs:7:17
27+
|
28+
LL | impl<T: Grault> Grault for (T,)
29+
| ^^^^^^ ^^^^
2230

2331
error: aborting due to 2 previous errors
2432

src/test/ui/associated-types/issue-44153.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | fn visit() {}
77
LL | <() as Visit>::visit();
88
| ^^^^^^^^^^^^^^^^^^^^ expected `&()`, found `()`
99
|
10-
= note: required because of the requirements on the impl of `Visit` for `()`
10+
note: required because of the requirements on the impl of `Visit` for `()`
11+
--> $DIR/issue-44153.rs:13:10
12+
|
13+
LL | impl<'a> Visit for () where
14+
| ^^^^^ ^^
1115

1216
error: aborting due to previous error
1317

src/test/ui/associated-types/issue-65774-1.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ error[E0277]: the trait bound `T: MyDisplay` is not satisfied
1313
LL | let closure = |config: &mut <S as MPU>::MpuConfig| writer.my_write(&config);
1414
| ^^^^^^^ the trait `MyDisplay` is not implemented for `T`
1515
|
16-
= note: required because of the requirements on the impl of `MyDisplay` for `&mut T`
16+
note: required because of the requirements on the impl of `MyDisplay` for `&mut T`
17+
--> $DIR/issue-65774-1.rs:5:24
18+
|
19+
LL | impl<'a, T: MyDisplay> MyDisplay for &'a mut T { }
20+
| ^^^^^^^^^ ^^^^^^^^^
1721
= note: required for the cast to the object type `dyn MyDisplay`
1822

1923
error: aborting due to 2 previous errors

src/test/ui/async-await/issue-72590-type-error-sized.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ LL | async fn frob(self) {}
1717
| ^^^^ doesn't have a size known at compile-time
1818
|
1919
= help: within `Foo`, the trait `Sized` is not implemented for `str`
20-
= note: required because it appears within the type `Foo`
20+
note: required because it appears within the type `Foo`
21+
--> $DIR/issue-72590-type-error-sized.rs:5:8
22+
|
23+
LL | struct Foo {
24+
| ^^^
2125
= help: unsized fn params are gated as an unstable feature
2226
help: function arguments must have a statically known size, borrowed types always have a known size
2327
|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::{
2+
future::Future,
3+
pin::Pin,
4+
marker::Unpin,
5+
task::{Context, Poll},
6+
};
7+
8+
struct Sleep(std::marker::PhantomPinned);
9+
10+
impl Future for Sleep {
11+
type Output = ();
12+
13+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
14+
Poll::Ready(())
15+
}
16+
}
17+
18+
impl Drop for Sleep {
19+
fn drop(&mut self) {}
20+
}
21+
22+
fn sleep() -> Sleep {
23+
Sleep(std::marker::PhantomPinned)
24+
}
25+
26+
27+
struct MyFuture {
28+
sleep: Sleep,
29+
}
30+
31+
impl MyFuture {
32+
fn new() -> Self {
33+
Self {
34+
sleep: sleep(),
35+
}
36+
}
37+
}
38+
39+
impl Future for MyFuture {
40+
type Output = ();
41+
42+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
43+
Pin::new(&mut self.sleep).poll(cx)
44+
//~^ ERROR `PhantomPinned` cannot be unpinned
45+
}
46+
}
47+
48+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: `PhantomPinned` cannot be unpinned
2+
--> $DIR/pin-needed-to-poll-2.rs:43:9
3+
|
4+
LL | Pin::new(&mut self.sleep).poll(cx)
5+
| ^^^^^^^^ within `Sleep`, the trait `Unpin` is not implemented for `PhantomPinned`
6+
|
7+
note: required because it appears within the type `Sleep`
8+
--> $DIR/pin-needed-to-poll-2.rs:8:8
9+
|
10+
LL | struct Sleep(std::marker::PhantomPinned);
11+
| ^^^^^
12+
= note: required by `Pin::<P>::new`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/block-result/issue-22645.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ LL | b + 3
66
|
77
= help: the following implementations were found:
88
<f64 as Scalar>
9-
= note: required because of the requirements on the impl of `Add<{integer}>` for `Bob`
9+
note: required because of the requirements on the impl of `Add<{integer}>` for `Bob`
10+
--> $DIR/issue-22645.rs:8:19
11+
|
12+
LL | impl<RHS: Scalar> Add <RHS> for Bob {
13+
| ^^^^^^^^^ ^^^
1014

1115
error[E0308]: mismatched types
1216
--> $DIR/issue-22645.rs:15:3

0 commit comments

Comments
 (0)