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 warning on assigning to field of const that doesn't succeed #74053

Closed
CraftSpider opened this issue Jul 5, 2020 · 7 comments · Fixed by #75573
Closed

No warning on assigning to field of const that doesn't succeed #74053

CraftSpider opened this issue Jul 5, 2020 · 7 comments · Fixed by #75573
Labels
A-lints 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. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users.

Comments

@CraftSpider
Copy link
Contributor

I tried this code:

I expected to see this happen:

  • Either successful assignment or a compiler error, I actually would prefer the first but I'm not sure the intended behavior either way.

Instead, this happened:

  • Compile succeeds without warning, the value doesn't change.

Meta

Occurs in Stable, Beta, and Nightly builds on the playground.

No backtrace is emitted

@CraftSpider CraftSpider added the C-bug Category: This is a bug. label Jul 5, 2020
@lcnr lcnr added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Jul 5, 2020
@lcnr
Copy link
Contributor

lcnr commented Jul 5, 2020

I am not completely sure if this behavior is intended. But even if this is not a bug,
we should still at least warn here.

Inline snippet

struct Foo {
    val: i32,
}

const FOO: Foo = Foo { val: 100 };

const fn bar() -> i32 {
    FOO.val = -100;
    FOO.val
}

fn main() {
    println!("{}", FOO.val);
    println!("{}", bar());
}

@lcnr lcnr added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 5, 2020
@tesuji
Copy link
Contributor

tesuji commented Jul 5, 2020

I think this is intentional behavior base on its MIR and how const works: https://rust.godbolt.org/z/G-BPoi.

@petrochenkov
Copy link
Contributor

cc rust-lang/rust-clippy#829
(Also see the many issues linked to it.)

@leonardo-m
Copy link

Is this better left in Clippy or better moved to Rustc? (I think the latter)

@CraftSpider
Copy link
Contributor Author

Ahhh, I get it. Because it's always inlined, the left hand is exactly equivalent to <snip>.foo, which is a valid assignment, but is immediately discarded. I'd definitely prefer the compiler warning, would make it much more clear what's going on.

@LeSeulArtichaut
Copy link
Contributor

Seems related to #16789. I also think we should emit an unused_assignments warn here, because this code behaves correctly, but is useless as the constant gets inlined but isn't assigned to a variable.

@LeSeulArtichaut LeSeulArtichaut added A-lints 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. and removed C-bug Category: This is a bug. labels Jul 6, 2020
@Dylan-DPC-zz
Copy link

This doesn't require prioritisation. Removing it based on the discussion

@Dylan-DPC-zz Dylan-DPC-zz removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 6, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 10, 2020
…, r=oli-obk

Add CONST_ITEM_MUTATION lint

Fixes rust-lang#74053
Fixes rust-lang#55721

This PR adds a new lint `CONST_ITEM_MUTATION`.
Given an item `const FOO: SomeType = ..`, this lint fires on:

* Attempting to write directly to a field (`FOO.field = some_val`) or
  array entry (`FOO.array_field[0] = val`)
* Taking a mutable reference to the `const` item (`&mut FOO`), including
  through an autoderef `FOO.some_mut_self_method()`

The lint message explains that since each use of a constant creates a
new temporary, the original `const` item will not be modified.
@bors bors closed this as completed in f422ef1 Sep 10, 2020
flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 10, 2020
…, r=oli-obk

Add CONST_ITEM_MUTATION lint

Fixes rust-lang#74053
Fixes rust-lang#55721

This PR adds a new lint `CONST_ITEM_MUTATION`.
Given an item `const FOO: SomeType = ..`, this lint fires on:

* Attempting to write directly to a field (`FOO.field = some_val`) or
  array entry (`FOO.array_field[0] = val`)
* Taking a mutable reference to the `const` item (`&mut FOO`), including
  through an autoderef `FOO.some_mut_self_method()`

The lint message explains that since each use of a constant creates a
new temporary, the original `const` item will not be modified.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints 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. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants