Skip to content

Commit 40f2a3b

Browse files
committed
Move object safety suggestions to the end of the error
1 parent c7dbe7a commit 40f2a3b

28 files changed

+50
-48
lines changed

compiler/rustc_infer/src/traits/error_reporting/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ pub fn report_object_safety_error(
8383
messages.push(msg.clone());
8484
}
8585
}
86-
if trait_span.is_some() {
87-
// Only provide the help if its a local trait, otherwise it's not actionable.
88-
violation.solution(&mut err);
89-
}
9086
}
9187
}
9288
let has_multi_span = !multi_span.is_empty();
@@ -104,5 +100,11 @@ pub fn report_object_safety_error(
104100
to be resolvable dynamically; for more information visit \
105101
<https://doc.rust-lang.org/reference/items/traits.html#object-safety>",
106102
);
103+
if trait_span.is_some() {
104+
for violation in reported_violations {
105+
// Only provide the help if its a local trait, otherwise it's not actionable.
106+
violation.solution(&mut err);
107+
}
108+
}
107109
err
108110
}

src/test/ui/associated-consts/associated-const-in-trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `Trait` cannot be made into an object
44
LL | impl dyn Trait {
55
| ^^^^^^^^^ `Trait` cannot be made into an object
66
|
7-
= help: consider moving `N` to another trait
87
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>
98
--> $DIR/associated-const-in-trait.rs:6:11
109
|
1110
LL | trait Trait {
1211
| ----- this trait cannot be made into an object...
1312
LL | const N: usize;
1413
| ^ ...because it contains this associated `const`
14+
= help: consider moving `N` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/associated-item/issue-48027.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ error[E0038]: the trait `Bar` cannot be made into an object
2121
LL | impl dyn Bar {}
2222
| ^^^^^^^ `Bar` cannot be made into an object
2323
|
24-
= help: consider moving `X` to another trait
2524
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>
2625
--> $DIR/issue-48027.rs:2:11
2726
|
2827
LL | trait Bar {
2928
| --- this trait cannot be made into an object...
3029
LL | const X: usize;
3130
| ^ ...because it contains this associated `const`
31+
= help: consider moving `X` to another trait
3232

3333
error: aborting due to 2 previous errors
3434

src/test/ui/coherence/coherence-impl-trait-for-trait-object-safe.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `NotObjectSafe` cannot be made into an object
44
LL | impl NotObjectSafe for dyn NotObjectSafe { }
55
| ^^^^^^^^^^^^^^^^^ `NotObjectSafe` cannot be made into an object
66
|
7-
= help: consider moving `eq` to another trait
87
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>
98
--> $DIR/coherence-impl-trait-for-trait-object-safe.rs:6:43
109
|
1110
LL | trait NotObjectSafe { fn eq(&self, other: Self); }
1211
| ------------- ^^^^ ...because method `eq` references the `Self` type in this parameter
1312
| |
1413
| this trait cannot be made into an object...
14+
= help: consider moving `eq` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/const-generics/generic_const_exprs/object-safety-err-ret.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `Foo` cannot be made into an object
44
LL | fn use_dyn(v: &dyn Foo) {
55
| ^^^^^^^ `Foo` cannot be made into an object
66
|
7-
= help: consider moving `test` to another trait
87
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>
98
--> $DIR/object-safety-err-ret.rs:8:23
109
|
1110
LL | trait Foo {
1211
| --- this trait cannot be made into an object...
1312
LL | fn test(&self) -> [u8; bar::<Self>()];
1413
| ^^^^^^^^^^^^^^^^^^^ ...because method `test` references the `Self` type in its return type
14+
= help: consider moving `test` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/error-codes/E0038.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `Trait` cannot be made into an object
44
LL | fn call_foo(x: Box<dyn Trait>) {
55
| ^^^^^^^^^ `Trait` cannot be made into an object
66
|
7-
= help: consider moving `foo` to another trait
87
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>
98
--> $DIR/E0038.rs:2:22
109
|
1110
LL | trait Trait {
1211
| ----- this trait cannot be made into an object...
1312
LL | fn foo(&self) -> Self;
1413
| ^^^^ ...because method `foo` references the `Self` type in its return type
14+
= help: consider moving `foo` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/feature-gates/feature-gate-object_safe_for_dispatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,29 @@ error[E0038]: the trait `NonObjectSafe3` cannot be made into an object
4040
LL | fn takes_non_object_safe_box(obj: Box<dyn NonObjectSafe3>) {
4141
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe3` cannot be made into an object
4242
|
43-
= help: consider moving `foo` to another trait
4443
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>
4544
--> $DIR/feature-gate-object_safe_for_dispatch.rs:11:8
4645
|
4746
LL | trait NonObjectSafe3 {
4847
| -------------- this trait cannot be made into an object...
4948
LL | fn foo<T>(&self);
5049
| ^^^ ...because method `foo` has generic type parameters
50+
= help: consider moving `foo` to another trait
5151

5252
error[E0038]: the trait `NonObjectSafe4` cannot be made into an object
5353
--> $DIR/feature-gate-object_safe_for_dispatch.rs:31:35
5454
|
5555
LL | fn return_non_object_safe_rc() -> std::rc::Rc<dyn NonObjectSafe4> {
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NonObjectSafe4` cannot be made into an object
5757
|
58-
= help: consider moving `foo` to another trait
5958
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>
6059
--> $DIR/feature-gate-object_safe_for_dispatch.rs:15:22
6160
|
6261
LL | trait NonObjectSafe4 {
6362
| -------------- this trait cannot be made into an object...
6463
LL | fn foo(&self, s: &Self);
6564
| ^^^^^ ...because method `foo` references the `Self` type in this parameter
65+
= help: consider moving `foo` to another trait
6666

6767
error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
6868
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:16

src/test/ui/generic-associated-types/gat-in-trait-path.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `Foo` cannot be made into an object
44
LL | fn f(_arg : Box<dyn for<'a> Foo<A<'a> = &'a ()>>) {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
66
|
7-
= help: consider moving `A` to another trait
87
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>
98
--> $DIR/gat-in-trait-path.rs:5:10
109
|
1110
LL | trait Foo {
1211
| --- this trait cannot be made into an object...
1312
LL | type A<'a> where Self: 'a;
1413
| ^ ...because it contains the generic associated type `A`
14+
= help: consider moving `A` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/generic-associated-types/issue-67510-pass.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0038]: the trait `X` cannot be made into an object
44
LL | fn _func1<'a>(_x: Box<dyn X<Y<'a>=&'a ()>>) {}
55
| ^^^^^^^^^^^^^^^^^^^ `X` cannot be made into an object
66
|
7-
= help: consider moving `Y` to another trait
87
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>
98
--> $DIR/issue-67510-pass.rs:4:10
109
|
1110
LL | trait X {
1211
| - this trait cannot be made into an object...
1312
LL | type Y<'a>;
1413
| ^ ...because it contains the generic associated type `Y`
14+
= help: consider moving `Y` to another trait
1515

1616
error: aborting due to previous error
1717

src/test/ui/generic-associated-types/issue-76535.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@ error[E0038]: the trait `SuperTrait` cannot be made into an object
2020
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
2222
|
23-
= help: consider moving `SubType` to another trait
2423
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>
2524
--> $DIR/issue-76535.rs:6:10
2625
|
2726
LL | pub trait SuperTrait {
2827
| ---------- this trait cannot be made into an object...
2928
LL | type SubType<'a>: SubTrait;
3029
| ^^^^^^^ ...because it contains the generic associated type `SubType`
30+
= help: consider moving `SubType` to another trait
3131

3232
error[E0038]: the trait `SuperTrait` cannot be made into an object
3333
--> $DIR/issue-76535.rs:36:57
3434
|
3535
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
3737
|
38-
= help: consider moving `SubType` to another trait
3938
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>
4039
--> $DIR/issue-76535.rs:6:10
4140
|
4241
LL | pub trait SuperTrait {
4342
| ---------- this trait cannot be made into an object...
4443
LL | type SubType<'a>: SubTrait;
4544
| ^^^^^^^ ...because it contains the generic associated type `SubType`
45+
= help: consider moving `SubType` to another trait
4646
= note: required because of the requirements on the impl of `CoerceUnsized<Box<dyn SuperTrait<SubType = SubStruct<'_>>>>` for `Box<SuperStruct>`
4747
= note: required by cast to type `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
4848

0 commit comments

Comments
 (0)