Skip to content

Commit d829163

Browse files
anforowiczcopybara-github
authored andcommitted
Fix offset_of assertions for fields of types with interior mutability.
Implementation of `memoffset::offset_of` (from the `memoffset` crate) ends up taking a reference to a struct. In `const` contexts (such as the context of the assertion) this runs into rust-lang/rust#80384. This CL works around this by opting the generated code into `#![feature(const_refs_to_cell)]`. After this CL bindings generated by `cc_bindings_from_rs` will require a "nightly" version of the Rust compiler. This is unfortunate, but this dependency already exists on `rs_bindings_from_cc` side (e.g. requiring `impl_trait_in_assoc_type` and/or `type_alias_impl_trait` unstable features). This CL unblocks implementing `Drop` support. `Drop` support adds bindings for additional types, some of which run into this bug. PiperOrigin-RevId: 546337289 Change-Id: I1684b30a1ac096cc5115aabbe6e5c6504286947c
1 parent aeb861f commit d829163

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

cc_bindings_from_rs/bindings.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ pub fn generate_bindings(input: &Input) -> Result<Output> {
101101
// bindings need to relax the `improper_ctypes_definitions` warning
102102
// for `char` (and possibly for other built-in types in the future).
103103
#![allow(improper_ctypes_definitions)] __NEWLINE__
104+
105+
// Workaround for b/290271595
106+
//
107+
// TODO(https://github.com/rust-lang/rust/issues/80384): Remove once the feature is
108+
// stabilized.
109+
#![feature(const_refs_to_cell)] __NEWLINE__
104110
__NEWLINE__
105111

106112
#rs_body

cc_bindings_from_rs/cc_bindings_from_rs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ inline void public_function() {
362362
// test_crate
363363
364364
#![allow(improper_ctypes_definitions)]
365+
#![feature(const_refs_to_cell)]
365366
366367
#[no_mangle]
367368
extern "C" fn __crubit_thunk__ANY_IDENTIFIER_CHARACTERS()

cc_bindings_from_rs/test/structs/structs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,33 @@ pub mod nested_ptr_type_mutability_qualifiers {
259259
}
260260
}
261261
}
262+
263+
/// This is a regression test for b/290271595 - it verifies that Rust-side
264+
/// `offset_of` assertions compile okay for bindings of types that use interior
265+
/// mutability. Before the bug was fixed, the test below would result in:
266+
///
267+
/// ```
268+
/// error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
269+
/// --> .../cc_bindings_from_rs/test/structs/structs_cc_api_impl.rs:254:23
270+
/// |
271+
/// 254 | const _: () = assert!(memoffset::offset_of!(::structs::...::SomeStruct, field) == 0);
272+
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
273+
/// |
274+
/// = note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more
275+
/// information
276+
/// = help: add `#![feature(const_refs_to_cell)]` to the crate attributes to enable
277+
/// = note: this error originates in the macro `_memoffset__let_base_ptr` which comes from the
278+
/// expansion of the macro `memoffset::offset_of` (in Nightly builds, run with -Z
279+
/// macro-backtrace for more info)
280+
/// ```
281+
pub mod interior_mutability {
282+
use std::cell::UnsafeCell;
283+
284+
#[derive(Debug, Default)]
285+
pub struct SomeStruct {
286+
/// `pub` to make sure that `assert!(memoffset::offset_of!(...) == ...)`
287+
/// is generated. (Such assertions are skipped for private
288+
/// fields.)
289+
pub field: UnsafeCell<i32>,
290+
}
291+
}

0 commit comments

Comments
 (0)