Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 60a72b6

Browse files
authoredJan 27, 2024
Rollup merge of rust-lang#120275 - estebank:issue-120241, r=fmease
Avoid ICE in trait without `dyn` lint Do not attempt to provide an accurate suggestion for `impl Trait` in bare trait types when linting. Instead, only do the object safety check when an E0782 is already going to be emitted in the 2021 edition. Fix rust-lang#120241.
2 parents 5803376 + 3022c76 commit 60a72b6

16 files changed

+222
-126
lines changed
 

‎compiler/rustc_hir_analysis/src/astconv/lint.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
132132
],
133133
Applicability::MachineApplicable,
134134
);
135-
} else if diag.is_error() && is_downgradable {
135+
} else if is_downgradable {
136136
// We'll emit the object safety error already, with a structured suggestion.
137137
diag.downgrade_to_delayed_bug();
138138
}
@@ -158,7 +158,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
158158
}
159159
if !is_object_safe {
160160
diag.note(format!("`{trait_name}` it is not object safe, so it can't be `dyn`"));
161-
if diag.is_error() && is_downgradable {
161+
if is_downgradable {
162162
// We'll emit the object safety error already, with a structured suggestion.
163163
diag.downgrade_to_delayed_bug();
164164
}
@@ -241,13 +241,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
241241
} else {
242242
let msg = "trait objects without an explicit `dyn` are deprecated";
243243
tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, msg, |lint| {
244-
if self_ty.span.can_be_used_for_suggestions()
245-
&& !self.maybe_lint_impl_trait(self_ty, lint)
246-
{
244+
if self_ty.span.can_be_used_for_suggestions() {
247245
lint.multipart_suggestion_verbose(
248246
"use `dyn`",
249247
sugg,
250-
Applicability::MachineApplicable,
248+
Applicability::MaybeIncorrect,
251249
);
252250
}
253251
self.maybe_lint_blanket_trait_impl(self_ty, lint);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0038]: the trait `Copy` cannot be made into an object
2+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
3+
|
4+
LL | fn id<F>(f: Copy) -> usize {
5+
| ^^^^ `Copy` cannot be made into an object
6+
|
7+
= note: the trait cannot be made into an object because it requires `Self: Sized`
8+
= 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>
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0038`.

‎tests/ui/object-safety/avoid-ice-on-warning-2.stderr ‎tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
11
warning: trait objects without an explicit `dyn` are deprecated
2-
--> $DIR/avoid-ice-on-warning-2.rs:1:13
2+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
33
|
44
LL | fn id<F>(f: Copy) -> usize {
55
| ^^^^
66
|
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9-
= note: `Copy` it is not object safe, so it can't be `dyn`
109
= note: `#[warn(bare_trait_objects)]` on by default
11-
help: use a new generic type parameter, constrained by `Copy`
10+
help: use `dyn`
1211
|
13-
LL | fn id<F, T: Copy>(f: T) -> usize {
14-
| +++++++++ ~
15-
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
16-
|
17-
LL | fn id<F>(f: impl Copy) -> usize {
18-
| ++++
12+
LL | fn id<F>(f: dyn Copy) -> usize {
13+
| +++
1914

2015
warning: trait objects without an explicit `dyn` are deprecated
21-
--> $DIR/avoid-ice-on-warning-2.rs:1:13
16+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
2217
|
2318
LL | fn id<F>(f: Copy) -> usize {
2419
| ^^^^
2520
|
2621
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2722
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
28-
= note: `Copy` it is not object safe, so it can't be `dyn`
2923
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
30-
help: use a new generic type parameter, constrained by `Copy`
31-
|
32-
LL | fn id<F, T: Copy>(f: T) -> usize {
33-
| +++++++++ ~
34-
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
24+
help: use `dyn`
3525
|
36-
LL | fn id<F>(f: impl Copy) -> usize {
37-
| ++++
26+
LL | fn id<F>(f: dyn Copy) -> usize {
27+
| +++
3828

3929
error[E0038]: the trait `Copy` cannot be made into an object
40-
--> $DIR/avoid-ice-on-warning-2.rs:1:13
30+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
4131
|
4232
LL | fn id<F>(f: Copy) -> usize {
4333
| ^^^^ `Copy` cannot be made into an object
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
// revisions: old new
2+
//[old] edition:2015
3+
//[new] edition:2021
14
fn id<F>(f: Copy) -> usize {
2-
//~^ WARN trait objects without an explicit `dyn` are deprecated
3-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
4-
//~| WARN trait objects without an explicit `dyn` are deprecated
5-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
6-
//~| ERROR the trait `Copy` cannot be made into an object
5+
//~^ ERROR the trait `Copy` cannot be made into an object
6+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
7+
//[old]~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
9+
//[old]~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
710
f()
811
}
912
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0038]: the trait `A` cannot be made into an object
2+
--> $DIR/avoid-ice-on-warning-3.rs:4:19
3+
|
4+
LL | trait B { fn f(a: A) -> A; }
5+
| ^ `A` 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/avoid-ice-on-warning-3.rs:12:14
9+
|
10+
LL | trait A { fn g(b: B) -> B; }
11+
| - ^ ...because associated function `g` has no `self` parameter
12+
| |
13+
| this trait cannot be made into an object...
14+
help: consider turning `g` into a method by giving it a `&self` argument
15+
|
16+
LL | trait A { fn g(&self, b: B) -> B; }
17+
| ++++++
18+
help: alternatively, consider constraining `g` so it does not apply to trait objects
19+
|
20+
LL | trait A { fn g(b: B) -> B where Self: Sized; }
21+
| +++++++++++++++++
22+
23+
error[E0038]: the trait `B` cannot be made into an object
24+
--> $DIR/avoid-ice-on-warning-3.rs:12:19
25+
|
26+
LL | trait A { fn g(b: B) -> B; }
27+
| ^ `B` cannot be made into an object
28+
|
29+
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>
30+
--> $DIR/avoid-ice-on-warning-3.rs:4:14
31+
|
32+
LL | trait B { fn f(a: A) -> A; }
33+
| - ^ ...because associated function `f` has no `self` parameter
34+
| |
35+
| this trait cannot be made into an object...
36+
help: consider turning `f` into a method by giving it a `&self` argument
37+
|
38+
LL | trait B { fn f(&self, a: A) -> A; }
39+
| ++++++
40+
help: alternatively, consider constraining `f` so it does not apply to trait objects
41+
|
42+
LL | trait B { fn f(a: A) -> A where Self: Sized; }
43+
| +++++++++++++++++
44+
45+
error: aborting due to 2 previous errors
46+
47+
For more information about this error, try `rustc --explain E0038`.

‎tests/ui/object-safety/avoid-ice-on-warning-3.stderr ‎tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr

+32-52
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,78 @@
11
warning: trait objects without an explicit `dyn` are deprecated
2-
--> $DIR/avoid-ice-on-warning-3.rs:9:19
2+
--> $DIR/avoid-ice-on-warning-3.rs:4:19
33
|
4-
LL | trait A { fn g(b: B) -> B; }
4+
LL | trait B { fn f(a: A) -> A; }
55
| ^
66
|
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9-
= note: `B` it is not object safe, so it can't be `dyn`
109
= note: `#[warn(bare_trait_objects)]` on by default
11-
help: use a new generic type parameter, constrained by `B`
12-
|
13-
LL | trait A { fn g<T: B>(b: T) -> B; }
14-
| ++++++ ~
15-
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
10+
help: use `dyn`
1611
|
17-
LL | trait A { fn g(b: impl B) -> B; }
18-
| ++++
12+
LL | trait B { fn f(a: dyn A) -> A; }
13+
| +++
1914

2015
warning: trait objects without an explicit `dyn` are deprecated
21-
--> $DIR/avoid-ice-on-warning-3.rs:9:25
16+
--> $DIR/avoid-ice-on-warning-3.rs:4:25
2217
|
23-
LL | trait A { fn g(b: B) -> B; }
18+
LL | trait B { fn f(a: A) -> A; }
2419
| ^
2520
|
2621
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2722
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
28-
help: `B` is not object safe, use `impl B` to return an opaque type, as long as you return a single underlying type
23+
help: use `dyn`
2924
|
30-
LL | trait A { fn g(b: B) -> impl B; }
31-
| ++++
25+
LL | trait B { fn f(a: A) -> dyn A; }
26+
| +++
3227

3328
warning: trait objects without an explicit `dyn` are deprecated
34-
--> $DIR/avoid-ice-on-warning-3.rs:1:19
29+
--> $DIR/avoid-ice-on-warning-3.rs:12:19
3530
|
36-
LL | trait B { fn f(a: A) -> A; }
31+
LL | trait A { fn g(b: B) -> B; }
3732
| ^
3833
|
3934
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
4035
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
41-
= note: `A` it is not object safe, so it can't be `dyn`
42-
help: use a new generic type parameter, constrained by `A`
43-
|
44-
LL | trait B { fn f<T: A>(a: T) -> A; }
45-
| ++++++ ~
46-
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
36+
help: use `dyn`
4737
|
48-
LL | trait B { fn f(a: impl A) -> A; }
49-
| ++++
38+
LL | trait A { fn g(b: dyn B) -> B; }
39+
| +++
5040

5141
warning: trait objects without an explicit `dyn` are deprecated
52-
--> $DIR/avoid-ice-on-warning-3.rs:1:25
42+
--> $DIR/avoid-ice-on-warning-3.rs:12:25
5343
|
54-
LL | trait B { fn f(a: A) -> A; }
44+
LL | trait A { fn g(b: B) -> B; }
5545
| ^
5646
|
5747
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
5848
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
59-
help: `A` is not object safe, use `impl A` to return an opaque type, as long as you return a single underlying type
49+
help: use `dyn`
6050
|
61-
LL | trait B { fn f(a: A) -> impl A; }
62-
| ++++
51+
LL | trait A { fn g(b: B) -> dyn B; }
52+
| +++
6353

6454
warning: trait objects without an explicit `dyn` are deprecated
65-
--> $DIR/avoid-ice-on-warning-3.rs:1:19
55+
--> $DIR/avoid-ice-on-warning-3.rs:4:19
6656
|
6757
LL | trait B { fn f(a: A) -> A; }
6858
| ^
6959
|
7060
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
7161
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
72-
= note: `A` it is not object safe, so it can't be `dyn`
7362
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
74-
help: use a new generic type parameter, constrained by `A`
75-
|
76-
LL | trait B { fn f<T: A>(a: T) -> A; }
77-
| ++++++ ~
78-
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
63+
help: use `dyn`
7964
|
80-
LL | trait B { fn f(a: impl A) -> A; }
81-
| ++++
65+
LL | trait B { fn f(a: dyn A) -> A; }
66+
| +++
8267

8368
error[E0038]: the trait `A` cannot be made into an object
84-
--> $DIR/avoid-ice-on-warning-3.rs:1:19
69+
--> $DIR/avoid-ice-on-warning-3.rs:4:19
8570
|
8671
LL | trait B { fn f(a: A) -> A; }
8772
| ^ `A` cannot be made into an object
8873
|
8974
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>
90-
--> $DIR/avoid-ice-on-warning-3.rs:9:14
75+
--> $DIR/avoid-ice-on-warning-3.rs:12:14
9176
|
9277
LL | trait A { fn g(b: B) -> B; }
9378
| - ^ ...because associated function `g` has no `self` parameter
@@ -103,32 +88,27 @@ LL | trait A { fn g(b: B) -> B where Self: Sized; }
10388
| +++++++++++++++++
10489

10590
warning: trait objects without an explicit `dyn` are deprecated
106-
--> $DIR/avoid-ice-on-warning-3.rs:9:19
91+
--> $DIR/avoid-ice-on-warning-3.rs:12:19
10792
|
10893
LL | trait A { fn g(b: B) -> B; }
10994
| ^
11095
|
11196
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
11297
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
113-
= note: `B` it is not object safe, so it can't be `dyn`
11498
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
115-
help: use a new generic type parameter, constrained by `B`
116-
|
117-
LL | trait A { fn g<T: B>(b: T) -> B; }
118-
| ++++++ ~
119-
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
99+
help: use `dyn`
120100
|
121-
LL | trait A { fn g(b: impl B) -> B; }
122-
| ++++
101+
LL | trait A { fn g(b: dyn B) -> B; }
102+
| +++
123103

124104
error[E0038]: the trait `B` cannot be made into an object
125-
--> $DIR/avoid-ice-on-warning-3.rs:9:19
105+
--> $DIR/avoid-ice-on-warning-3.rs:12:19
126106
|
127107
LL | trait A { fn g(b: B) -> B; }
128108
| ^ `B` cannot be made into an object
129109
|
130110
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>
131-
--> $DIR/avoid-ice-on-warning-3.rs:1:14
111+
--> $DIR/avoid-ice-on-warning-3.rs:4:14
132112
|
133113
LL | trait B { fn f(a: A) -> A; }
134114
| - ^ ...because associated function `f` has no `self` parameter
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
// revisions: old new
2+
//[old] edition:2015
3+
//[new] edition:2021
14
trait B { fn f(a: A) -> A; }
2-
//~^ WARN trait objects without an explicit `dyn` are deprecated
3-
//~| WARN trait objects without an explicit `dyn` are deprecated
4-
//~| WARN trait objects without an explicit `dyn` are deprecated
5-
//~| WARN this is accepted in the current edition
6-
//~| WARN this is accepted in the current edition
7-
//~| WARN this is accepted in the current edition
8-
//~| ERROR the trait `A` cannot be made into an object
5+
//~^ ERROR the trait `A` cannot be made into an object
6+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
7+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
8+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
9+
//[old]~| WARN this is accepted in the current edition
10+
//[old]~| WARN this is accepted in the current edition
11+
//[old]~| WARN this is accepted in the current edition
912
trait A { fn g(b: B) -> B; }
10-
//~^ WARN trait objects without an explicit `dyn` are deprecated
11-
//~| WARN trait objects without an explicit `dyn` are deprecated
12-
//~| WARN trait objects without an explicit `dyn` are deprecated
13-
//~| WARN this is accepted in the current edition
14-
//~| WARN this is accepted in the current edition
15-
//~| WARN this is accepted in the current edition
16-
//~| ERROR the trait `B` cannot be made into an object
13+
//~^ ERROR the trait `B` cannot be made into an object
14+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
15+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
16+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
17+
//[old]~| WARN this is accepted in the current edition
18+
//[old]~| WARN this is accepted in the current edition
19+
//[old]~| WARN this is accepted in the current edition
1720
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: return types are denoted using `->`
2+
--> $DIR/avoid-ice-on-warning.rs:4:23
3+
|
4+
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
5+
| ^ help: use `->` instead
6+
7+
error[E0405]: cannot find trait `call_that` in this scope
8+
--> $DIR/avoid-ice-on-warning.rs:4:36
9+
|
10+
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
11+
| ^^^^^^^^^ not found in this scope
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0405`.

‎tests/ui/object-safety/avoid-ice-on-warning.stderr ‎tests/ui/object-safety/avoid-ice-on-warning.old.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
error: return types are denoted using `->`
2-
--> $DIR/avoid-ice-on-warning.rs:1:23
2+
--> $DIR/avoid-ice-on-warning.rs:4:23
33
|
44
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
55
| ^ help: use `->` instead
66

77
error[E0405]: cannot find trait `call_that` in this scope
8-
--> $DIR/avoid-ice-on-warning.rs:1:36
8+
--> $DIR/avoid-ice-on-warning.rs:4:36
99
|
1010
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
1111
| ^^^^^^^^^ not found in this scope
1212

1313
warning: trait objects without an explicit `dyn` are deprecated
14-
--> $DIR/avoid-ice-on-warning.rs:1:25
14+
--> $DIR/avoid-ice-on-warning.rs:4:25
1515
|
1616
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
1717
| ^^^^^^^^^^^^^^^^^^^^
1818
|
1919
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2020
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2121
= note: `#[warn(bare_trait_objects)]` on by default
22-
help: `Fn(&str) + call_that` is not object safe, use `impl Fn(&str) + call_that` to return an opaque type, as long as you return a single underlying type
22+
help: use `dyn`
2323
|
24-
LL | fn call_this<F>(f: F) : impl Fn(&str) + call_that {}
25-
| ++++
24+
LL | fn call_this<F>(f: F) : dyn Fn(&str) + call_that {}
25+
| +++
2626

2727
error: aborting due to 2 previous errors; 1 warning emitted
2828

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
// revisions: old new
2+
//[old] edition:2015
3+
//[new] edition:2021
14
fn call_this<F>(f: F) : Fn(&str) + call_that {}
25
//~^ ERROR return types are denoted using `->`
36
//~| ERROR cannot find trait `call_that` in this scope
4-
//~| WARN trait objects without an explicit `dyn` are deprecated
5-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
7+
//[old]~| WARN trait objects without an explicit `dyn` are deprecated
8+
//[old]~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
69
fn main() {}

‎tests/ui/object-safety/bare-trait-dont-suggest-dyn.fixed

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// revisions: old new
2+
//[old] edition:2015
3+
//[new] edition:2021
4+
//[new] run-rustfix
5+
// FIXME: the test suite tries to create a crate called `bare_trait_dont_suggest_dyn.new`
6+
#![crate_name="bare_trait_dont_suggest_dyn"]
7+
#![deny(bare_trait_objects)]
8+
fn ord_prefer_dot(s: String) -> impl Ord {
9+
//~^ ERROR the trait `Ord` cannot be made into an object
10+
//[old]~| ERROR trait objects without an explicit `dyn` are deprecated
11+
//[old]~| WARNING this is accepted in the current edition (Rust 2015)
12+
(s.starts_with("."), s)
13+
}
14+
fn main() {
15+
let _ = ord_prefer_dot(String::new());
16+
}

‎tests/ui/object-safety/bare-trait-dont-suggest-dyn.stderr ‎tests/ui/object-safety/bare-trait-dont-suggest-dyn.new.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0038]: the trait `Ord` cannot be made into an object
2-
--> $DIR/bare-trait-dont-suggest-dyn.rs:3:33
2+
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
33
|
44
LL | fn ord_prefer_dot(s: String) -> Ord {
55
| ^^^ `Ord` cannot be made into an object
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: trait objects without an explicit `dyn` are deprecated
2+
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
3+
|
4+
LL | fn ord_prefer_dot(s: String) -> Ord {
5+
| ^^^
6+
|
7+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9+
note: the lint level is defined here
10+
--> $DIR/bare-trait-dont-suggest-dyn.rs:7:9
11+
|
12+
LL | #![deny(bare_trait_objects)]
13+
| ^^^^^^^^^^^^^^^^^^
14+
help: use `dyn`
15+
|
16+
LL | fn ord_prefer_dot(s: String) -> dyn Ord {
17+
| +++
18+
19+
error[E0038]: the trait `Ord` cannot be made into an object
20+
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
21+
|
22+
LL | fn ord_prefer_dot(s: String) -> Ord {
23+
| ^^^ `Ord` cannot be made into an object
24+
|
25+
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>
26+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
27+
|
28+
= note: the trait cannot be made into an object because it uses `Self` as a type parameter
29+
::: $SRC_DIR/core/src/cmp.rs:LL:COL
30+
|
31+
= note: the trait cannot be made into an object because it uses `Self` as a type parameter
32+
help: consider using an opaque type instead
33+
|
34+
LL | fn ord_prefer_dot(s: String) -> impl Ord {
35+
| ++++
36+
37+
error: aborting due to 2 previous errors
38+
39+
For more information about this error, try `rustc --explain E0038`.

‎tests/ui/object-safety/bare-trait-dont-suggest-dyn.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
// run-rustfix
1+
// revisions: old new
2+
//[old] edition:2015
3+
//[new] edition:2021
4+
//[new] run-rustfix
5+
// FIXME: the test suite tries to create a crate called `bare_trait_dont_suggest_dyn.new`
6+
#![crate_name="bare_trait_dont_suggest_dyn"]
27
#![deny(bare_trait_objects)]
38
fn ord_prefer_dot(s: String) -> Ord {
49
//~^ ERROR the trait `Ord` cannot be made into an object
10+
//[old]~| ERROR trait objects without an explicit `dyn` are deprecated
11+
//[old]~| WARNING this is accepted in the current edition (Rust 2015)
512
(s.starts_with("."), s)
613
}
714
fn main() {

‎tests/ui/traits/bound/not-on-bare-trait.stderr

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@ LL | fn foo(_x: Foo + Send) {
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use a new generic type parameter, constrained by `Foo + Send`
10+
help: use `dyn`
1111
|
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-
| +++++ +
12+
LL | fn foo(_x: dyn Foo + Send) {
13+
| +++
2214

2315
error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
2416
--> $DIR/not-on-bare-trait.rs:7:8

0 commit comments

Comments
 (0)
Please sign in to comment.