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

Compile error on const offset in struct with interior mutability #37

Closed
hvenev opened this issue Dec 11, 2019 · 4 comments · Fixed by #55
Closed

Compile error on const offset in struct with interior mutability #37

hvenev opened this issue Dec 11, 2019 · 4 comments · Fixed by #55

Comments

@hvenev
Copy link

hvenev commented Dec 11, 2019

Playground containing an extract of the code from the current master (d3f5f23)

#[repr(C)]
struct Foo {
    a: core::sync::atomic::AtomicUsize,
}

assert_eq!([0; offset_of!(Foo, a)].len(), 0);

Building this fails with the following error:

error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
  --> src/offset_of:112:66
    |
112 |             let base_ref = ::core::mem::transmute::<_, &$parent>(&uninit);
    |                                                                  ^^^^^^^
...
NNN |     assert_eq!([0; offset_of!(Foo, a)].len(), 0);
    |                    ------------------ in this macro invocation

Note that there is no error if Foo::a is usize instead.

@RalfJung
Copy link
Collaborator

@hvenev could you try this again with #![feature(const_cell_refs)] (rust-lang/rust#79703)? That enables a more precise way to check what happens with references to interior mutable types.

@RalfJung
Copy link
Collaborator

Sorry, I meant const_refs_to_cell, tracked at rust-lang/rust#80384.

@hvenev
Copy link
Author

hvenev commented Mar 28, 2021

@RalfJung
Copy link
Collaborator

Great, thanks for testing. :)

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

Successfully merging a pull request may close this issue.

2 participants