Skip to content

Correctly document the CRUBIT_THREAD_SAFE annotation. - #1510

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_952220435
Jul 29, 2026
Merged

Correctly document the CRUBIT_THREAD_SAFE annotation.#1510
copybara-service[bot] merged 1 commit into
mainfrom
test_952220435

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Jul 22, 2026

Copy link
Copy Markdown

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!

struct CRUBIT_THREAD_SAFE MyType {
  void Mutate() { mu_.Lock(); x_++; mu_.Unlock();}
  int x_ = 0;  // not mutable
  mutable Mutex mu_;
};
pub fn MyRustFunction(x: &MyType) {
  x.Mutate();
}
const MyType x;
MyRustFunction(x); // UB

@copybara-service
copybara-service Bot force-pushed the test_952220435 branch 2 times, most recently from 6689954 to 72cccdf Compare July 29, 2026 10:18
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
copybara-service Bot merged commit 389b098 into main Jul 29, 2026
@copybara-service
copybara-service Bot deleted the test_952220435 branch July 29, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant