Skip to content

rustc suggests dyn SomeTrait for traits which aren't object safe #119525

Closed
@Wilfred

Description

@Wilfred

Code

fn ord_prefer_dot(s: &str) -> Ord {
    (s.starts_with("."), s)
}

Current output

error[E0038]: the trait `Ord` cannot be made into an object
   --> src/lib.rs:1:31
    |
1   | fn ord_prefer_dot(s: &str) -> Ord {
    |                               ^^^ `Ord` 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>
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:305:15
    |
305 | pub trait Eq: PartialEq<Self> {
    |               ^^^^^^^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter
...
793 | pub trait Ord: Eq + PartialOrd<Self> {
    |                     ^^^^^^^^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:1:31
  |
1 | fn ord_prefer_dot(s: &str) -> Ord {
  |                               ^^^
  |
help: add `dyn` keyword before this trait
  |
1 | fn ord_prefer_dot(s: &str) -> dyn Ord {
  |                               +++

Some errors have detailed explanations: E0038, E0782.
For more information about an error, try `rustc --explain E0038`.

Desired output

error[E0038]: the trait `Ord` cannot be made into an object
   --> src/lib.rs:1:31
    |
1   | fn ord_prefer_dot(s: &str) -> Ord {
    |                               ^^^ `Ord` 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>
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:305:15
    |
305 | pub trait Eq: PartialEq<Self> {
    |               ^^^^^^^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter
...
793 | pub trait Ord: Eq + PartialOrd<Self> {
    |                     ^^^^^^^^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter

help: add `impl` keyword before this trait
  |
1 | fn ord_prefer_dot(s: &str) -> impl Ord {
  |                               ++++

Rationale and extra context

In this case, a possible valid implementation would be:

fn ord_prefer_dot(s: &str) -> impl Ord + '_ {
    (s.starts_with("."), s)
}

It would be an improvement to not suggest dyn Ord, but ideally rustc would suggest impl Ord I think.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions