Skip to content

Commit d549b8a

Browse files
committed
bug: nesting pattern types in enums doesn't use layout optimizations
1 parent bdbc26b commit d549b8a

File tree

5 files changed

+360
-6
lines changed

5 files changed

+360
-6
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ pub enum TyKind {
21352135
Err,
21362136
/// Placeholder for a `va_list`.
21372137
CVarArgs,
2138-
/// Pattern types like `u32 as 1..=`, which is the same as `NonZeroU32`,
2138+
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
21392139
/// just as part of the type system.
21402140
Pat(P<Ty>, P<Pat>),
21412141
}

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@ pub enum TyKind<'hir> {
26212621
Infer,
26222622
/// Placeholder for a type that has failed to be defined.
26232623
Err(rustc_span::ErrorGuaranteed),
2624-
/// Pattern types (`u32 as 1..`)
2624+
/// Pattern types (`pattern_type!(u32 is 1..)`)
26252625
Pat(&'hir Ty<'hir>, &'hir Pat<'hir>),
26262626
}
26272627

tests/ui/type/pattern_types/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![allow(incomplete_features)]
55

66
// Check that pattern types do not affect existing macros.
7-
// They don't, because `is` was never legal after `ty` fragments.
7+
// They don't, because pattern types don't have surface syntax.
88

