Skip to content

Commit 99ab45b

Browse files
committed
Handle Self restriction needed
1 parent dbd75c8 commit 99ab45b

14 files changed

+76
-37
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,34 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
986986
while let Some(node) = self.tcx.hir().find(hir_id) {
987987
debug!("suggest_restricting_param_bound node={:?}", node);
988988
match node {
989+
hir::Node::Item(hir::Item {
990+
kind: hir::ItemKind::Fn(decl, _, generics, _), ..
991+
}) |
992+
hir::Node::TraitItem(hir::TraitItem {
993+
generics,
994+
kind: hir::TraitItemKind::Method(hir::MethodSig { decl, .. }, _), ..
995+
}) |
996+
hir::Node::ImplItem(hir::ImplItem {
997+
generics,
998+
kind: hir::ImplItemKind::Method(hir::MethodSig { decl, .. }, _), ..
999+
}) if param_ty.name.as_str() == "Self" => {
1000+
if !generics.where_clause.predicates.is_empty() {
1001+
err.span_suggestion(
1002+
generics.where_clause.span().unwrap().shrink_to_hi(),
1003+
"consider further restricting `Self`",
1004+
format!(", {}", trait_ref.to_predicate()),
1005+
Applicability::MachineApplicable,
1006+
);
1007+
} else {
1008+
err.span_suggestion(
1009+
decl.output.span().shrink_to_hi(),
1010+
"consider further restricting `Self`",
1011+
format!(" where {}", trait_ref.to_predicate()),
1012+
Applicability::MachineApplicable,
1013+
);
1014+
}
1015+
return;
1016+
}
9891017
hir::Node::Item(hir::Item { kind: hir::ItemKind::Struct(_, generics), span, .. }) |
9901018
hir::Node::Item(hir::Item { kind: hir::ItemKind::Enum(_, generics), span, .. }) |
9911019
hir::Node::Item(hir::Item { kind: hir::ItemKind::Union(_, generics), span, .. }) |

src/test/ui/associated-types/associated-types-for-unimpl-trait.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0277]: the trait bound `Self: Get` is not satisfied
22
--> $DIR/associated-types-for-unimpl-trait.rs:7:5
33
|
44
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
6-
|
7-
= help: consider adding a `where Self: Get` bound
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-^
6+
| | |
7+
| | help: consider further restricting `Self`: `where Self: Get`
8+
| the trait `Get` is not implemented for `Self`
89

910
error: aborting due to previous error
1011

src/test/ui/associated-types/associated-types-no-suitable-supertrait-2.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0277]: the trait bound `Self: Get` is not satisfied
22
--> $DIR/associated-types-no-suitable-supertrait-2.rs:17:5
33
|
44
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
6-
|
7-
= help: consider adding a `where Self: Get` bound
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-^
6+
| | |
7+
| | help: consider further restricting `Self`: `where Self: Get`
8+
| the trait `Get` is not implemented for `Self`
89

910
error: aborting due to previous error
1011

src/test/ui/associated-types/associated-types-no-suitable-supertrait.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0277]: the trait bound `Self: Get` is not satisfied
22
--> $DIR/associated-types-no-suitable-supertrait.rs:17:5
33
|
44
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
6-
|
7-
= help: consider adding a `where Self: Get` bound
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-^
6+
| | |
7+
| | help: consider further restricting `Self`: `where Self: Get`
8+
| the trait `Get` is not implemented for `Self`
89

910
error[E0277]: the trait bound `(T, U): Get` is not satisfied
1011
--> $DIR/associated-types-no-suitable-supertrait.rs:22:5

src/test/ui/associated-types/associated-types-projection-to-unrelated-trait-in-method-without-default.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0277]: the trait bound `Self: Get` is not satisfied
22
--> $DIR/associated-types-projection-to-unrelated-trait-in-method-without-default.rs:9:5
33
|
44
LL | fn okay<U:Get>(&self, foo: U, bar: <Self as Get>::Value);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
6-
|
7-
= help: consider adding a `where Self: Get` bound
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
6+
| | |
7+
| | help: consider further restricting `Self`: `where Self: Get`
8+
| the trait `Get` is not implemented for `Self`
89

910
error: aborting due to previous error
1011

src/test/ui/issues/issue-20005.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ LL | trait From<Src> {
77
LL | / fn to<Dst>(
88
LL | | self
99
LL | | ) -> <Dst as From<Self>>::Result where Dst: From<Self> {
10+
| | - help: consider further restricting `Self`: `, Self: std::marker::Sized`
1011
LL | | From::from(self)
1112
LL | | }
1213
| |_____^ doesn't have a size known at compile-time
1314
|
1415
= help: the trait `std::marker::Sized` is not implemented for `Self`
1516
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
16-
= help: consider adding a `where Self: std::marker::Sized` bound
1717

1818
error: aborting due to previous error
1919

src/test/ui/issues/issue-27078.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
22
--> $DIR/issue-27078.rs:5:12
33
|
44
LL | fn foo(self) -> &'static i32 {
5-
| ^^^^ doesn't have a size known at compile-time
5+
| ^^^^ - help: consider further restricting `Self`: `where Self: std::marker::Sized`
6+
| |
7+
| doesn't have a size known at compile-time
68
|
79
= help: the trait `std::marker::Sized` is not implemented for `Self`
810
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9-
= help: consider adding a `where Self: std::marker::Sized` bound
1011
= note: all local variables must have a statically known size
1112
= help: unsized locals are gated as an unstable feature
1213

src/test/ui/type/type-params-in-different-spaces-2.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
44
LL | fn op(_: T) -> Self;
55
| -------------------- required by `Tr::op`
66
...
7+
LL | fn test<U>(u: U) -> Self {
8+
| - help: consider further restricting `Self`: `where Self: Tr<U>`
79
LL | Tr::op(u)
810
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
9-
|
10-
= help: consider adding a `where Self: Tr<U>` bound
1111

1212
error[E0277]: the trait bound `Self: Tr<U>` is not satisfied
1313
--> $DIR/type-params-in-different-spaces-2.rs:16:9
1414
|
1515
LL | fn op(_: T) -> Self;
1616
| -------------------- required by `Tr::op`
1717
...
18+
LL | fn test<U>(u: U) -> Self {
19+
| - help: consider further restricting `Self`: `where Self: Tr<U>`
1820
LL | Tr::op(u)
1921
| ^^^^^^ the trait `Tr<U>` is not implemented for `Self`
20-
|
21-
= help: consider adding a `where Self: Tr<U>` bound
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/wf/wf-trait-default-fn-arg.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ error[E0277]: the trait bound `Self: std::cmp::Eq` is not satisfied
44
LL | struct Bar<T:Eq+?Sized> { value: Box<T> }
55
| ----------------------- required by `Bar`
66
...
7-
LL | / fn bar(&self, x: &Bar<Self>) {
7+
LL | fn bar(&self, x: &Bar<Self>) {
8+
| ^ - help: consider further restricting `Self`: `where Self: std::cmp::Eq`
9+
| _____|
10+
| |
811
LL | |
912
LL | | //
1013
LL | | // Here, Eq ought to be implemented.
1114
LL | | }
1215
| |_____^ the trait `std::cmp::Eq` is not implemented for `Self`
13-
|
14-
= help: consider adding a `where Self: std::cmp::Eq` bound
1516

1617
error: aborting due to previous error
1718

src/test/ui/wf/wf-trait-default-fn-ret.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ error[E0277]: the trait bound `Self: std::cmp::Eq` is not satisfied
44
LL | struct Bar<T:Eq+?Sized> { value: Box<T> }
55
| ----------------------- required by `Bar`
66
...
7-
LL | / fn bar(&self) -> Bar<Self> {
7+
LL | fn bar(&self) -> Bar<Self> {
8+
| ^ - help: consider further restricting `Self`: `where Self: std::cmp::Eq`
9+
| _____|
10+
| |
811
LL | |
912
LL | | //
1013
LL | | // Here, Eq ought to be implemented.
1114
LL | | loop { }
1215
LL | | }
1316
| |_____^ the trait `std::cmp::Eq` is not implemented for `Self`
14-
|
15-
= help: consider adding a `where Self: std::cmp::Eq` bound
1617

1718
error: aborting due to previous error
1819

0 commit comments

Comments
 (0)