Skip to content

Commit c62e36b

Browse files
committed
make rustc_layout also work for type definitions
1 parent d9f60bc commit c62e36b

File tree

3 files changed

+238
-10
lines changed

3 files changed

+238
-10
lines changed

src/librustc_passes/layout_test.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
2929
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
3030
let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
3131

32-
if let ItemKind::TyAlias(..) = item.kind {
33-
for attr in self.tcx.get_attrs(item_def_id).iter() {
34-
if attr.check_name(sym::rustc_layout) {
35-
self.dump_layout_of(item_def_id, item, attr);
32+
match item.kind {
33+
ItemKind::TyAlias(..) |
34+
ItemKind::Enum(..) |
35+
ItemKind::Struct(..) |
36+
ItemKind::Union(..) => {
37+
for attr in self.tcx.get_attrs(item_def_id).iter() {
38+
if attr.check_name(sym::rustc_layout) {
39+
self.dump_layout_of(item_def_id, item, attr);
40+
}
3641
}
3742
}
43+
_ => {}
3844
}
3945
}
4046

src/test/ui/layout/debug.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#![feature(never_type, rustc_attrs)]
22
#![crate_type = "lib"]
33

4-
enum E { Foo, Bar(!, i32, i32) }
4+
#[rustc_layout(debug)]
5+
enum E { Foo, Bar(!, i32, i32) } //~ ERROR: layout debugging
6+
7+
#[rustc_layout(debug)]
8+
struct S { f1: i32, f2: (), f3: i32 } //~ ERROR: layout debugging
9+
10+
#[rustc_layout(debug)]
11+
union U { f1: (i32, i32), f3: i32 } //~ ERROR: layout debugging
512

613
#[rustc_layout(debug)]
7-
type Test = E; //~ ERROR: layout debugging
14+
type Test = Result<i32, i32>; //~ ERROR: layout debugging

src/test/ui/layout/debug.stderr

+219-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,225 @@ error: layout debugging: LayoutDetails {
111111
raw: 12,
112112
},
113113
}
114-
--> $DIR/debug.rs:7:1
114+
--> $DIR/debug.rs:5:1
115115
|
116-
LL | type Test = E;
117-
| ^^^^^^^^^^^^^^
116+
LL | enum E { Foo, Bar(!, i32, i32) }
117+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118118

119-
error: aborting due to previous error
119+
error: layout debugging: LayoutDetails {
120+
fields: Arbitrary {
121+
offsets: [
122+
Size {
123+
raw: 0,
124+
},
125+
Size {
126+
raw: 0,
127+
},
128+
Size {
129+
raw: 4,
130+
},
131+
],
132+
memory_index: [
133+
1,
134+
0,
135+
2,
136+
],
137+
},
138+
variants: Single {
139+
index: 0,
140+
},
141+
abi: ScalarPair(
142+
Scalar {
143+
value: Int(
144+
I32,
145+
true,
146+
),
147+
valid_range: 0..=4294967295,
148+
},
149+
Scalar {
150+
value: Int(
151+
I32,
152+
true,
153+
),
154+
valid_range: 0..=4294967295,
155+
},
156+
),
157+
largest_niche: None,
158+
align: AbiAndPrefAlign {
159+
abi: Align {
160+
pow2: 2,
161+
},
162+
pref: Align {
163+
pow2: 3,
164+
},
165+
},
166+
size: Size {
167+
raw: 8,
168+
},
169+
}
170+
--> $DIR/debug.rs:8:1
171+
|
172+
LL | struct S { f1: i32, f2: (), f3: i32 }
173+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174+
175+
error: layout debugging: LayoutDetails {
176+
fields: Union(
177+
2,
178+
),
179+
variants: Single {
180+
index: 0,
181+
},
182+
abi: Aggregate {
183+
sized: true,
184+
},
185+
largest_niche: None,
186+
align: AbiAndPrefAlign {
187+
abi: Align {
188+
pow2: 2,
189+
},
190+
pref: Align {
191+
pow2: 3,
192+
},
193+
},
194+
size: Size {
195+
raw: 8,
196+
},
197+
}
198+
--> $DIR/debug.rs:11:1
199+
|
200+
LL | union U { f1: (i32, i32), f3: i32 }
201+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
202+
203+
error: layout debugging: LayoutDetails {
204+
fields: Arbitrary {
205+
offsets: [
206+
Size {
207+
raw: 0,
208+
},
209+
],
210+
memory_index: [
211+
0,
212+
],
213+
},
214+
variants: Multiple {
215+
discr: Scalar {
216+
value: Int(
217+
I32,
218+
false,
219+
),
220+
valid_range: 0..=1,
221+
},
222+
discr_kind: Tag,
223+
discr_index: 0,
224+
variants: [
225+
LayoutDetails {
226+
fields: Arbitrary {
227+
offsets: [
228+
Size {
229+
raw: 4,
230+
},
231+
],
232+
memory_index: [
233+
0,
234+
],
235+
},
236+
variants: Single {
237+
index: 0,
238+
},
239+
abi: Aggregate {
240+
sized: true,
241+
},
242+
largest_niche: None,
243+
align: AbiAndPrefAlign {
244+
abi: Align {
245+
pow2: 2,
246+
},
247+
pref: Align {
248+
pow2: 3,
249+
},
250+
},
251+
size: Size {
252+
raw: 8,
253+
},
254+
},
255+
LayoutDetails {
256+
fields: Arbitrary {
257+
offsets: [
258+
Size {
259+
raw: 4,
260+
},
261+
],
262+
memory_index: [
263+
0,
264+
],
265+
},
266+
variants: Single {
267+
index: 1,
268+
},
269+
abi: Aggregate {
270+
sized: true,
271+
},
272+
largest_niche: None,
273+
align: AbiAndPrefAlign {
274+
abi: Align {
275+
pow2: 2,
276+
},
277+
pref: Align {
278+
pow2: 3,
279+
},
280+
},
281+
size: Size {
282+
raw: 8,
283+
},
284+
},
285+
],
286+
},
287+
abi: ScalarPair(
288+
Scalar {
289+
value: Int(
290+
I32,
291+
false,
292+
),
293+
valid_range: 0..=1,
294+
},
295+
Scalar {
296+
value: Int(
297+
I32,
298+
true,
299+
),
300+
valid_range: 0..=4294967295,
301+
},
302+
),
303+
largest_niche: Some(
304+
Niche {
305+
offset: Size {
306+
raw: 0,
307+
},
308+
scalar: Scalar {
309+
value: Int(
310+
I32,
311+
false,
312+
),
313+
valid_range: 0..=1,
314+
},
315+
},
316+
),
317+
align: AbiAndPrefAlign {
318+
abi: Align {
319+
pow2: 2,
320+
},
321+
pref: Align {
322+
pow2: 3,
323+
},
324+
},
325+
size: Size {
326+
raw: 8,
327+
},
328+
}
329+
--> $DIR/debug.rs:14:1
330+
|
331+
LL | type Test = Result<i32, i32>;
332+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
333+
334+
error: aborting due to 4 previous errors
120335

0 commit comments

Comments
 (0)