Skip to content

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

@hvenev

Description

@hvenev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions