Skip to content

No compiler error when attempting to change field of const struct #49974

Open
@stefanhoelzl

Description

@stefanhoelzl

There is no compiler error, when trying to change the field of a const struct.

I tried this code:

struct StructA {
	pub a: u32,
}

const A: StructA = StructA{a: 0};

fn main() {
    A.a = 10;
    println!("{}", A.a);
}

I expected to see this happen:
Compiler error, because I try to change the value of a const.

Instead, this happened:
The statement is just ignored. A.a = 10;looks like the value of A.a is set to 10, since there is no compiler error, I expect this to happen.
Instead when printing the value of A.a, it is still the initial value 0

Meta

rustc --version --verbose:

rustc 1.24.0-nightly (8e7a609e6 2018-01-04)
binary: rustc
commit-hash: 8e7a609e635b728eba65d471c985ab462dc4cfc7
commit-date: 2018-01-04
host: x86_64-apple-darwin
release: 1.24.0-nightly
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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