-
Notifications
You must be signed in to change notification settings - Fork 13.5k
distinguish "no data" from "heterogeneous" in ABI #57645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 1 commit into
rust-lang:master
from
nikomatsakis:issue-56877-abi-aggregates
Jan 26, 2019
+492
−20
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use rustc::hir; | ||
use rustc::hir::def_id::DefId; | ||
use rustc::hir::itemlikevisit::ItemLikeVisitor; | ||
use rustc::hir::ItemKind; | ||
use rustc::ty::layout::HasDataLayout; | ||
use rustc::ty::layout::HasTyCtxt; | ||
use rustc::ty::layout::LayoutOf; | ||
use rustc::ty::layout::TargetDataLayout; | ||
use rustc::ty::layout::TyLayout; | ||
use rustc::ty::ParamEnv; | ||
use rustc::ty::Ty; | ||
use rustc::ty::TyCtxt; | ||
use syntax::ast::Attribute; | ||
|
||
pub fn test_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { | ||
if tcx.features().rustc_attrs { | ||
// if the `rustc_attrs` feature is not enabled, don't bother testing layout | ||
tcx.hir() | ||
.krate() | ||
.visit_all_item_likes(&mut VarianceTest { tcx }); | ||
} | ||
} | ||
|
||
struct VarianceTest<'a, 'tcx: 'a> { | ||
tcx: TyCtxt<'a, 'tcx, 'tcx>, | ||
} | ||
|
||
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> { | ||
fn visit_item(&mut self, item: &'tcx hir::Item) { | ||
let item_def_id = self.tcx.hir().local_def_id(item.id); | ||
|
||
if let ItemKind::Ty(..) = item.node { | ||
for attr in self.tcx.get_attrs(item_def_id).iter() { | ||
if attr.check_name("rustc_layout") { | ||
self.dump_layout_of(item_def_id, item, attr); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) {} | ||
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) {} | ||
} | ||
|
||
impl<'a, 'tcx> VarianceTest<'a, 'tcx> { | ||
fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item, attr: &Attribute) { | ||
let tcx = self.tcx; | ||
let param_env = self.tcx.param_env(item_def_id); | ||
let ty = self.tcx.type_of(item_def_id); | ||
match self.tcx.layout_of(param_env.and(ty)) { | ||
Ok(ty_layout) => { | ||
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump. | ||
// The `..` are the names of fields to dump. | ||
let meta_items = attr.meta_item_list().unwrap_or_default(); | ||
for meta_item in meta_items { | ||
let name = meta_item.word().map(|mi| mi.name().as_str()); | ||
let name = name.as_ref().map(|s| &s[..]).unwrap_or(""); | ||
|
||
match name { | ||
"abi" => { | ||
self.tcx | ||
.sess | ||
.span_err(item.span, &format!("abi: {:?}", ty_layout.abi)); | ||
} | ||
|
||
"align" => { | ||
self.tcx | ||
.sess | ||
.span_err(item.span, &format!("align: {:?}", ty_layout.align)); | ||
} | ||
|
||
"size" => { | ||
self.tcx | ||
.sess | ||
.span_err(item.span, &format!("size: {:?}", ty_layout.size)); | ||
} | ||
|
||
"homogeneous_aggregate" => { | ||
self.tcx.sess.span_err( | ||
item.span, | ||
&format!( | ||
"homogeneous_aggregate: {:?}", | ||
ty_layout | ||
.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }), | ||
), | ||
); | ||
} | ||
|
||
_ => { | ||
self.tcx.sess.span_err( | ||
meta_item.span, | ||
&format!("unrecognized field name `{}`", name), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Err(layout_error) => { | ||
self.tcx | ||
.sess | ||
.span_err(item.span, &format!("layout error: {:?}", layout_error)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct UnwrapLayoutCx<'me, 'tcx> { | ||
tcx: TyCtxt<'me, 'tcx, 'tcx>, | ||
param_env: ParamEnv<'tcx>, | ||
} | ||
|
||
impl<'me, 'tcx> LayoutOf for UnwrapLayoutCx<'me, 'tcx> { | ||
type Ty = Ty<'tcx>; | ||
type TyLayout = TyLayout<'tcx>; | ||
|
||
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout { | ||
self.tcx.layout_of(self.param_env.and(ty)).unwrap() | ||
} | ||
} | ||
|
||
impl<'me, 'tcx> HasTyCtxt<'tcx> for UnwrapLayoutCx<'me, 'tcx> { | ||
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { | ||
self.tcx | ||
} | ||
} | ||
|
||
impl<'me, 'tcx> HasDataLayout for UnwrapLayoutCx<'me, 'tcx> { | ||
fn data_layout(&self) -> &TargetDataLayout { | ||
self.tcx.data_layout() | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
36 changes: 36 additions & 0 deletions
36
src/test/ui/layout/homogeneous-aggr-zero-sized-c-struct.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
// Show that `homogeneous_aggregate` code ignores zero-length C | ||
// arrays. This matches the recent C standard, though not the | ||
// behavior of all older compilers, which somtimes consider `T[0]` to | ||
// be a "flexible array member" (see discussion on #56877 for | ||
// details). | ||
|
||
#[repr(C)] | ||
pub struct Foo { | ||
x: u32 | ||
} | ||
|
||
#[repr(C)] | ||
pub struct Middle { | ||
pub a: f32, | ||
pub foo: [Foo; 0], | ||
pub b: f32, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type TestMiddle = Middle; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
#[repr(C)] | ||
pub struct Final { | ||
pub a: f32, | ||
pub b: f32, | ||
pub foo: [Foo; 0], | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type TestFinal = Final; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
fn main() { } |
14 changes: 14 additions & 0 deletions
14
src/test/ui/layout/homogeneous-aggr-zero-sized-c-struct.stderr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-c-struct.rs:22:1 | ||
| | ||
LL | pub type TestMiddle = Middle; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-c-struct.rs:33:1 | ||
| | ||
LL | pub type TestFinal = Final; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
73 changes: 73 additions & 0 deletions
73
src/test/ui/layout/homogeneous-aggr-zero-sized-repr-rust.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
// Regression test for #56877. We want to ensure that the presence of | ||
// `PhantomData` does not prevent `Bar` from being considered a | ||
// homogeneous aggregate. | ||
|
||
#[repr(C)] | ||
pub struct BaseCase { | ||
pub a: f32, | ||
pub b: f32, | ||
} | ||
|
||
#[repr(C)] | ||
pub struct WithPhantomData { | ||
pub a: f32, | ||
pub b: f32, | ||
pub _unit: std::marker::PhantomData<()>, | ||
} | ||
|
||
pub struct EmptyRustStruct { | ||
} | ||
|
||
#[repr(C)] | ||
pub struct WithEmptyRustStruct { | ||
pub a: f32, | ||
pub b: f32, | ||
pub _unit: EmptyRustStruct, | ||
} | ||
|
||
pub struct TransitivelyEmptyRustStruct { | ||
field: EmptyRustStruct, | ||
array: [u32; 0], | ||
} | ||
|
||
#[repr(C)] | ||
pub struct WithTransitivelyEmptyRustStruct { | ||
pub a: f32, | ||
pub b: f32, | ||
pub _unit: TransitivelyEmptyRustStruct, | ||
} | ||
|
||
pub enum EmptyRustEnum { | ||
Dummy, | ||
} | ||
|
||
#[repr(C)] | ||
pub struct WithEmptyRustEnum { | ||
pub a: f32, | ||
pub b: f32, | ||
pub _unit: EmptyRustEnum, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type Test1 = BaseCase; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type Test2 = WithPhantomData; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type Test3 = WithEmptyRustStruct; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type Test4 = WithTransitivelyEmptyRustStruct; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
pub type Test5 = WithEmptyRustEnum; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
|
||
fn main() { } |
32 changes: 32 additions & 0 deletions
32
src/test/ui/layout/homogeneous-aggr-zero-sized-repr-rust.stderr
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-repr-rust.rs:54:1 | ||
| | ||
LL | pub type Test1 = BaseCase; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-repr-rust.rs:58:1 | ||
| | ||
LL | pub type Test2 = WithPhantomData; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-repr-rust.rs:62:1 | ||
| | ||
LL | pub type Test3 = WithEmptyRustStruct; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-repr-rust.rs:66:1 | ||
| | ||
LL | pub type Test4 = WithTransitivelyEmptyRustStruct; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/homogeneous-aggr-zero-sized-repr-rust.rs:70:1 | ||
| | ||
LL | pub type Test5 = WithEmptyRustEnum; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#![feature(rustc_attrs)] | ||
|
||
// Various tests around the behavior of zero-sized arrays and | ||
// unions. This matches the behavior of modern C compilers, though | ||
// older compilers (and sometimes clang) treat `T[0]` as a "flexible | ||
// array member". See more | ||
// details in #56877. | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct Empty { } | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct Empty2 { | ||
e: Empty | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct Empty3 { | ||
z: [f32; 0], | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct Empty4 { | ||
e: Empty3 | ||
} | ||
|
||
#[repr(C)] | ||
union U1 { | ||
s: Empty | ||
} | ||
|
||
#[repr(C)] | ||
union U2 { | ||
s: Empty2 | ||
} | ||
|
||
#[repr(C)] | ||
union U3 { | ||
s: Empty3 | ||
} | ||
|
||
#[repr(C)] | ||
union U4 { | ||
s: Empty4 | ||
} | ||
|
||
#[repr(C)] | ||
struct Baz1 { | ||
x: f32, | ||
y: f32, | ||
u: U1, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
type TestBaz1 = Baz1; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
#[repr(C)] | ||
struct Baz2 { | ||
x: f32, | ||
y: f32, | ||
u: U2, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
type TestBaz2 = Baz2; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
#[repr(C)] | ||
struct Baz3 { | ||
x: f32, | ||
y: f32, | ||
u: U3, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
type TestBaz3 = Baz3; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
#[repr(C)] | ||
struct Baz4 { | ||
x: f32, | ||
y: f32, | ||
u: U4, | ||
} | ||
|
||
#[rustc_layout(homogeneous_aggregate)] | ||
type TestBaz4 = Baz4; | ||
//~^ ERROR homogeneous_aggregate: Homogeneous | ||
|
||
fn main() { } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/zero-sized-array-union.rs:59:1 | ||
| | ||
LL | type TestBaz1 = Baz1; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/zero-sized-array-union.rs:70:1 | ||
| | ||
LL | type TestBaz2 = Baz2; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/zero-sized-array-union.rs:81:1 | ||
| | ||
LL | type TestBaz3 = Baz3; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: homogeneous_aggregate: Homogeneous(Reg { kind: Float, size: Size { raw: 4 } }) | ||
--> $DIR/zero-sized-array-union.rs:92:1 | ||
| | ||
LL | type TestBaz4 = Baz4; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.