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

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

Open
stefanhoelzl opened this issue Apr 15, 2018 · 5 comments
Open

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

stefanhoelzl opened this issue Apr 15, 2018 · 5 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. 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.

Comments

@stefanhoelzl
Copy link

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
@sfackler
Copy link
Member

const is different from static. Every use of a const creates a new copy of the value. That code is equivalent to

struct StructA {
    pub a: u32,
}

fn main() {
    StructA { a: 0 }.a = 10;
    println!("{}", StructA { a: 0 }.a);
}

@stefanhoelzl
Copy link
Author

ok, know I understand at least why the behavior is how it is.

But it still seems misleading to me.

@oli-obk oli-obk added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 15, 2018
@estebank
Copy link
Contributor

This should probably be a clippy lint.

@zackmdavis
Copy link
Member

possible duplicate of #49256?

@XAMPPRocky XAMPPRocky added 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 Aug 27, 2018
@estebank estebank added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Apr 29, 2019
@steveklabnik
Copy link
Member

Triage: no change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. 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.
Projects
None yet
Development

No branches or pull requests

7 participants