Wording changes to object unsafe trait errors

Stemming from the thread at https://twitter.com/indygreg/status/1223279056398929920
This commit is contained in:
Esteban Küber 2020-01-31 16:47:00 -08:00
parent 3ca1c5d5b5
commit 413bfa4b98
51 changed files with 332 additions and 91 deletions

View File

@ -1034,6 +1034,10 @@ pub fn report_object_safety_error(
violations: Vec<ObjectSafetyViolation>,
) -> DiagnosticBuilder<'tcx> {
let trait_str = tcx.def_path_str(trait_def_id);
let trait_span = tcx.hir().get_if_local(trait_def_id).and_then(|node| match node {
hir::Node::Item(item) => Some(item.ident.span),
_ => None,
});
let span = tcx.sess.source_map().def_span(span);
let mut err = struct_span_err!(
tcx.sess,
@ -1045,6 +1049,7 @@ pub fn report_object_safety_error(
err.span_label(span, format!("the trait `{}` cannot be made into an object", trait_str));
let mut reported_violations = FxHashSet::default();
let mut had_span_label = false;
for violation in violations {
if let ObjectSafetyViolation::SizedSelf(sp) = &violation {
if !sp.is_empty() {
@ -1055,15 +1060,28 @@ pub fn report_object_safety_error(
}
if reported_violations.insert(violation.clone()) {
let spans = violation.spans();
let msg = if trait_span.is_none() || spans.is_empty() {
format!("the trait cannot be made into an object because {}", violation.error_msg())
} else {
had_span_label = true;
format!("...because {}", violation.error_msg())
};
if spans.is_empty() {
err.note(&violation.error_msg());
err.note(&msg);
} else {
for span in spans {
err.span_label(span, violation.error_msg());
err.span_label(span, &msg);
}
}
if let (Some(_), Some(note)) = (trait_span, violation.solution()) {
// Only provide the help if its a local trait, otherwise it's not actionable.
err.help(&note);
}
}
}
if let (Some(trait_span), true) = (trait_span, had_span_label) {
err.span_label(trait_span, "this trait cannot be made into an object...");
}
if tcx.sess.trait_methods_not_found.borrow().contains(&span) {
// Avoid emitting error caused by non-existing method (#58734)

View File

@ -43,12 +43,9 @@ pub enum ObjectSafetyViolation {
impl ObjectSafetyViolation {
pub fn error_msg(&self) -> Cow<'static, str> {
match *self {
ObjectSafetyViolation::SizedSelf(_) => {
"traits that require `Self: Sized` cannot be made into an object".into()
}
ObjectSafetyViolation::SizedSelf(_) => "it requires `Self: Sized`".into(),
ObjectSafetyViolation::SupertraitSelf => {
"the trait cannot use `Self` as a type parameter \
in the supertraits or where-clauses"
"it cannot use `Self` as a type parameter in the supertraits or `where`-clauses"
.into()
}
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod, _) => {
@ -63,19 +60,45 @@ impl ObjectSafetyViolation {
name,
MethodViolationCode::WhereClauseReferencesSelf,
_,
) => format!("method `{}` references the `Self` type in where clauses", name).into(),
) => {
format!("method `{}` references the `Self` type in its `where` clause", name).into()
}
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic, _) => {
format!("method `{}` has generic type parameters", name).into()
}
ObjectSafetyViolation::Method(name, MethodViolationCode::UndispatchableReceiver, _) => {
format!("method `{}`'s `self` parameter cannot be dispatched on", name).into()
}
ObjectSafetyViolation::AssocConst(_, DUMMY_SP) => {
"it cannot contain associated consts".into()
}
ObjectSafetyViolation::AssocConst(name, _) => {
format!("the trait cannot contain associated consts like `{}`", name).into()
format!("it cannot contain associated consts like `{}`", name).into()
}
}
}
pub fn solution(&self) -> Option<String> {
Some(match *self {
ObjectSafetyViolation::SizedSelf(_) | ObjectSafetyViolation::SupertraitSelf => {
return None;
}
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod, _) => format!(
"consider turning `{}` into a method by giving it a `&self` argument or \
constraining it with `where Self: Sized`",
name
),
ObjectSafetyViolation::Method(name, MethodViolationCode::UndispatchableReceiver, _) => {
format!("consider changing method `{}`'s `self` parameter to be `&self`", name)
.into()
}
ObjectSafetyViolation::AssocConst(name, _)
| ObjectSafetyViolation::Method(name, ..) => {
format!("consider moving `{}` to another trait", name)
}
})
}
pub fn spans(&self) -> SmallVec<[Span; 1]> {
// When `span` comes from a separate crate, it'll be `DUMMY_SP`. Treat it as `None` so
// diagnostics use a `note` instead of a `span_label`.
@ -190,7 +213,21 @@ fn object_safety_violations_for_trait(
tcx.def_path_str(trait_def_id)
),
);
err.span_label(*span, violation.error_msg());
let node = tcx.hir().get_if_local(trait_def_id);
let msg = if let Some(hir::Node::Item(item)) = node {
err.span_label(item.ident.span, "this trait cannot be made into an object...");
format!("...because {}", violation.error_msg())
} else {
format!(
"the trait cannot be made into an object because {}",
violation.error_msg()
)
};
err.span_label(*span, &msg);
if let (Some(_), Some(note)) = (node, violation.solution()) {
// Only provide the help if its a local trait, otherwise it's not actionable.
err.help(&note);
}
err.emit();
false
} else {

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/associated-const-in-trait.rs:9:6
|
LL | trait Trait {
| ----- this trait cannot be made into an object...
LL | const N: usize;
| - the trait cannot contain associated consts like `N`
| - ...because it cannot contain associated consts like `N`
...
LL | impl dyn Trait {
| ^^^^^^^^^ the trait `Trait` cannot be made into an object
|
= help: consider moving `N` to another trait
error: aborting due to previous error

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/issue-48027.rs:6:6
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | const X: usize;
| - the trait cannot contain associated consts like `X`
| - ...because it cannot contain associated consts like `X`
...
LL | impl dyn Bar {}
| ^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `X` to another trait
error[E0283]: type annotations needed
--> $DIR/issue-48027.rs:3:32

View File

@ -2,9 +2,13 @@ error[E0038]: the trait `NotObjectSafe` cannot be made into an object
--> $DIR/coherence-impl-trait-for-trait-object-safe.rs:7:6
|
LL | trait NotObjectSafe { fn eq(&self, other: Self); }
| -- method `eq` references the `Self` type in its parameters or return type
| ------------- -- ...because method `eq` references the `Self` type in its parameters or return type
| |
| this trait cannot be made into an object...
LL | impl NotObjectSafe for dyn NotObjectSafe { }
| ^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
|
= help: consider moving `eq` to another trait
error: aborting due to previous error

View File

@ -16,7 +16,7 @@ error[E0038]: the trait `std::marker::Copy` cannot be made into an object
LL | let _: &Copy + 'static;
| ^^^^^ the trait `std::marker::Copy` cannot be made into an object
|
= note: traits that require `Self: Sized` cannot be made into an object
= note: the trait cannot be made into an object because it requires `Self: Sized`
error: aborting due to 3 previous errors

View File

@ -7,11 +7,15 @@ LL | let trait_obj: &dyn SomeTrait = SomeTrait;
error[E0038]: the trait `SomeTrait` cannot be made into an object
--> $DIR/E0033-teach.rs:8:20
|
LL | trait SomeTrait {
| --------- this trait cannot be made into an object...
LL | fn foo();
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
--> $DIR/E0033-teach.rs:12:9

View File

@ -7,11 +7,15 @@ LL | let trait_obj: &dyn SomeTrait = SomeTrait;
error[E0038]: the trait `SomeTrait` cannot be made into an object
--> $DIR/E0033.rs:6:20
|
LL | trait SomeTrait {
| --------- this trait cannot be made into an object...
LL | fn foo();
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
| ^^^^^^^^^^^^^^ the trait `SomeTrait` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
--> $DIR/E0033.rs:10:9

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/E0038.rs:5:16
|
LL | trait Trait {
| ----- this trait cannot be made into an object...
LL | fn foo(&self) -> Self;
| --- method `foo` references the `Self` type in its parameters or return type
| --- ...because method `foo` references the `Self` type in its parameters or return type
...
LL | fn call_foo(x: Box<dyn Trait>) {
| ^^^^^^^^^^^^^^ the trait `Trait` cannot be made into an object
|
= help: consider moving `foo` to another trait
error: aborting due to previous error

View File

@ -2,7 +2,9 @@ error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:18:38
|
LL | trait NonObjectSafe1: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| -------------- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | fn takes_non_object_safe_ref<T>(obj: &dyn NonObjectSafe1) {
| ^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe1` cannot be made into an object
@ -10,35 +12,49 @@ LL | fn takes_non_object_safe_ref<T>(obj: &dyn NonObjectSafe1) {
error[E0038]: the trait `NonObjectSafe2` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:22:36
|
LL | trait NonObjectSafe2 {
| -------------- this trait cannot be made into an object...
LL | fn static_fn() {}
| --------- associated function `static_fn` has no `self` parameter
| --------- ...because associated function `static_fn` has no `self` parameter
...
LL | fn return_non_object_safe_ref() -> &'static dyn NonObjectSafe2 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe2` cannot be made into an object
|
= help: consider turning `static_fn` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error[E0038]: the trait `NonObjectSafe3` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:27:35
|
LL | trait NonObjectSafe3 {
| -------------- this trait cannot be made into an object...
LL | fn foo<T>(&self);
| --- method `foo` has generic type parameters
| --- ...because method `foo` has generic type parameters
...
LL | fn takes_non_object_safe_box(obj: Box<dyn NonObjectSafe3>) {
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe3` cannot be made into an object
|
= help: consider moving `foo` to another trait
error[E0038]: the trait `NonObjectSafe4` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:31:35
|
LL | trait NonObjectSafe4 {
| -------------- this trait cannot be made into an object...
LL | fn foo(&self, &Self);
| --- method `foo` references the `Self` type in its parameters or return type
| --- ...because method `foo` references the `Self` type in its parameters or return type
...
LL | fn return_non_object_safe_rc() -> std::rc::Rc<dyn NonObjectSafe4> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe4` cannot be made into an object
|
= help: consider moving `foo` to another trait
error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:6
|
LL | trait NonObjectSafe1: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| -------------- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | impl Trait for dyn NonObjectSafe1 {}
| ^^^^^ the trait `NonObjectSafe1` cannot be made into an object

View File

@ -1,20 +1,28 @@
error[E0038]: the trait `NotObjectSafe` cannot be made into an object
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:21:13
|
LL | trait NotObjectSafe {
| ------------- this trait cannot be made into an object...
LL | fn foo() -> Self;
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | fn car() -> dyn NotObjectSafe {
| ^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error[E0038]: the trait `NotObjectSafe` cannot be made into an object
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:28:13
|
LL | trait NotObjectSafe {
| ------------- this trait cannot be made into an object...
LL | fn foo() -> Self;
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | fn cat() -> Box<dyn NotObjectSafe> {
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error: aborting due to 2 previous errors

View File

@ -2,10 +2,14 @@ error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/issue-18959.rs:11:11
|
LL | pub trait Foo { fn foo<T>(&self, ext_thing: &T); }
| --- method `foo` has generic type parameters
| --- ...because method `foo` has generic type parameters
LL | pub trait Bar: Foo { }
| --- this trait cannot be made into an object...
...
LL | fn foo(b: &dyn Bar) {
| ^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `foo` to another trait
error: aborting due to previous error

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Qiz` cannot be made into an object
--> $DIR/issue-19380.rs:11:3
|
LL | trait Qiz {
| --- this trait cannot be made into an object...
LL | fn qiz();
| --- associated function `qiz` has no `self` parameter
| --- ...because associated function `qiz` has no `self` parameter
...
LL | foos: &'static [&'static (dyn Qiz + 'static)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Qiz` cannot be made into an object
|
= help: consider turning `qiz` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error: aborting due to previous error

View File

@ -2,20 +2,29 @@ error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/issue-19538.rs:17:15
|
LL | fn foo<T>(&self, val: T);
| --- method `foo` has generic type parameters
| --- ...because method `foo` has generic type parameters
...
LL | trait Bar: Foo { }
| --- this trait cannot be made into an object...
...
LL | let test: &mut dyn Bar = &mut thing;
| ^^^^^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `foo` to another trait
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/issue-19538.rs:17:30
|
LL | fn foo<T>(&self, val: T);
| --- method `foo` has generic type parameters
| --- ...because method `foo` has generic type parameters
...
LL | trait Bar: Foo { }
| --- this trait cannot be made into an object...
...
LL | let test: &mut dyn Bar = &mut thing;
| ^^^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `foo` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&mut dyn Bar>` for `&mut Thing`
= note: required by cast to type `&mut dyn Bar`

View File

@ -2,9 +2,10 @@ error[E0038]: the trait `Array` cannot be made into an object
--> $DIR/issue-20692.rs:7:5
|
LL | trait Array: Sized + Copy {}
| ----- ---- traits that require `Self: Sized` cannot be made into an object
| |
| traits that require `Self: Sized` cannot be made into an object
| ----- ----- ---- ...because it requires `Self: Sized`
| | |
| | ...because it requires `Self: Sized`
| this trait cannot be made into an object...
...
LL | &dyn Array;
| ^^^^^^^^^^ the trait `Array` cannot be made into an object
@ -13,9 +14,10 @@ error[E0038]: the trait `Array` cannot be made into an object
--> $DIR/issue-20692.rs:4:13
|
LL | trait Array: Sized + Copy {}
| ----- ---- traits that require `Self: Sized` cannot be made into an object
| |
| traits that require `Self: Sized` cannot be made into an object
| ----- ----- ---- ...because it requires `Self: Sized`
| | |
| | ...because it requires `Self: Sized`
| this trait cannot be made into an object...
...
LL | let _ = x
| ^ the trait `Array` cannot be made into an object

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `Map` cannot be made into an object
LL | as &dyn Map<Key=u32,MapValue=u32>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Map` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error: aborting due to previous error

View File

@ -5,7 +5,7 @@ LL | / dyn Bar
LL | | <Assoc=()>
| |________________________^ the trait `Bar` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `B` cannot be made into an object
LL | trait C<T>: A<dyn B<T, Output=usize>> {}
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `B` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `Foo` cannot be made into an object
LL | let _f: Box<dyn Foo> =
| ^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/issue-38604.rs:15:9
@ -12,7 +12,7 @@ error[E0038]: the trait `Foo` cannot be made into an object
LL | Box::new(());
| ^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::boxed::Box<dyn Foo>>` for `std::boxed::Box<()>`
= note: required by cast to type `std::boxed::Box<dyn Foo>`

View File

@ -1,8 +1,10 @@
error: the trait `X` cannot be made into an object
--> $DIR/issue-50781.rs:6:8
|
LL | trait X {
| - this trait cannot be made into an object...
LL | fn foo(&self) where Self: Trait;
| ^^^ method `foo` references the `Self` type in where clauses
| ^^^ ...because method `foo` references the `Self` type in its `where` clause
|
note: the lint level is defined here
--> $DIR/issue-50781.rs:1:9
@ -11,6 +13,7 @@ LL | #![deny(where_clauses_object_safety)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443>
= help: consider moving `foo` to another trait
error: aborting due to previous error

View File

@ -13,7 +13,9 @@ error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:28:19
|
LL | trait Foo : Copy {
| ---- traits that require `Self: Sized` cannot be made into an object
| --- ---- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let z = &x as &dyn Foo;
| ^^^^^^^^ the trait `Foo` cannot be made into an object
@ -22,7 +24,9 @@ error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:28:13
|
LL | trait Foo : Copy {
| ---- traits that require `Self: Sized` cannot be made into an object
| --- ---- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let z = &x as &dyn Foo;
| ^^ the trait `Foo` cannot be made into an object

View File

@ -13,7 +13,9 @@ error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/kindck-inherited-copy-bound.rs:28:13
|
LL | trait Foo : Copy {
| ---- traits that require `Self: Sized` cannot be made into an object
| --- ---- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let z = &x as &dyn Foo;
| ^^ the trait `Foo` cannot be made into an object

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-associated-consts.rs:12:30
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | const X: usize;
| - the trait cannot contain associated consts like `X`
| - ...because it cannot contain associated consts like `X`
...
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `X` to another trait
error: aborting due to previous error

View File

@ -1,12 +1,15 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-associated-consts.rs:14:5
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | const X: usize;
| - the trait cannot contain associated consts like `X`
| - ...because it cannot contain associated consts like `X`
...
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= help: consider moving `X` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Bar>` for `&T`
= note: required by cast to type `&dyn Bar`

View File

@ -1,20 +1,28 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:18:30
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar<T>(&self, t: T);
| --- method `bar` has generic type parameters
| --- ...because method `bar` has generic type parameters
...
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:24:39
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar<T>(&self, t: T);
| --- method `bar` has generic type parameters
| --- ...because method `bar` has generic type parameters
...
LL | fn make_bar_explicit<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
error: aborting due to 2 previous errors

View File

@ -1,24 +1,30 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:20:5
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar<T>(&self, t: T);
| --- method `bar` has generic type parameters
| --- ...because method `bar` has generic type parameters
...
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Bar>` for `&T`
= note: required by cast to type `&dyn Bar`
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-generics.rs:26:5
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar<T>(&self, t: T);
| --- method `bar` has generic type parameters
| --- ...because method `bar` has generic type parameters
...
LL | t as &dyn Bar
| ^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Bar>` for `&T`
= note: required by cast to type `&dyn Bar`

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `Expr` cannot be made into an object
LL | elements: Vec<Box<dyn Expr + 'x>>,
| ^^^^^^^^^^^^^ the trait `Expr` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error: aborting due to previous error

View File

@ -1,20 +1,28 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:22:30
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar(&self, x: &Self);
| --- method `bar` references the `Self` type in its parameters or return type
| --- ...because method `bar` references the `Self` type in its parameters or return type
...
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
error[E0038]: the trait `Baz` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:28:30
|
LL | trait Baz {
| --- this trait cannot be made into an object...
LL | fn baz(&self) -> Self;
| --- method `baz` references the `Self` type in its parameters or return type
| --- ...because method `baz` references the `Self` type in its parameters or return type
...
LL | fn make_baz<T:Baz>(t: &T) -> &dyn Baz {
| ^^^^^^^^ the trait `Baz` cannot be made into an object
|
= help: consider moving `baz` to another trait
error: aborting due to 2 previous errors

View File

@ -1,24 +1,30 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:24:5
|
LL | trait Bar {
| --- this trait cannot be made into an object...
LL | fn bar(&self, x: &Self);
| --- method `bar` references the `Self` type in its parameters or return type
| --- ...because method `bar` references the `Self` type in its parameters or return type
...
LL | t
| ^ the trait `Bar` cannot be made into an object
|
= help: consider moving `bar` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Bar>` for `&T`
= note: required by cast to type `&dyn Bar`
error[E0038]: the trait `Baz` cannot be made into an object
--> $DIR/object-safety-mentions-Self.rs:30:5
|
LL | trait Baz {
| --- this trait cannot be made into an object...
LL | fn baz(&self) -> Self;
| --- method `baz` references the `Self` type in its parameters or return type
| --- ...because method `baz` references the `Self` type in its parameters or return type
...
LL | t
| ^ the trait `Baz` cannot be made into an object
|
= help: consider moving `baz` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Baz>` for `&T`
= note: required by cast to type `&dyn Baz`

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety-no-static.rs:12:18
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn foo() {}
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | fn diverges() -> Box<dyn Foo> {
| ^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error: aborting due to previous error

View File

@ -1,12 +1,15 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety-no-static.rs:22:27
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn foo() {}
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | let b: Box<dyn Foo> = Box::new(Bar);
| ^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::boxed::Box<dyn Foo>>` for `std::boxed::Box<Bar>`
= note: required by cast to type `std::boxed::Box<dyn Foo>`

View File

@ -1,8 +1,10 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-sized-2.rs:14:30
|
LL | trait Bar
| --- this trait cannot be made into an object...
LL | where Self : Sized
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ...because it requires `Self: Sized`
...
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object

View File

@ -1,8 +1,10 @@
error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-sized-2.rs:16:5
|
LL | trait Bar
| --- this trait cannot be made into an object...
LL | where Self : Sized
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ...because it requires `Self: Sized`
...
LL | t
| ^ the trait `Bar` cannot be made into an object

View File

@ -2,7 +2,9 @@ error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-sized.rs:12:30
|
LL | trait Bar : Sized {
| ----- traits that require `Self: Sized` cannot be made into an object
| --- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
| ^^^^^^^^ the trait `Bar` cannot be made into an object

View File

@ -2,7 +2,9 @@ error[E0038]: the trait `Bar` cannot be made into an object
--> $DIR/object-safety-sized.rs:14:5
|
LL | trait Bar : Sized {
| ----- traits that require `Self: Sized` cannot be made into an object
| --- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | t
| ^ the trait `Bar` cannot be made into an object

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `Baz` cannot be made into an object
LL | fn make_baz<T:Baz>(t: &T) -> &dyn Baz {
| ^^^^^^^ the trait `Baz` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `issue_3907::Foo` cannot be made into an object
LL | fn bar(_x: Foo) {}
| ^^^ the trait `issue_3907::Foo` cannot be made into an object
|
= note: associated function `bar` has no `self` parameter
= note: the trait cannot be made into an object because associated function `bar` has no `self` parameter
error: aborting due to previous error

View File

@ -1,21 +1,28 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:33:32
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn foo(self: &Rc<Self>) -> usize;
| --- method `foo`'s `self` parameter cannot be dispatched on
| --- ...because method `foo`'s `self` parameter cannot be dispatched on
...
LL | let x = Rc::new(5usize) as Rc<dyn Foo>;
| ^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= help: consider changing method `foo`'s `self` parameter to be `&self`
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:33:13
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn foo(self: &Rc<Self>) -> usize;
| --- method `foo`'s `self` parameter cannot be dispatched on
| --- ...because method `foo`'s `self` parameter cannot be dispatched on
...
LL | let x = Rc::new(5usize) as Rc<dyn Foo>;
| ^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= help: consider changing method `foo`'s `self` parameter to be `&self`
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::rc::Rc<dyn Foo>>` for `std::rc::Rc<usize>`
= note: required by cast to type `std::rc::Rc<dyn Foo>`

View File

@ -1,12 +1,15 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/arbitrary-self-types-not-object-safe.rs:33:13
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn foo(self: &Rc<Self>) -> usize;
| --- method `foo`'s `self` parameter cannot be dispatched on
| --- ...because method `foo`'s `self` parameter cannot be dispatched on
...
LL | let x = Rc::new(5usize) as Rc<dyn Foo>;
| ^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object
|
= help: consider changing method `foo`'s `self` parameter to be `&self`
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::rc::Rc<dyn Foo>>` for `std::rc::Rc<usize>`
= note: required by cast to type `std::rc::Rc<dyn Foo>`

View File

@ -15,7 +15,9 @@ error[E0038]: the trait `A` cannot be made into an object
--> $DIR/object-unsafe-trait-should-use-self.rs:3:13
|
LL | trait A: Sized {
| ----- traits that require `Self: Sized` cannot be made into an object
| - ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
LL | fn f(a: A) -> A;
| ^ the trait `A` cannot be made into an object
@ -35,10 +37,14 @@ LL | fn f(a: Self) -> Self;
error[E0038]: the trait `B` cannot be made into an object
--> $DIR/object-unsafe-trait-should-use-self.rs:8:13
|
LL | trait B {
| - this trait cannot be made into an object...
LL | fn f(a: B) -> B;
| - ^ the trait `B` cannot be made into an object
| |
| associated function `f` has no `self` parameter
| ...because associated function `f` has no `self` parameter
|
= help: consider turning `f` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error: aborting due to 4 previous errors

View File

@ -4,7 +4,7 @@ error[E0038]: the trait `std::cmp::Eq` cannot be made into an object
LL | let _: &dyn EqAlias = &123;
| ^^^^^^^^^^^ the trait `std::cmp::Eq` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
= note: the trait cannot be made into an object because it cannot use `Self` as a type parameter in the supertraits or `where`-clauses
error[E0191]: the value of the associated type `Item` (from trait `std::iter::Iterator`) must be specified
--> $DIR/trait-alias-object-fail.rs:9:17

View File

@ -111,16 +111,22 @@ error[E0038]: the trait `assoc_const::C` cannot be made into an object
--> $DIR/trait-item-privacy.rs:101:5
|
LL | const A: u8 = 0;
| - the trait cannot contain associated consts like `A`
| - ...because it cannot contain associated consts like `A`
...
LL | const B: u8 = 0;
| - the trait cannot contain associated consts like `B`
| - ...because it cannot contain associated consts like `B`
...
LL | pub trait C: A + B {
| - this trait cannot be made into an object...
LL | const C: u8 = 0;
| - the trait cannot contain associated consts like `C`
| - ...because it cannot contain associated consts like `C`
...
LL | C::A;
| ^^^^ the trait `assoc_const::C` cannot be made into an object
|
= help: consider moving `C` to another trait
= help: consider moving `B` to another trait
= help: consider moving `A` to another trait
error[E0223]: ambiguous associated type
--> $DIR/trait-item-privacy.rs:115:12

View File

@ -10,7 +10,7 @@ error[E0038]: the trait `std::marker::Copy` cannot be made into an object
LL | m!(dyn Copy + Send + 'static);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` cannot be made into an object
|
= note: traits that require `Self: Sized` cannot be made into an object
= note: the trait cannot be made into an object because it requires `Self: Sized`
error: aborting due to 2 previous errors

View File

@ -1,23 +1,30 @@
error[E0038]: the trait `Tr` cannot be made into an object
--> $DIR/trait-object-safety.rs:15:22
|
LL | trait Tr {
| -- this trait cannot be made into an object...
LL | fn foo();
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | let _: &dyn Tr = &St;
| ^^^ the trait `Tr` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Tr>` for `&St`
= note: required by cast to type `&dyn Tr`
error[E0038]: the trait `Tr` cannot be made into an object
--> $DIR/trait-object-safety.rs:15:12
|
LL | trait Tr {
| -- this trait cannot be made into an object...
LL | fn foo();
| --- associated function `foo` has no `self` parameter
| --- ...because associated function `foo` has no `self` parameter
...
LL | let _: &dyn Tr = &St;
| ^^^^^^^ the trait `Tr` cannot be made into an object
|
= help: consider turning `foo` into a method by giving it a `&self` argument or constraining it with `where Self: Sized`
error: aborting due to 2 previous errors

View File

@ -14,24 +14,31 @@ error[E0038]: the trait `bar` cannot be made into an object
--> $DIR/trait-test-2.rs:11:16
|
LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
| --- ---- method `blah` has generic type parameters
| |
| method `dup` references the `Self` type in its parameters or return type
| --- --- ---- ...because method `blah` has generic type parameters
| | |
| | ...because method `dup` references the `Self` type in its parameters or return type
| this trait cannot be made into an object...
...
LL | (box 10 as Box<dyn bar>).dup();
| ^^^^^^^^^^^^ the trait `bar` cannot be made into an object
|
= help: consider moving `dup` to another trait
= help: consider moving `blah` to another trait
error[E0038]: the trait `bar` cannot be made into an object
--> $DIR/trait-test-2.rs:11:6
|
LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
| --- ---- method `blah` has generic type parameters
| |
| method `dup` references the `Self` type in its parameters or return type
| --- --- ---- ...because method `blah` has generic type parameters
| | |
| | ...because method `dup` references the `Self` type in its parameters or return type
| this trait cannot be made into an object...
...
LL | (box 10 as Box<dyn bar>).dup();
| ^^^^^^ the trait `bar` cannot be made into an object
|
= help: consider moving `dup` to another trait
= help: consider moving `blah` to another trait
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::boxed::Box<dyn bar>>` for `std::boxed::Box<{integer}>`
= note: required by cast to type `std::boxed::Box<dyn bar>`

View File

@ -14,10 +14,14 @@ error[E0038]: the trait `MyAdd` cannot be made into an object
--> $DIR/type-parameter-defaults-referencing-Self-ppaux.rs:14:18
|
LL | trait MyAdd<Rhs=Self> { fn add(&self, other: &Rhs) -> Self; }
| --- method `add` references the `Self` type in its parameters or return type
| ----- --- ...because method `add` references the `Self` type in its parameters or return type
| |
| this trait cannot be made into an object...
...
LL | let y = x as dyn MyAdd<i32>;
| ^^^^^^^^^^^^^^ the trait `MyAdd` cannot be made into an object
|
= help: consider moving `add` to another trait
error: aborting due to 2 previous errors

View File

@ -2,7 +2,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj-box.rs:16:33
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let t_box: Box<dyn Trait> = Box::new(S);
| ^^^^^^^^^^^ the trait `Trait` cannot be made into an object
@ -14,7 +16,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj-box.rs:17:15
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | takes_box(Box::new(S));
| ^^^^^^^^^^^ the trait `Trait` cannot be made into an object
@ -26,7 +30,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj-box.rs:15:5
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | Box::new(S) as Box<dyn Trait>;
| ^^^^^^^^^^^ the trait `Trait` cannot be made into an object

View File

@ -2,7 +2,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj.rs:16:25
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let t: &dyn Trait = &S;
| ^^ the trait `Trait` cannot be made into an object
@ -14,7 +16,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj.rs:17:17
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | takes_trait(&S);
| ^^ the trait `Trait` cannot be made into an object
@ -26,7 +30,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-convert-unsafe-trait-obj.rs:15:5
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | &S as &dyn Trait;
| ^^ the trait `Trait` cannot be made into an object

View File

@ -25,7 +25,7 @@ error[E0038]: the trait `std::marker::Copy` cannot be made into an object
LL | fn bar() where Vec<dyn Copy>:, {}
| ^^^^^^^^^^^^^ the trait `std::marker::Copy` cannot be made into an object
|
= note: traits that require `Self: Sized` cannot be made into an object
= note: the trait cannot be made into an object because it requires `Self: Sized`
error: aborting due to 3 previous errors

View File

@ -1,11 +1,15 @@
error[E0038]: the trait `A` cannot be made into an object
--> $DIR/wf-object-safe.rs:9:13
|
LL | trait A {
| - this trait cannot be made into an object...
LL | fn foo(&self, _x: &Self);
| --- method `foo` references the `Self` type in its parameters or return type
| --- ...because method `foo` references the `Self` type in its parameters or return type
...
LL | let _x: &dyn A;
| ^^^^^^ the trait `A` cannot be made into an object
|
= help: consider moving `foo` to another trait
error: aborting due to previous error

View File

@ -16,7 +16,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-unsafe-trait-obj-match.rs:26:21
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | Some(()) => &S,
| ^^ the trait `Trait` cannot be made into an object
@ -28,7 +30,9 @@ error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/wf-unsafe-trait-obj-match.rs:25:25
|
LL | trait Trait: Sized {}
| ----- traits that require `Self: Sized` cannot be made into an object
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
LL | let t: &dyn Trait = match opt() {
| ^^^^^^^^^^^ the trait `Trait` cannot be made into an object