You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structNeedAlign(u64);implDropforNeedAlign{fndrop(&mutself){assert_eq!(selfas*mut _ asusize % 8,0);}}#[repr(packed)]#[derive(Copy,Clone)]structUnalign<T>(T);structBreakit{x:u8,y:Unalign<NeedAlign>,}fnmain(){println!("before");{let x = NeedAlign(0);}println!("middle");{let x = Breakit{x:0,y:Unalign(NeedAlign(0))};}println!("after");}
This could lead to UB, because code for drop is generated under the assumption that the pointer is aligned.
Related to #27060, but not solved by making taking references to field of packed structs unsafe.
We furthermore have to forbid using types which implement Drop in a packed struct.
Open question:
How is this supposed to handle generic types, like in the example above? Error on declaration (T may implement Drop), or only error when Unalign is actually used with a type that implements Drop?
IIRC unions also have a no-drop restriction, so this should probably use the same rules.
The text was updated successfully, but these errors were encountered:
retep998
added
the
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
label
Jul 18, 2017
The union/destructor restriction is more like a lint than a strong restriction.
I think we might want a "finer" solution here, because people might have unaligned Box<T>/Rc<T>/etc., and we won't want them to be "use unsafe for instant UB".
The following test fails:
This could lead to UB, because code for
drop
is generated under the assumption that the pointer is aligned.Related to #27060, but not solved by making taking references to field of packed structs unsafe.
We furthermore have to forbid using types which implement
Drop
in a packed struct.Open question:
How is this supposed to handle generic types, like in the example above? Error on declaration (
T
may implementDrop
), or only error whenUnalign
is actually used with a type that implementsDrop
?IIRC unions also have a no-drop restriction, so this should probably use the same rules.
The text was updated successfully, but these errors were encountered: