-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #112062 - lukas-code:unsized-layout, r=wesleywiser
Make struct layout not depend on unsizeable tail fixes (after backport) #112048 Since unsizing `Ptr<Foo<T>>` -> `Ptr<Foo<U>` just copies the pointer and adds the metadata, the layout of `Foo` must not depend on niches in and alignment of the tail `T`. Nominating for beta 1.71, because it will have this issue: `@rustbot` label beta-nominated
- Loading branch information
Showing
3 changed files
with
109 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// run-pass | ||
|
||
// Check that unsizing doesn't reorder fields. | ||
|
||
#![allow(dead_code)] | ||
|
||
use std::fmt::Debug; | ||
|
||
#[derive(Debug)] | ||
struct GcNode<T: ?Sized> { | ||
gets_swapped_with_next: usize, | ||
next: Option<&'static GcNode<dyn Debug>>, | ||
tail: T, | ||
} | ||
|
||
fn main() { | ||
let node: Box<GcNode<dyn Debug>> = Box::new(GcNode { | ||
gets_swapped_with_next: 42, | ||
next: None, | ||
tail: Box::new(1), | ||
}); | ||
|
||
assert_eq!(node.gets_swapped_with_next, 42); | ||
assert!(node.next.is_none()); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-pass | ||
|
||
// Check that unsizing does not change which field is considered for niche layout. | ||
|
||
#![feature(offset_of)] | ||
#![allow(dead_code)] | ||
|
||
#[derive(Clone)] | ||
struct WideptrField<T: ?Sized> { | ||
first: usize, | ||
second: usize, | ||
niche: NicheAtEnd, | ||
tail: T, | ||
} | ||
|
||
#[derive(Clone)] | ||
#[repr(C)] | ||
struct NicheAtEnd { | ||
arr: [u8; 7], | ||
b: bool, | ||
} | ||
|
||
type Tail = [bool; 8]; | ||
|
||
fn main() { | ||
assert_eq!( | ||
core::mem::offset_of!(WideptrField<Tail>, niche), | ||
core::mem::offset_of!(WideptrField<dyn Send>, niche) | ||
); | ||
} |