Correctly document the CRUBIT_THREAD_SAFE annotation. - #1510
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_952220435
branch
2 times, most recently
from
July 29, 2026 10:18
6689954 to
72cccdf
Compare
The issue is that if the fields are not marked mutable, then it is UB to store it in a const object and pass it to Rust.
You might say, "why store it as a const object?" but C++ programmers sometimes store anything as const that they can, and if their only use of it ends up being passing it to Rust, and Rust only requires a const reference... oops!
```c++
struct CRUBIT_THREAD_SAFE MyType {
void Mutate() { mu_.Lock(); x_++; mu_.Unlock();}
int x_ = 0; // not mutable
mutable Mutex mu_;
};
```
```rust
pub fn MyRustFunction(x: &MyType) {
x.Mutate();
}
```
```c++
const MyType x;
MyRustFunction(x); // UB
```
PiperOrigin-RevId: 955780479
copybara-service
Bot
force-pushed
the
test_952220435
branch
from
July 29, 2026 10:34
72cccdf to
389b098
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Correctly document the CRUBIT_THREAD_SAFE annotation.
The issue is that if the fields are not marked mutable, then it is UB to store it in a const object and pass it to Rust.
You might say, "why store it as a const object?" but C++ programmers sometimes store anything as const that they can, and if their only use of it ends up being passing it to Rust, and Rust only requires a const reference... oops!