You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide better suggestions when encountering a bare trait as a type
Add the following suggestions:
```
error[E0782]: trait objects must include the `dyn` keyword
--> $DIR/not-on-bare-trait-2021.rs:11:11
|
LL | fn bar(x: Foo) -> Foo {
| ^^^
|
help: use a generic type parameter, constrained by the trait `Foo`
|
LL | fn bar<T: Foo>(x: T) -> Foo {
| ++++++++ ~
help: you can also use `impl Foo`, but users won't be able to specify the type paramer when calling the `fn`, having to rely exclusively on type inference
|
LL | fn bar(x: impl Foo) -> Foo {
| ++++
help: alternatively, use a trait object to accept any type that implements `Foo`, accessing its methods at runtime using dynamic dispatch
|
LL | fn bar(x: &dyn Foo) -> Foo {
| ++++
error[E0782]: trait objects must include the `dyn` keyword
--> $DIR/not-on-bare-trait-2021.rs:11:19
|
LL | fn bar(x: Foo) -> Foo {
| ^^^
|
help: use `impl Foo` to return an opaque type, as long as you return a single underlying type
|
LL | fn bar(x: Foo) -> impl Foo {
| ++++
help: alternatively, you can return an owned trait object
|
LL | fn bar(x: Foo) -> Box<dyn Foo> {
| +++++++ +
```
error[E0782]: trait objects must include the `dyn` keyword
2
+
--> $DIR/not-on-bare-trait-2021.rs:8:12
3
+
|
4
+
LL | fn foo(_x: Foo + Send) {
5
+
| ^^^^^^^^^^
6
+
|
7
+
help: use a new generic type parameter, constrained by `Foo + Send`
8
+
|
9
+
LL | fn foo<T: Foo + Send>(_x: T) {
10
+
| +++++++++++++++ ~
11
+
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
12
+
|
13
+
LL | fn foo(_x: impl Foo + Send) {
14
+
| ++++
15
+
help: alternatively, use a trait object to accept any type that implements `Foo + Send`, accessing its methods at runtime using dynamic dispatch
16
+
|
17
+
LL | fn foo(_x: &(dyn Foo + Send)) {
18
+
| +++++ +
19
+
20
+
error[E0782]: trait objects must include the `dyn` keyword
21
+
--> $DIR/not-on-bare-trait-2021.rs:11:11
22
+
|
23
+
LL | fn bar(x: Foo) -> Foo {
24
+
| ^^^
25
+
|
26
+
help: use a new generic type parameter, constrained by `Foo`
27
+
|
28
+
LL | fn bar<T: Foo>(x: T) -> Foo {
29
+
| ++++++++ ~
30
+
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
31
+
|
32
+
LL | fn bar(x: impl Foo) -> Foo {
33
+
| ++++
34
+
help: alternatively, use a trait object to accept any type that implements `Foo`, accessing its methods at runtime using dynamic dispatch
35
+
|
36
+
LL | fn bar(x: &dyn Foo) -> Foo {
37
+
| ++++
38
+
39
+
error[E0782]: trait objects must include the `dyn` keyword
40
+
--> $DIR/not-on-bare-trait-2021.rs:11:19
41
+
|
42
+
LL | fn bar(x: Foo) -> Foo {
43
+
| ^^^
44
+
|
45
+
help: use `impl Foo` to return an opaque type, as long as you return a single underlying type
46
+
|
47
+
LL | fn bar(x: Foo) -> impl Foo {
48
+
| ++++
49
+
help: alternatively, you can return an owned trait object
50
+
|
51
+
LL | fn bar(x: Foo) -> Box<dyn Foo> {
52
+
| +++++++ +
53
+
54
+
error: aborting due to 3 previous errors
55
+
56
+
For more information about this error, try `rustc --explain E0782`.
Copy file name to clipboardExpand all lines: tests/ui/traits/bound/not-on-bare-trait.stderr
+11-3
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,18 @@ LL | fn foo(_x: Foo + Send) {
7
7
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8
8
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9
9
= note: `#[warn(bare_trait_objects)]` on by default
10
-
help: use `dyn`
10
+
help: use a new generic type parameter, constrained by `Foo + Send`
11
11
|
12
-
LL | fn foo(_x: dyn Foo + Send) {
13
-
| +++
12
+
LL | fn foo<T: Foo + Send>(_x: T) {
13
+
| +++++++++++++++ ~
14
+
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
15
+
|
16
+
LL | fn foo(_x: impl Foo + Send) {
17
+
| ++++
18
+
help: alternatively, use a trait object to accept any type that implements `Foo + Send`, accessing its methods at runtime using dynamic dispatch
19
+
|
20
+
LL | fn foo(_x: &(dyn Foo + Send)) {
21
+
| +++++ +
14
22
15
23
error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
0 commit comments