Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
estebank opened this issue Jun 19, 2019 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

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.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system labels Jun 19, 2019
@estebank
Copy link
Contributor Author

estebank commented Sep 8, 2019

Fixed by #64110

@estebank estebank closed this as completed Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant