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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 46 additions & 45 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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>

0 commit comments

Comments
 (0)