Skip to content

Setting of member of const struct should not be possible #68713

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

Closed
gwierzchowski opened this issue Jan 31, 2020 · 2 comments
Closed

Setting of member of const struct should not be possible #68713

gwierzchowski opened this issue Jan 31, 2020 · 2 comments

Comments

@gwierzchowski
Copy link

In below listing the assignment line I think should not compile with error like "invalid left-hand side expression" - as it would be in case variable was not a struct.
PS: Moving const definition to the function body does not change anything.

pub struct Foo {
    bar: bool,
}
const CONF: Foo = Foo { bar: false };

fn main() {
    println!("main 0 {}", CONF.bar); // prints false - OK
    
    // This does compile ?!?
    CONF.bar = true;

    println!("main 1 {}", CONF.bar); // prints false - ?!?
}

(Playground)

Output:

main 0 false
main 1 false

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/playground`

@jonas-schievink
Copy link
Contributor

This is working as expected – the LHS of an assignment must be a place expression, which a field access is. This should probably be warned against though, but that is covered by #16789, so closing in favor of that.

@gwierzchowski
Copy link
Author

Got it.
As I understand assignment in my sample is equivalent to:

Foo { bar: false }.bar = true;

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants