Fix strict-aliasing violation on traits holding inner fields
authorMatt Corallo <git@bluematt.me>
Thu, 27 Jul 2023 20:00:25 +0000 (20:00 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 28 Jul 2023 17:41:31 +0000 (17:41 +0000)
commitcd94859a3ef20447674f393d3f169bd9e29023a1
treeebdf9a27a364f5218670f2e54acaa4662a43b507
parent316ba2b5b764a1595f0993bc457714efdaf424ad
Fix strict-aliasing violation on traits holding inner fields

When we map a trait method which returns a reference to a struct,
we map it by storing said struct in the trait implementation
struct. Then, we create a setter method which ensures the new field
is set. Sadly, because the original Rust trait method may take a
non-mutable reference to self, we have to update the field without
a mutable reference to the trait implementation struct.

Previously we did this by simply unsafe-casting a pointer to
mutable, which violates the aliasing rules in Rust. In a recent
rustc (at least on macOS), this broke.

Here, we convert the stored struct to wrap it in an `UnsafeCell`,
which fixes the issue.
c-bindings-gen/src/main.rs
genbindings.sh