Skip to content

Trying to create a trait object with associated functions should have a better explanation #61973

Closed
@estebank

Description

@estebank

When trying to use associated functions and trait objects we get the following type of errors:

error[E0038]: the trait `content::database::utils::CodeNormalizer` cannot be made into an object
   --> src/content/database/utils.rs:175:16
    |
175 | const SYSTEMS: &[Box<&dyn CodeNormalizer>] = &[
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `content::database::utils::CodeNormalizer` cannot be made into an object
    |
    = note: method `normalize` has no receiver
    = note: method `get_name` has no receiver

There are two possible cases for intent:

  • we're trying to do something that Rust doesn't allow, which is having associated functions that don't take self in a trait object
  • we have zero-sized structs that implement a given trait to carry a bunch of functionality

In the former case, the explanation leaves a lot to be desired: what is a receiver? Why are we calling it a method when it's really a function in this case? Maybe we forgot to have self as the first argument? Should we be suggesting adding it? Why aren't we pointing at the trait fn definition nor the impl?

For the second case, I believe if we can detect it it'd be great, because we could customize the help text to suggest adding &self in a confident manner, as we wouldn't have to care about lifetime issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemT-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