Skip to content

Incorrect Behavior in Updating Cell Value #114939

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

Open
theshaurya opened this issue Aug 17, 2023 · 2 comments
Open

Incorrect Behavior in Updating Cell Value #114939

theshaurya opened this issue Aug 17, 2023 · 2 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@theshaurya
Copy link

theshaurya commented Aug 17, 2023

I tried this code:

use std::cell::Cell;

const A: Cell<i64> = Cell::new(0);

fn main() {
    A.set(2);
    println!("Value of A: {}", A.get());
}

The code should update the value of the Cell to 2 and print "Value of A: 2" or must show compilation error.

Instead, The code still prints "Value of A: 0" instead of "Value of A: 2".

image

@theshaurya theshaurya added the C-bug Category: This is a bug. label Aug 17, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 17, 2023
@SamZhang3
Copy link
Contributor

This is not a bug; the problem is that the Cell is a const, so using it creates a copy of the Cell each time, and the second time A is used to get the value out of it, it uses a fresh copy of the Cell which still has the value 0 in it.

You should get a warning about using const for a interior mutable type like Cell if you run this code through clippy.

@WaffleLapkin WaffleLapkin added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 17, 2023
@nbdd0121
Copy link
Contributor

Run with clippy gives a lint:

warning: a `const` item should never be interior mutable
 --> src/main.rs:3:1
  |
3 | const A: Cell<i64> = Cell::new(0);
  | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | make this a static item (maybe with lazy_static)
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
  = note: `#[warn(clippy::declare_interior_mutable_const)]` on by default

warning: a `const` item with interior mutability should not be borrowed
 --> src/main.rs:6:5
  |
6 |     A.set(2);
  |     ^
  |
  = help: assign this const to a local or static variable, and use the variable here
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
  = note: `#[warn(clippy::borrow_interior_mutable_const)]` on by default

warning: a `const` item with interior mutability should not be borrowed
 --> src/main.rs:7:32
  |
7 |     println!("Value of A: {}", A.get());
  |                                ^
  |
  = help: assign this const to a local or static variable, and use the variable here
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants