Skip to content

Commit b71fd5b

Browse files
committed
Add tests for various dyn Trait in path cases
1 parent 7d5f3b2 commit b71fd5b

11 files changed

+227
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Trait {
2+
fn function() {}
3+
}
4+
5+
impl Trait for () {}
6+
7+
fn main() {
8+
Trait::function();
9+
//~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
2+
--> $DIR/inferred-type-in-qualified-path-for-with-one-impl.rs:8:5
3+
|
4+
LL | fn function() {}
5+
| ---------------- `Trait::function` defined here
6+
...
7+
LL | Trait::function();
8+
| ^^^^^^^^^^^^^^^^^ cannot call associated function of trait
9+
|
10+
help: use the fully-qualified path to the only available implementation
11+
|
12+
LL | <() as Trait>::function();
13+
| ++++++ +
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0790`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trait Trait {
2+
fn function() {}
3+
}
4+
5+
impl Trait for () {}
6+
7+
fn main() {
8+
<dyn Trait>::function(); //~ ERROR the trait `Trait` cannot be made into an object
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs:8:6
3+
|
4+
LL | <dyn Trait>::function();
5+
| ^^^^^^^^^ `Trait` cannot be made into an object
6+
|
7+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8+
--> $DIR/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs:2:8
9+
|
10+
LL | trait Trait {
11+
| ----- this trait cannot be made into an object...
12+
LL | fn function() {}
13+
| ^^^^^^^^ ...because associated function `function` has no `self` parameter
14+
= help: only type `()` implements the trait, consider using it directly instead
15+
help: consider turning `function` into a method by giving it a `&self` argument
16+
|
17+
LL | fn function(&self) {}
18+
| +++++
19+
help: alternatively, consider constraining `function` so it does not apply to trait objects
20+
|
21+
LL | fn function() where Self: Sized {}
22+
| +++++++++++++++++
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0038`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ run-pass
2+
3+
trait Trait {
4+
fn function(&self) {}
5+
}
6+
7+
impl dyn Trait {
8+
}
9+
10+
impl Trait for () {}
11+
12+
fn main() {
13+
Trait::function(&());
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Trait: Sized {}
2+
3+
impl dyn Trait { //~ ERROR the trait `Trait` cannot be made into an object
4+
fn function(&self) {} //~ ERROR the trait `Trait` cannot be made into an object
5+
}
6+
7+
impl Trait for () {}
8+
9+
fn main() {
10+
<dyn Trait>::function(&());
11+
//~^ ERROR the trait `Trait` cannot be made into an object
12+
//~| ERROR the trait `Trait` cannot be made into an object
13+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:3:6
3+
|
4+
LL | impl dyn Trait {
5+
| ^^^^^^^^^ `Trait` cannot be made into an object
6+
|
7+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14
9+
|
10+
LL | trait Trait: Sized {}
11+
| ----- ^^^^^ ...because it requires `Self: Sized`
12+
| |
13+
| this trait cannot be made into an object...
14+
= help: only type `()` implements the trait, consider using it directly instead
15+
16+
error[E0038]: the trait `Trait` cannot be made into an object
17+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:4:18
18+
|
19+
LL | fn function(&self) {}
20+
| ^^^^ `Trait` cannot be made into an object
21+
|
22+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
23+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14
24+
|
25+
LL | trait Trait: Sized {}
26+
| ----- ^^^^^ ...because it requires `Self: Sized`
27+
| |
28+
| this trait cannot be made into an object...
29+
= help: only type `()` implements the trait, consider using it directly instead
30+
31+
error[E0038]: the trait `Trait` cannot be made into an object
32+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:10:27
33+
|
34+
LL | <dyn Trait>::function(&());
35+
| ^^^ `Trait` cannot be made into an object
36+
|
37+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
38+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14
39+
|
40+
LL | trait Trait: Sized {}
41+
| ----- ^^^^^ ...because it requires `Self: Sized`
42+
| |
43+
| this trait cannot be made into an object...
44+
= help: only type `()` implements the trait, consider using it directly instead
45+
= note: required for the cast from `&()` to `&(dyn Trait + 'static)`
46+
47+
error[E0038]: the trait `Trait` cannot be made into an object
48+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:10:6
49+
|
50+
LL | <dyn Trait>::function(&());
51+
| ^^^^^^^^^ `Trait` cannot be made into an object
52+
|
53+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
54+
--> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14
55+
|
56+
LL | trait Trait: Sized {}
57+
| ----- ^^^^^ ...because it requires `Self: Sized`
58+
| |
59+
| this trait cannot be made into an object...
60+
= help: only type `()` implements the trait, consider using it directly instead
61+
62+
error: aborting due to 4 previous errors
63+
64+
For more information about this error, try `rustc --explain E0038`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait Trait {
2+
fn function(&self) {}
3+
}
4+
impl dyn Trait {
5+
fn function(&self) {}
6+
}
7+
impl Trait for () {}
8+
fn main() {
9+
<dyn Trait>::function(&());
10+
//~^ ERROR multiple applicable items in scope
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/trait-dyn-in-qualified-path-with-collision.rs:9:18
3+
|
4+
LL | <dyn Trait>::function(&());
5+
| -------------^^^^^^^^ multiple `function` found
6+
| |
7+
| help: use fully-qualified syntax to disambiguate: `Trait::`
8+
|
9+
note: candidate #1 is defined in the trait `Trait`
10+
--> $DIR/trait-dyn-in-qualified-path-with-collision.rs:2:5
11+
|
12+
LL | fn function(&self) {}
13+
| ^^^^^^^^^^^^^^^^^^
14+
note: candidate #2 is defined in an impl for the type `(dyn Trait + 'static)`
15+
--> $DIR/trait-dyn-in-qualified-path-with-collision.rs:5:5
16+
|
17+
LL | fn function(&self) {}
18+
| ^^^^^^^^^^^^^^^^^^
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0034`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait Trait: Sized {
2+
fn function() {}
3+
}
4+
fn main() {
5+
<dyn Trait>::function();
6+
//~^ ERROR trait `Trait` cannot be made into an object
7+
//~| ERROR the size for values of type `dyn Trait` cannot be known at compilation time
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0038]: the trait `Trait` cannot be made into an object
2+
--> $DIR/trait-dyn-in-qualified-path.rs:5:6
3+
|
4+
LL | <dyn Trait>::function();
5+
| ^^^^^^^^^ `Trait` cannot be made into an object
6+
|
7+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
8+
--> $DIR/trait-dyn-in-qualified-path.rs:1:14
9+
|
10+
LL | trait Trait: Sized {
11+
| ----- ^^^^^ ...because it requires `Self: Sized`
12+
| |
13+
| this trait cannot be made into an object...
14+
15+
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
16+
--> $DIR/trait-dyn-in-qualified-path.rs:5:6
17+
|
18+
LL | <dyn Trait>::function();
19+
| ^^^^^^^^^ doesn't have a size known at compile-time
20+
|
21+
= help: the trait `Sized` is not implemented for `dyn Trait`
22+
note: required by a bound in `Trait::function`
23+
--> $DIR/trait-dyn-in-qualified-path.rs:1:14
24+
|
25+
LL | trait Trait: Sized {
26+
| ^^^^^ required by this bound in `Trait::function`
27+
LL | fn function() {}
28+
| -------- required by a bound in this associated function
29+
30+
error: aborting due to 2 previous errors
31+
32+
Some errors have detailed explanations: E0038, E0277.
33+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)