99
macro_rules! foo {
1010
($t:ty is $p:pat) => {}; //~ ERROR `$t:ty` is followed by `is`, which is not allowed for `ty` fragments
+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
#![feature(pattern_types)]
1+
#![feature(pattern_types, rustc_attrs)]
22
#![feature(core_pattern_type)]
3+
#![feature(core_pattern_types)]
34
#![allow(incomplete_features)]
45

5-
// check-pass
6+
use std::pat::pattern_type;
7+
8+
#[rustc_layout(debug)]
9+
type X = std::num::NonZeroU32; //~ ERROR layout_of
10+
#[rustc_layout(debug)]
11+
type Y = pattern_type!(u32 is 1..); //~ ERROR layout_of
12+
#[rustc_layout(debug)]
13+
type Z = Option<pattern_type!(u32 is 1..)>; //~ ERROR layout_of
14+
#[rustc_layout(debug)]
15+
type A = Option<std::num::NonZeroU32>; //~ ERROR layout_of
16+
#[rustc_layout(debug)]
17+
struct NonZeroU32New(pattern_type!(u32 is 1..)); //~ ERROR layout_of
618

719
fn main() {
8-
let x: std::pat::pattern_type!(u32 is 1..) = todo!();
20+
let x: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(42_u32) };
921
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
error: layout_of(NonZeroU32) = Layout {
2+
size: Size(4 bytes),
3+
align: AbiAndPrefAlign {
4+
abi: Align(4 bytes),
5+
pref: Align(8 bytes),
6+
},
7+
abi: Scalar(
8+
Initialized {
9+
value: Int(
10+
I32,
11+
false,
12+
),
13+
valid_range: 1..=4294967295,
14+
},
15+
),
16+
fields: Arbitrary {
17+
offsets: [
18+
Size(0 bytes),
19+
],
20+
memory_index: [
21+
0,
22+
],
23+
},
24+
largest_niche: Some(
25+
Niche {
26+
offset: Size(0 bytes),
27+
value: Int(
28+
I32,
29+
false,
30+
),
31+
valid_range: 1..=4294967295,
32+
},
33+
),
34+
variants: Single {
35+
index: 0,
36+
},
37+
max_repr_align: None,
38+
unadjusted_abi_align: Align(4 bytes),
39+
}
40+
--> $DIR/range_patterns.rs:9:1
41+
|
42+
LL | type X = std::num::NonZeroU32;
43+
| ^^^^^^
44+
45+
error: layout_of((u32) is 1..=) = Layout {
46+
size: Size(4 bytes),
47+
align: AbiAndPrefAlign {
48+
abi: Align(4 bytes),
49+
pref: Align(4 bytes),
50+
},
51+
abi: Scalar(
52+
Initialized {
53+
value: Int(
54+
I32,
55+
false,
56+
),
57+
valid_range: 1..=4294967295,
58+
},
59+
),
60+
fields: Primitive,
61+
largest_niche: None,
62+
variants: Single {
63+
index: 0,
64+
},
65+
max_repr_align: None,
66+
unadjusted_abi_align: Align(4 bytes),
67+
}
68+
--> $DIR/range_patterns.rs:11:1
69+
|
70+
LL | type Y = pattern_type!(u32 is 1..);
71+
| ^^^^^^
72+
73+
error: layout_of(Option<(u32) is 1..=>) = Layout {
74+
size: Size(8 bytes),
75+
align: AbiAndPrefAlign {
76+
abi: Align(4 bytes),
77+
pref: Align(8 bytes),
78+
},
79+
abi: ScalarPair(
80+
Initialized {
81+
value: Int(
82+
I32,
83+
false,
84+
),
85+
valid_range: 0..=1,
86+
},
87+
Union {
88+
value: Int(
89+
I32,
90+
false,
91+
),
92+
},
93+
),
94+
fields: Arbitrary {
95+
offsets: [
96+
Size(0 bytes),
97+
],
98+
memory_index: [
99+
0,
100+
],
101+
},
102+
largest_niche: Some(
103+
Niche {
104+
offset: Size(0 bytes),
105+
value: Int(
106+
I32,
107+
false,
108+
),
109+
valid_range: 0..=1,
110+
},
111+
),
112+
variants: Multiple {
113+
tag: Initialized {
114+
value: Int(
115+
I32,
116+
false,
117+
),
118+
valid_range: 0..=1,
119+
},
120+
tag_encoding: Direct,
121+
tag_field: 0,
122+
variants: [
123+
Layout {
124+
size: Size(4 bytes),
125+
align: AbiAndPrefAlign {
126+
abi: Align(1 bytes),
127+
pref: Align(8 bytes),
128+
},
129+
abi: Aggregate {
130+
sized: true,
131+
},
132+
fields: Arbitrary {
133+
offsets: [],
134+
memory_index: [],
135+
},
136+
largest_niche: None,
137+
variants: Single {
138+
index: 0,
139+
},
140+
max_repr_align: None,
141+
unadjusted_abi_align: Align(1 bytes),
142+
},
143+
Layout {
144+
size: Size(8 bytes),
145+
align: AbiAndPrefAlign {
146+
abi: Align(4 bytes),
147+
pref: Align(8 bytes),
148+
},
149+
abi: ScalarPair(
150+
Initialized {
151+
value: Int(
152+
I32,
153+
false,
154+
),
155+
valid_range: 0..=1,
156+
},
157+
Union {
158+
value: Int(
159+
I32,
160+
false,
161+
),
162+
},
163+
),
164+
fields: Arbitrary {
165+
offsets: [
166+
Size(4 bytes),
167+
],
168+
memory_index: [
169+
0,
170+
],
171+
},
172+
largest_niche: None,
173+
variants: Single {
174+
index: 1,
175+
},
176+
max_repr_align: None,
177+
unadjusted_abi_align: Align(4 bytes),
178+
},
179+
],
180+
},
181+
max_repr_align: None,
182+
unadjusted_abi_align: Align(4 bytes),
183+
}
184+
--> $DIR/range_patterns.rs:13:1
185+
|
186+
LL | type Z = Option<pattern_type!(u32 is 1..)>;
187+
| ^^^^^^
188+
189+
error: layout_of(Option<NonZeroU32>) = Layout {
190+
size: Size(4 bytes),
191+
align: AbiAndPrefAlign {
192+
abi: Align(4 bytes),
193+
pref: Align(8 bytes),
194+
},
195+
abi: Scalar(
196+
Initialized {
197+
value: Int(
198+
I32,
199+
false,
200+
),
201+
valid_range: (..=0) | (1..),
202+
},
203+
),
204+
fields: Arbitrary {
205+
offsets: [
206+
Size(0 bytes),
207+
],
208+
memory_index: [
209+
0,
210+
],
211+
},
212+
largest_niche: None,
213+
variants: Multiple {
214+
tag: Initialized {
215+
value: Int(
216+
I32,
217+
false,
218+
),
219+
valid_range: (..=0) | (1..),
220+
},
221+
tag_encoding: Niche {
222+
untagged_variant: 1,
223+
niche_variants: 0..=0,
224+
niche_start: 0,
225+
},
226+
tag_field: 0,
227+
variants: [
228+
Layout {
229+
size: Size(0 bytes),
230+
align: AbiAndPrefAlign {
231+
abi: Align(1 bytes),
232+
pref: Align(8 bytes),
233+
},
234+
abi: Aggregate {
235+
sized: true,
236+
},
237+
fields: Arbitrary {
238+
offsets: [],
239+
memory_index: [],
240+
},
241+
largest_niche: None,
242+
variants: Single {
243+
index: 0,
244+
},
245+
max_repr_align: None,
246+
unadjusted_abi_align: Align(1 bytes),
247+
},
248+
Layout {
249+
size: Size(4 bytes),
250+
align: AbiAndPrefAlign {
251+
abi: Align(4 bytes),
252+
pref: Align(8 bytes),
253+
},
254+
abi: Scalar(
255+
Initialized {
256+
value: Int(
257+
I32,
258+
false,
259+
),
260+
valid_range: 1..=4294967295,
261+
},
262+
),
263+
fields: Arbitrary {
264+
offsets: [
265+
Size(0 bytes),
266+
],
267+
memory_index: [
268+
0,
269+
],
270+
},
271+
largest_niche: Some(
272+
Niche {
273+
offset: Size(0 bytes),
274+
value: Int(
275+
I32,
276+
false,
277+
),
278+
valid_range: 1..=4294967295,
279+
},
280+
),
281+
variants: Single {
282+
index: 1,
283+
},
284+
max_repr_align: None,
285+
unadjusted_abi_align: Align(4 bytes),
286+
},
287+
],
288+
},
289+
max_repr_align: None,
290+
unadjusted_abi_align: Align(4 bytes),
291+
}
292+
--> $DIR/range_patterns.rs:15:1
293+
|
294+
LL | type A = Option<std::num::NonZeroU32>;
295+
| ^^^^^^
296+
297+
error: layout_of(NonZeroU32New) = Layout {
298+
size: Size(4 bytes),
299+
align: AbiAndPrefAlign {
300+
abi: Align(4 bytes),
301+
pref: Align(8 bytes),
302+
},
303+
abi: Scalar(
304+
Initialized {
305+
value: Int(
306+
I32,
307+
false,
308+
),
309+
valid_range: 1..=4294967295,
310+
},
311+
),
312+
fields: Arbitrary {
313+
offsets: [
314+
Size(0 bytes),
315+
],
316+
memory_index: [
317+
0,
318+
],
319+
},
320+
largest_niche: Some(
321+
Niche {
322+
offset: Size(0 bytes),
323+
value: Int(
324+
I32,
325+
false,
326+
),
327+
valid_range: 1..=4294967295,
328+
},
329+
),
330+
variants: Single {
331+
index: 0,
332+
},
333+
max_repr_align: None,
334+
unadjusted_abi_align: Align(4 bytes),
335+
}
336+
--> $DIR/range_patterns.rs:17:1
337+
|
338+
LL | struct NonZeroU32New(pattern_type!(u32 is 1..));
339+
| ^^^^^^^^^^^^^^^^^^^^
340+
341+
error: aborting due to 5 previous errors
342+

0 commit comments

Comments
 (0)