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

Produce a warning when using `const` with interior mutability #40543

Open
matklad opened this Issue Mar 15, 2017 · 16 comments

Comments

Projects
None yet
10 participants
@matklad
Copy link
Member

matklad commented Mar 15, 2017

Originally reported in https://users.rust-lang.org/t/broken-atomics-puzzle/9533

Consider this code

use std::sync::atomic::{ATOMIC_BOOL_INIT, AtomicBool, Ordering};

pub const A: AtomicBool = ATOMIC_BOOL_INIT;

fn main() {
    A.compare_and_swap(false, true, Ordering::SeqCst);
    println!("A = {}", A.load(Ordering::SeqCst));
}

Playground

It compiles and runs cleanly, but produces unexpected results because const is used instead of static.

It would be nice to somehow give a warning for .compare_and_swap call, but I am not sure it is possible.

@petrochenkov

This comment has been minimized.

Copy link
Contributor

petrochenkov commented Mar 15, 2017

@matklad

This comment has been minimized.

Copy link
Member Author

matklad commented Mar 15, 2017

Note that compare_and_swap uses &self:

compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool
@petrochenkov

This comment has been minimized.

Copy link
Contributor

petrochenkov commented Mar 15, 2017

The underlying issue is the same - A is a rvalue and copied on use and this becomes observable when any mutability is involved, interior or exterior.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Mar 15, 2017

New warnings generally require an RFC, but I'll let @rust-lang/compiler make the call

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 16, 2017

I'm not sure -- precisely what conditions would trigger the warning? I agree there is a subtle footgun at play here, if you don't understand the rules.

@matklad

This comment has been minimized.

Copy link
Member Author

matklad commented Mar 16, 2017

I'm not sure -- precisely what conditions would trigger the warning?

I think "calling a method exploiting interior mutability on const" is the right condition, but I don't think we can really check it because we don't know which methods involve interior mutability.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 21, 2017

Seems like it would have to "calling any method on const if the type of that const involves UnsafeCell", roughly speaking. Right?

@matklad

This comment has been minimized.

Copy link
Member Author

matklad commented Mar 21, 2017

Seems like it would have to "calling any method on const if the type of that const involves UnsafeCell", roughly speaking. Right?

Yep. Perhaps this won't add false positives in practice?

I wonder if we need to warn about creating constants with interior mutability. This is the root of the problem. Currently we need such constants for ATOMIC_X_INIT, but it is only because there are no constant functions in Rust yet.

@Diggsey

This comment has been minimized.

Copy link
Contributor

Diggsey commented Sep 13, 2017

I'm not sure that's quite right:

Seems to me like this behaviour occurs whenever an internally mutable rvalue is borrowed. However, it's only surprising when that rvalue comes from a constant, because constants don't look like rvalues, and because they can be used multiple times, whereas most rvalues can only be used once.

Putting that together, it looks like the compiler should issue a warning whenever a constant containing an UnsafeCell is borrowed.

@burdges

This comment has been minimized.

Copy link

burdges commented Jan 19, 2018

Another autotrait would be required for this, no? All atomics have this problem, but maybe not all usages of UnsafeCell? Also variance matters, like a &AtomicBool or &Arc<T> sounds okay.

Also, there are usages for const fn with smart pointer, like Mutex/Arc::new() should become const fn so that statics containing Arcs can be initialized at compile time, meaning you'll transiently have a const with an Atomic and/or UnsafeCell during compilation.

@pedrohjordao

This comment has been minimized.

Copy link

pedrohjordao commented Aug 11, 2018

Just got hit by this unexpected behavior as well and thought I was going crazy. Reading the explanations made everything make sense, but some warning would be ideal.

@dhardy

This comment has been minimized.

Copy link
Contributor

dhardy commented Sep 8, 2018

@burdges has a point. Items like the following should be illegal, not merely illegal to try mutating.

const X: Cell<u32> = Cell::new(0);
@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Sep 8, 2018

@dhardy Why so? That's not a static, there's no Cell<u32> place in memory, it's just a constant value, a "template" for creating the same value.

@dhardy

This comment has been minimized.

Copy link
Contributor

dhardy commented Sep 9, 2018

But are there any legitimate uses for this? Possibly as part of a compound type I guess, but that would be unusual. A lint may be better then.

Okay, the problematic bit is that one can call X.set(5); even though consts do not permit mutation.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Sep 9, 2018

@dhardy Yeah. IMO, this is in the same category as this:

struct Foo<T>(T);
const X: Foo<i32> = Foo(0);
fn main() {
    X.0 += 1;
}

Clippy should be linting both this and your example, saying that you're mutating a (field of a) temporary and you probably wanted to do something else.
Although it's maybe harder for Cell::set (assuming we'd rather not hardcode it in clippy).

@dhardy

This comment has been minimized.

Copy link
Contributor

dhardy commented Sep 10, 2018

Why rely on Clippy here? As I understand it, Clippy is primarily about style and succinctness, not correctness. If someone is assigning to a temporary field that is very likely incorrect code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.