Skip to content

Rustc doesn't error when trying to write to a const array #55721

Closed
@morbatex

Description

@morbatex

When trying to write to a const array rustc does compile the code without throwing an error but does not assign the value to the array.

As an example

const FOO : [i32;1] = [0];

fn main() {
    FOO[0] = 1;
    println!("{:?}",FOO);
}

compiles and prints

[0]

Is this behavior intended or should the compiler stop with an error as it does when compiling the following code?

const BAR : i32 = 0;

fn main() {
   BAR = 1;
    println!("{:?}",BAR);
}
BAR = 1;
^^^^^^^ left-hand of expression not valid

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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