Skip to content

Commit 88f5e11

Browse files
committed
review comments
1 parent ae0e3d0 commit 88f5e11

File tree

55 files changed

+133
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+133
-132
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn report_object_safety_error(
5454
"the trait `{}` cannot be made into an object",
5555
trait_str
5656
);
57-
err.span_label(span, format!("the trait `{}` cannot be made into an object", trait_str));
57+
err.span_label(span, format!("`{}` cannot be made into an object", trait_str));
5858

5959
let mut reported_violations = FxHashSet::default();
6060
let mut multi_span = vec![];

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl ObjectSafetyViolation {
647647
ObjectSafetyViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
648648
ObjectSafetyViolation::SupertraitSelf(ref spans) => {
649649
if spans.iter().any(|sp| *sp != DUMMY_SP) {
650-
"it uses `Self` as a type parameter in this".into()
650+
"it uses `Self` as a type parameter".into()
651651
} else {
652652
"it cannot use `Self` as a type parameter in a supertrait or `where`-clause"
653653
.into()

compiler/rustc_trait_selection/src/traits/object_safety.rs

+46-45
Original file line numberDiff line numberDiff line change
@@ -100,51 +100,7 @@ fn object_safety_violations_for_trait(
100100
span,
101101
) = violation
102102
{
103-
// Using `CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
104-
// It's also hard to get a use site span, so we use the method definition span.
105-
tcx.struct_span_lint_hir(
106-
WHERE_CLAUSES_OBJECT_SAFETY,
107-
hir::CRATE_HIR_ID,
108-
*span,
109-
|lint| {
110-
let mut err = lint.build(&format!(
111-
"the trait `{}` cannot be made into an object",
112-
tcx.def_path_str(trait_def_id)
113-
));
114-
let node = tcx.hir().get_if_local(trait_def_id);
115-
let mut spans = MultiSpan::from_span(*span);
116-
if let Some(hir::Node::Item(item)) = node {
117-
spans.push_span_label(
118-
item.ident.span,
119-
"this trait cannot be made into an object...".into(),
120-
);
121-
spans.push_span_label(
122-
*span,
123-
format!("...because {}", violation.error_msg()),
124-
);
125-
} else {
126-
spans.push_span_label(
127-
*span,
128-
format!(
129-
"the trait cannot be made into an object because {}",
130-
violation.error_msg()
131-
),
132-
);
133-
};
134-
err.span_note(
135-
spans,
136-
"for a trait to be \"object safe\" it needs to allow building a vtable \
137-
to allow the call to be resolvable dynamically; for more information \
138-
visit <https://doc.rust-lang.org/reference/items/traits.html\
139-
#object-safety>",
140-
);
141-
if node.is_some() {
142-
// Only provide the help if its a local trait, otherwise it's not
143-
violation.solution(&mut err);
144-
}
145-
err.emit();
146-
},
147-
);
103+
lint_object_unsafe_trait(tcx, *span, trait_def_id, violation);
148104
false
149105
} else {
150106
true
@@ -182,6 +138,51 @@ fn object_safety_violations_for_trait(
182138
violations
183139
}
184140

141+
/// Lint object-unsafe trait.
142+
fn lint_object_unsafe_trait(
143+
tcx: TyCtxt<'_>,
144+
span: Span,
145+
trait_def_id: DefId,
146+
violation: &ObjectSafetyViolation,
147+
) {
148+
// Using `CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
149+
// It's also hard to get a use site span, so we use the method definition span.
150+
tcx.struct_span_lint_hir(WHERE_CLAUSES_OBJECT_SAFETY, hir::CRATE_HIR_ID, span, |lint| {
151+
let mut err = lint.build(&format!(
152+
"the trait `{}` cannot be made into an object",
153+
tcx.def_path_str(trait_def_id)
154+
));
155+
let node = tcx.hir().get_if_local(trait_def_id);
156+
let mut spans = MultiSpan::from_span(span);
157+
if let Some(hir::Node::Item(item)) = node {
158+
spans.push_span_label(
159+
item.ident.span,
160+
"this trait cannot be made into an object...".into(),
161+
);
162+
spans.push_span_label(span, format!("...because {}", violation.error_msg()));
163+
} else {
164+
spans.push_span_label(
165+
span,
166+
format!(
167+
"the trait cannot be made into an object because {}",
168+
violation.error_msg()
169+
),
170+
);
171+
};
172+
err.span_note(
173+
spans,
174+
"for a trait to be \"object safe\" it needs to allow building a vtable to allow the \
175+
call to be resolvable dynamically; for more information visit \
176+
<https://doc.rust-lang.org/reference/items/traits.html#object-safety>",
177+
);
178+
if node.is_some() {
179+
// Only provide the help if its a local trait, otherwise it's not
180+
violation.solution(&mut err);
181+
}
182+
err.emit();
183+
});
184+
}
185+
185186
fn sized_trait_bound_spans<'tcx>(
186187
tcx: TyCtxt<'tcx>,
187188
bounds: hir::GenericBounds<'tcx>,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Trait` cannot be made into an object
22
--> $DIR/associated-const-in-trait.rs:9:6
33
|
44
LL | impl dyn Trait {
5-
| ^^^^^^^^^ the trait `Trait` cannot be made into an object
5+
| ^^^^^^^^^ `Trait` cannot be made into an object
66
|
77
= help: consider moving `N` to another trait
88
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>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Bar` cannot be made into an object
22
--> $DIR/issue-48027.rs:6:6
33
|
44
LL | impl dyn Bar {}
5-
| ^^^^^^^ the trait `Bar` cannot be made into an object
5+
| ^^^^^^^ `Bar` cannot be made into an object
66
|
77
= help: consider moving `X` to another trait
88
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>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `NotObjectSafe` cannot be made into an object
22
--> $DIR/coherence-impl-trait-for-trait-object-safe.rs:7:24
33
|
44
LL | impl NotObjectSafe for dyn NotObjectSafe { }
5-
| ^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^ `NotObjectSafe` cannot be made into an object
66
|
77
= help: consider moving `eq` to another trait
88
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>

src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0038]: the trait `Copy` cannot be made into an object
1414
--> $DIR/trait-object-reference-without-parens-suggestion.rs:4:12
1515
|
1616
LL | let _: &Copy + 'static;
17-
| ^^^^^ the trait `Copy` cannot be made into an object
17+
| ^^^^^ `Copy` cannot be made into an object
1818
|
1919
= note: the trait cannot be made into an object because it requires `Self: Sized`
2020
= 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>

src/test/ui/error-codes/E0033-teach.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0038]: the trait `SomeTrait` cannot be made into an object
88
--> $DIR/E0033-teach.rs:8:20
99
|
1010
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
11-
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
11+
| ^^^^^^^^^^^^^^ `SomeTrait` cannot be made into an object
1212
|
1313
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>
1414
--> $DIR/E0033-teach.rs:4:8

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0038]: the trait `SomeTrait` cannot be made into an object
88
--> $DIR/E0033.rs:6:20
99
|
1010
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
11-
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
11+
| ^^^^^^^^^^^^^^ `SomeTrait` cannot be made into an object
1212
|
1313
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>
1414
--> $DIR/E0033.rs:2:8

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Trait` cannot be made into an object
22
--> $DIR/E0038.rs:5:16
33
|
44
LL | fn call_foo(x: Box<dyn Trait>) {
5-
| ^^^^^^^^^^^^^^ the trait `Trait` cannot be made into an object
5+
| ^^^^^^^^^^^^^^ `Trait` cannot be made into an object
66
|
77
= help: consider moving `foo` to another trait
88
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>

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
22
--> $DIR/feature-gate-object_safe_for_dispatch.rs:18:38
33
|
44
LL | fn takes_non_object_safe_ref<T>(obj: &dyn NonObjectSafe1) {
5-
| ^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe1` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^^^ `NonObjectSafe1` cannot be made into an object
66
|
77
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>
88
--> $DIR/feature-gate-object_safe_for_dispatch.rs:4:23
@@ -16,7 +16,7 @@ error[E0038]: the trait `NonObjectSafe2` cannot be made into an object
1616
--> $DIR/feature-gate-object_safe_for_dispatch.rs:22:36
1717
|
1818
LL | fn return_non_object_safe_ref() -> &'static dyn NonObjectSafe2 {
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe2` cannot be made into an object
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NonObjectSafe2` cannot be made into an object
2020
|
2121
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>
2222
--> $DIR/feature-gate-object_safe_for_dispatch.rs:7:8
@@ -38,7 +38,7 @@ error[E0038]: the trait `NonObjectSafe3` cannot be made into an object
3838
--> $DIR/feature-gate-object_safe_for_dispatch.rs:27:35
3939
|
4040
LL | fn takes_non_object_safe_box(obj: Box<dyn NonObjectSafe3>) {
41-
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe3` cannot be made into an object
41+
| ^^^^^^^^^^^^^^^^^^^^^^^ `NonObjectSafe3` cannot be made into an object
4242
|
4343
= help: consider moving `foo` to another trait
4444
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>
@@ -53,7 +53,7 @@ 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> {
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe4` cannot be made into an object
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NonObjectSafe4` cannot be made into an object
5757
|
5858
= help: consider moving `foo` to another trait
5959
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>
@@ -68,7 +68,7 @@ error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
6868
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:16
6969
|
7070
LL | impl Trait for dyn NonObjectSafe1 {}
71-
| ^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe1` cannot be made into an object
71+
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe1` cannot be made into an object
7272
|
7373
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>
7474
--> $DIR/feature-gate-object_safe_for_dispatch.rs:4:23

src/test/ui/impl-trait/object-unsafe-trait-in-return-position-dyn-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `NotObjectSafe` cannot be made into an object
22
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:21:13
33
|
44
LL | fn car() -> dyn NotObjectSafe {
5-
| ^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^ `NotObjectSafe` cannot be made into an object
66
|
77
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>
88
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:3:8
@@ -24,7 +24,7 @@ error[E0038]: the trait `NotObjectSafe` cannot be made into an object
2424
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:28:13
2525
|
2626
LL | fn cat() -> Box<dyn NotObjectSafe> {
27-
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
27+
| ^^^^^^^^^^^^^^^^^^^^^^ `NotObjectSafe` cannot be made into an object
2828
|
2929
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>
3030
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:3:8

src/test/ui/issues/issue-18959.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Bar` cannot be made into an object
22
--> $DIR/issue-18959.rs:11:11
33
|
44
LL | fn foo(b: &dyn Bar) {
5-
| ^^^^^^^^ the trait `Bar` cannot be made into an object
5+
| ^^^^^^^^ `Bar` cannot be made into an object
66
|
77
= help: consider moving `foo` to another trait
88
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>

src/test/ui/issues/issue-19380.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Qiz` cannot be made into an object
22
--> $DIR/issue-19380.rs:11:9
33
|
44
LL | foos: &'static [&'static (dyn Qiz + 'static)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Qiz` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Qiz` cannot be made into an object
66
|
77
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>
88
--> $DIR/issue-19380.rs:2:6

src/test/ui/issues/issue-19538.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Bar` cannot be made into an object
22
--> $DIR/issue-19538.rs:17:15
33
|
44
LL | let test: &mut dyn Bar = &mut thing;
5-
| ^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
5+
| ^^^^^^^^^^^^ `Bar` cannot be made into an object
66
|
77
= help: consider moving `foo` to another trait
88
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>
@@ -18,7 +18,7 @@ error[E0038]: the trait `Bar` cannot be made into an object
1818
--> $DIR/issue-19538.rs:17:30
1919
|
2020
LL | let test: &mut dyn Bar = &mut thing;
21-
| ^^^^^^^^^^ the trait `Bar` cannot be made into an object
21+
| ^^^^^^^^^^ `Bar` cannot be made into an object
2222
|
2323
= help: consider moving `foo` to another trait
2424
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>

src/test/ui/issues/issue-20692.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `Array` cannot be made into an object
22
--> $DIR/issue-20692.rs:7:5
33
|
44
LL | &dyn Array;
5-
| ^^^^^^^^^^ the trait `Array` cannot be made into an object
5+
| ^^^^^^^^^^ `Array` cannot be made into an object
66
|
77
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>
88
--> $DIR/issue-20692.rs:1:14
@@ -17,7 +17,7 @@ error[E0038]: the trait `Array` cannot be made into an object
1717
--> $DIR/issue-20692.rs:4:13
1818
|
1919
LL | let _ = x
20-
| ^ the trait `Array` cannot be made into an object
20+
| ^ `Array` cannot be made into an object
2121
|
2222
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>
2323
--> $DIR/issue-20692.rs:1:14

src/test/ui/issues/issue-26056.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0038]: the trait `Map` cannot be made into an object
22
--> $DIR/issue-26056.rs:20:13
33
|
44
LL | as &dyn Map<Key=u32,MapValue=u32>;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Map` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Map` cannot be made into an object
66
|
77
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>
88
--> $DIR/issue-26056.rs:9:12
99
|
1010
LL | trait Map: MapLookup<<Self as Map>::Key> {
11-
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because it uses `Self` as a type parameter in this
11+
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because it uses `Self` as a type parameter
1212
| |
1313
| this trait cannot be made into an object...
1414

src/test/ui/issues/issue-28576.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ error[E0038]: the trait `Bar` cannot be made into an object
33
|
44
LL | / dyn Bar
55
LL | | <Assoc=()>
6-
| |________________________^ the trait `Bar` cannot be made into an object
6+
| |________________________^ `Bar` cannot be made into an object
77
|
88
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>
99
--> $DIR/issue-28576.rs:5:16
1010
|
1111
LL | pub trait Bar: Foo<Assoc=()> {
1212
| --- ^^^^^^^^^^^^^
1313
| | | |
14-
| | | ...because it uses `Self` as a type parameter in this
15-
| | ...because it uses `Self` as a type parameter in this
14+
| | | ...because it uses `Self` as a type parameter
15+
| | ...because it uses `Self` as a type parameter
1616
| this trait cannot be made into an object...
1717

1818
error: aborting due to previous error

0 commit comments

Comments
 (0)