Repository navigation
Always translate global objects as static mut - #67
Merged
Merged
Conversation
joaotgouveia
pushed a commit
to joaotgouveia/cpp2rust
that referenced
this pull request
Oct 27, 2026
This is a continuation of Cpp2Rust#65: > This is incomplete, it only solves part of the problem. > > In Rust, `static` global variables require `Sync` so that multiple threads can take an immutable reference to the `static` storage, `static mut` global variables don't require `Sync` because it's by itself unsafe to access the `static mut` storage. > > This PR solved the problem for `static` global struct objects that contained raw pointers. Raw pointers are not `Sync` in Rust so any struct containing them needs to implement the `Sync` trait. What this PR does not solve is global arrays containing pointers in `static` storage, for example `[*const T; N]`. > > One solution would be to translate the const global variables as `const` instead of `static`. However `const` global variables don't have a stable address in the final binary. Other solution would be to wrap the array in a newtype, but this makes later usages of the array harder in the codegen. > > None of the two above solution is complete. For the moment we can translate all global variables as `static mut` instead of `static` so that we don't have to implement `Sync` for them. By construction, the generated code is guaranteed not to modify the global variable, even if it's declared as `static mut` instead of `static`. > > This only touches the unsafe model.
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.
This is a continuation of #65: