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

Unclear error message when destructuring in const fn #29525

Closed
jethrogb opened this issue Nov 2, 2015 · 3 comments
Closed

Unclear error message when destructuring in const fn #29525

jethrogb opened this issue Nov 2, 2015 · 3 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@jethrogb
Copy link
Contributor

jethrogb commented Nov 2, 2015

const fn i((a, b): (u32, u32)) -> u32 { a + b }

Errors with E0022 "arguments of constant functions can only be immutable by-value bindings". The detailed explanation for E0022 is:

Constant functions are not allowed to mutate anything. Thus, binding to an argument with a mutable pattern is not allowed. For example,

const fn foo(mut x: u8) {
    // do stuff
}

is bad because the function body may not mutate x.

Remove any mutable bindings from the argument list to fix this error. In case you need to mutate the argument, try lazily initializing a global variable instead of using a const fn, or refactoring the code to a functional style to avoid mutation if possible.

This does not seem to be the correct error message.

@steveklabnik steveklabnik added A-diagnostics Area: Messages for errors, warnings, and lints A-const-fn labels Nov 3, 2015
@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@estebank
Copy link
Contributor

Current output:

error: local variables in const fn are unstable
 --> src/lib.rs:1:13
  |
1 | const fn i((a, b): (u32, u32)) -> u32 { a + b }
  |             ^

@jethrogb
Copy link
Contributor Author

That's a lot better than it used to be, but still a little strange. Maybe the message should be "pattern matching in const fn is unstable"?

@estebank
Copy link
Contributor

This is now accepted in beta and nightly :)

@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants