Skip to content

Fix -Wmaybe-uninitialized for Vec of a shared struct - #1770

Open
tonycoder-hub wants to merge 1 commit into
dtolnay:masterfrom
tonycoder-hub:gb/vec-maybe-uninitialized
Open

tonycoder-hub wants to merge 1 commit into
dtolnay:masterfrom
tonycoder-hub:gb/vec-maybe-uninitialized

Conversation

@tonycoder-hub

Copy link
Copy Markdown

rust::Vec<T>::Vec() for a shared struct value-initializes through cxxbridge1$rust_vec$...$new(this). The Rust shim writes a new vector through that pointer, but the generated C++ declaration took const rust::Vec<T> *. GCC treats the uninitialized this as a read and emits -Wmaybe-uninitialized. With -Werror, a normal Vec of a shared struct fails to build.

The builtin specializations in src/cxx.cc already declare this pointer without const. The generated declaration now matches.

Fixes #1769

Test plan

  • CXX=g++ cargo test --test cpp_ui_tests test_vec_of_shared_struct_constructor fails before the change with -Werror=maybe-uninitialized and passes after
  • CXX=g++ cargo test --workspace --exclude demo
  • cargo fmt --all -- --check
  • RUSTFLAGS=-Dwarnings cargo clippy --workspace --tests --exclude demo -- -Dclippy::all -Dclippy::pedantic
  • RUSTFLAGS=-Dwarnings cargo clippy --manifest-path demo/Cargo.toml -- -Dclippy::all

This change was prepared with AI assistance and verified by the tests above.

Generated cxxbridge1$rust_vec$...$new took a const pointer, but the Rust
shim writes a new Vec through that pointer. GCC treats the uninitialized
this inside Vec<T>::Vec() as a read and warns, so -Werror fails the build
when a shared struct is used as a Vec element. Declare the pointer
mutable, matching the builtin specializations in cxx.cc.

Fixes dtolnay#1769

Signed-off-by: Tony Coder <407243179@qq.com>

This branch has not been deployed

No deployments
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.

Compilation warnings for generated Vec specializations

1 participant