Skip to content

Commit 542130b

Browse files
committed
add tests for structured suggestion
1 parent a52ec87 commit 542130b

5 files changed

+103
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait Trait {
2+
fn baz(&self, _: Self) {}
3+
fn bat(&self) -> Self {}
4+
}
5+
6+
fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object
7+
8+
trait Other: Sized {}
9+
10+
fn foo(x: &dyn Other) {} //~ ERROR the trait `Other` cannot be made into an object
11+
12+
fn main() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/object-unsafe-trait-references-self.rs:6:11
3+
|
4+
LL | trait Trait {
5+
| ----- this trait cannot be made into an object...
6+
LL | fn baz(&self, _: Self) {}
7+
| ---- ...because method `baz` references the `Self` type in this parameter
8+
LL | fn bat(&self) -> Self {}
9+
| ---- ...because method `bat` references the `Self` type in its return type
10+
...
11+
LL | fn bar(x: &dyn Trait) {}
12+
| ^^^^^^^^^^ the trait `Trait` cannot be made into an object
13+
|
14+
= help: consider moving `baz` to another trait
15+
= help: consider moving `bat` to another trait
16+
17+
error[E0038]: the trait `Other` cannot be made into an object
18+
--> $DIR/object-unsafe-trait-references-self.rs:10:11
19+
|
20+
LL | trait Other: Sized {}
21+
| ----- ----- ...because it requires `Self: Sized`
22+
| |
23+
| this trait cannot be made into an object...
24+
LL |
25+
LL | fn foo(x: &dyn Other) {}
26+
| ^^^^^^^^^^ the trait `Other` cannot be made into an object
27+
28+
error: aborting due to 2 previous errors
29+
30+
For more information about this error, try `rustc --explain E0038`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![allow(unused_variables, dead_code)]
3+
4+
trait Trait {
5+
fn foo() where Self: Other, Self: Sized, { }
6+
fn bar(self: &Self) {} //~ ERROR invalid `self` parameter type
7+
}
8+
9+
fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object
10+
11+
trait Other {}
12+
13+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![allow(unused_variables, dead_code)]
3+
4+
trait Trait {
5+
fn foo() where Self: Other, { }
6+
fn bar(self: ()) {} //~ ERROR invalid `self` parameter type
7+
}
8+
9+
fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object
10+
11+
trait Other {}
12+
13+
fn main() {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/object-unsafe-trait-should-use-where-sized.rs:9:11
3+
|
4+
LL | trait Trait {
5+
| ----- this trait cannot be made into an object...
6+
LL | fn foo() where Self: Other, { }
7+
| --- ...because associated function `foo` has no `self` parameter
8+
LL | fn bar(self: ()) {}
9+
| -- ...because method `bar`'s `self` parameter cannot be dispatched on
10+
...
11+
LL | fn bar(x: &dyn Trait) {}
12+
| ^^^^^^^^^^ the trait `Trait` cannot be made into an object
13+
|
14+
help: consider turning `foo` into a method by giving it a `&self` argument or constraining it so it does not apply to trait objects
15+
|
16+
LL | fn foo() where Self: Other, Self: Sized, { }
17+
| ^^^^^^^^^^^^^
18+
help: consider changing method `bar`'s `self` parameter to be `&self`
19+
|
20+
LL | fn bar(self: &Self) {}
21+
| ^^^^^
22+
23+
error[E0307]: invalid `self` parameter type: ()
24+
--> $DIR/object-unsafe-trait-should-use-where-sized.rs:6:18
25+
|
26+
LL | fn bar(self: ()) {}
27+
| ^^
28+
|
29+
= note: type of `self` must be `Self` or a type that dereferences to it
30+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
31+
32+
error: aborting due to 2 previous errors
33+
34+
Some errors have detailed explanations: E0038, E0307.
35+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)