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

Suggest using let when a const uses outer function's generics #79955

Open
eloff opened this issue Dec 12, 2020 · 1 comment
Open

Suggest using let when a const uses outer function's generics #79955

eloff opened this issue Dec 12, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eloff
Copy link

eloff commented Dec 12, 2020

If you use:

const size = std::mem::size_of::<T>() instead of let size = ...

You get the misleading compiler error E401:

error[E0401]: can't use generic parameters from outer function

Which is total nonsense. You just need let instead of const here. This fooled me for a while, so I'm opening this issue so it will get addressed and not fool other developers.

For more info see: https://stackoverflow.com/questions/59808575/cant-create-a-constant-using-memsize-of-inside-a-function-on-a-sized-generic

@camelid
Copy link
Member

camelid commented Dec 12, 2020

Well, to my understanding, the error is correct, it's just a bit confusing. The compiler doesn't support using generic parameters from the function the const is defined in.

I'm labeling this as a diagnostics improvement issue.

@camelid camelid changed the title Can't create a constant using mem::size_of inside a function on a sized generic type (E401) Suggest using let when a const uses outer function's generics Dec 12, 2020
@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-papercut Diagnostics: An error or lint that needs small tweaks. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

2 participants