Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 2, 2024
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 2, 2024
  3. fmease commented on Jan 3, 2024

    @fmease
    Member

    As far as I can tell, #119148 would slightly help here by also suggesting impl Ord (next to Box<dyn Ord>).

    @estebank, do you think you can tweak #119148 to prefer impl-Trait over dyn-Trait if !tcx.check_is_object_safe(def_id) (or in a follow-up PR)? Unless that extra query call would lead to cycles of course.

  4. added
    A-dyn-traitArea: trait objects, vtable layout
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 3, 2024
  5. self-assigned this
    on Jan 3, 2024
  6. added a commit that references this issue on Jan 3, 2024
    78ef946
  7. added a commit that references this issue on Jan 5, 2024
    f41ad1b
  8. added a commit that references this issue on Jan 5, 2024
    541006b
  9. added a commit that references this issue on Jan 11, 2024
    3a683f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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