Skip to content

Unclear error message when destructuring in const fn #29525

Closed
@jethrogb

Description

@jethrogb
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions