transpile: Properly handle address-of static compound literals#1644
transpile: Properly handle address-of static compound literals#1644Rua wants to merge 3 commits intoimmunant:masterfrom
Conversation
dcbe0fa to
8a200eb
Compare
4a06d79 to
b768426
Compare
c2rust-transpile/src/with_stmts.rs
Outdated
| /// If all statements in self.stmts are Item statements, returns the contained Items. | ||
| /// Otherwise, returns None. |
There was a problem hiding this comment.
| /// If all statements in self.stmts are Item statements, returns the contained Items. | |
| /// Otherwise, returns None. | |
| /// If all statements in `self.stmts` are [`Item`]s, returns the contained [`Item`]s. | |
| /// Otherwise, returns [`None`]. |
| val, | ||
| is_unsafe: true, | ||
| } | ||
| } |
There was a problem hiding this comment.
| } | |
| pub fn new_unsafe(stmts: Vec<Stmt>, val: T) -> Self { | |
| WithStmts { | |
| stmts, | |
| val, | |
| is_unsafe: true, | |
| } | |
| } | |
I'm not sure why there aren't newlines between the functions here, but let's fix it for new functions.
There was a problem hiding this comment.
Also, I feel like these 4 functions would be better off as builder-like methods, like this:
pub fn new_val(val: T) -> Self {
WithStmts {
stmts: vec![],
val,
is_unsafe: false,
}
}
pub fn stmts(self, stmts: Vec<Stmt>) -> Self {
Self { stmts, ..self }
}
pub fn is_unsafe(self, is_unsafe: bool) -> Self {
Self { is_unsafe, ..self }
}There was a problem hiding this comment.
WithStmts doesn't use the builder pattern anywhere, so converting it to one seems like a separate refactor that belongs in its own PR?
| #[no_mangle] | ||
| pub static mut static_single_int_ptr: *mut ::core::ffi::c_int = | ||
| &42 as *const ::core::ffi::c_int as *mut ::core::ffi::c_int; | ||
| unsafe { &raw const c2rust_fresh0 as *mut ::core::ffi::c_int }; |
There was a problem hiding this comment.
Why do these need to be unsafe?
There was a problem hiding this comment.
Hmm, I guess taking a raw pointer is not unsafe, only taking a regular reference is.
There was a problem hiding this comment.
It seems that convert_address_of_common doesn't mark its expression as unsafe, and never did even before my refactorings? Even when it's taking the address of a static? How did it work before then?
e380100 to
3e04701
Compare
3e04701 to
e485aae
Compare
WithStmtsin static initialisers to contain statements #1610.This allows limited use of statements in translations of statics, only if the statements are
Stmt::Item. This is then used to solve the original bug, by declaring a fresh static variable to take the address of.This code doesn't currently account for situations in which the compound literal expression needs to be sectioned off for late initialisation, as that would have complicated the code a fair bit further.