Skip to content

Commit

Permalink
Fix offset_of assertions for fields of types with interior mutability.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anforowicz authored and copybara-github committed Jul 7, 2023
1 parent aeb861f commit d829163
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cc_bindings_from_rs/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ pub fn generate_bindings(input: &Input) -> Result<Output> {
// bindings need to relax the `improper_ctypes_definitions` warning
// for `char` (and possibly for other built-in types in the future).
#![allow(improper_ctypes_definitions)] __NEWLINE__

// Workaround for b/290271595
//
// TODO(https://github.com/rust-lang/rust/issues/80384): Remove once the feature is
// stabilized.
#![feature(const_refs_to_cell)] __NEWLINE__
__NEWLINE__

#rs_body
Expand Down
1 change: 1 addition & 0 deletions cc_bindings_from_rs/cc_bindings_from_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ inline void public_function() {
// test_crate
#![allow(improper_ctypes_definitions)]
#![feature(const_refs_to_cell)]
#[no_mangle]
extern "C" fn __crubit_thunk__ANY_IDENTIFIER_CHARACTERS()
Expand Down
30 changes: 30 additions & 0 deletions cc_bindings_from_rs/test/structs/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,33 @@ pub mod nested_ptr_type_mutability_qualifiers {
}
}
}

/// This is a regression test for b/290271595 - it verifies that Rust-side
/// `offset_of` assertions compile okay for bindings of types that use interior
/// mutability. Before the bug was fixed, the test below would result in:
///
/// ```
/// error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
/// --> .../cc_bindings_from_rs/test/structs/structs_cc_api_impl.rs:254:23
/// |
/// 254 | const _: () = assert!(memoffset::offset_of!(::structs::...::SomeStruct, field) == 0);
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// |
/// = note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more
/// information
/// = help: add `#![feature(const_refs_to_cell)]` to the crate attributes to enable
/// = note: this error originates in the macro `_memoffset__let_base_ptr` which comes from the
/// expansion of the macro `memoffset::offset_of` (in Nightly builds, run with -Z
/// macro-backtrace for more info)
/// ```
pub mod interior_mutability {
use std::cell::UnsafeCell;

#[derive(Debug, Default)]
pub struct SomeStruct {
/// `pub` to make sure that `assert!(memoffset::offset_of!(...) == ...)`
/// is generated. (Such assertions are skipped for private
/// fields.)
pub field: UnsafeCell<i32>,
}
}

0 comments on commit d829163

Please sign in to comment.