Closed
Description
pub trait Item {
fn get_category()->String; // error
fn do_thing_inner(&self);
fn get_inner_val(&self) -> u32;
}
impl dyn Item {
pub fn do_thing(&self) {
self.do_thing_inner();
//etc.
}
pub fn get_val(&self) -> u32 {
self.get_inner_val() + 1
}
}
We currently emit
error[E0038]: the trait `Item` cannot be made into an object
--> src/lib.rs:7:6
|
1 | pub trait Item {
| ---- this trait cannot be made into an object...
2 | fn get_category()->String; // error
| ------------ ...because associated function `get_category` has no `self` parameter
...
7 | impl dyn Item {
| ^^^^^^^^ the trait `Item` cannot be made into an object
|
help: consider turning `get_category` into a method by giving it a `&self` argument or constraining it so it does not apply to trait objects
|
2 | fn get_category()->String where Self: Sized; // error
| ^^^^^^^^^^^^^^^^^
and two other similar errors.
Ideally, we would emit only a single error for this case, and produce output closer to
error[E0038]: the trait `Item` cannot be made into an object
--> src/lib.rs:7:6
|
7 | impl dyn Item {
| ^^^^^^^^ the trait `Item` cannot be made into an object
|
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
|
1 | pub trait Item {
| ---- this trait cannot be made into an object...
2 | fn get_category()->String; // error
| ^^^^^^^^^^^^ ...because associated function `get_category` has no `self` parameter
|
help: consider turning `get_category` into a method by giving it a `&self` argument
|
2 | fn get_category(&self)->String; // error
| ^^^^^
help: alternatively, consider constraining it so it does not apply to trait objects
|
2 | fn get_category()->String where Self: Sized; // error
| ^^^^^^^^^^^^^^^^^
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Trait systemCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Too much output caused by a single piece of incorrect code.Relevant to the compiler team, which will review and decide on the PR/issue.