Skip to content

Commit d9f22d2

Browse files
committed
Pattern types have their base type as a field
1 parent e821cb8 commit d9f22d2

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,15 @@ where
839839
| ty::FnDef(..)
840840
| ty::CoroutineWitness(..)
841841
| ty::Foreign(..)
842-
| ty::Pat(_, _)
843842
| ty::Dynamic(_, _) => {
844843
bug!("TyAndLayout::field({:?}): not applicable", this)
845844
}
846845

846+
ty::Pat(base, _) => {
847+
assert_eq!(i, 0);
848+
TyMaybeWithLayout::Ty(base)
849+
}
850+
847851
ty::UnsafeBinder(bound_ty) => {
848852
let ty = tcx.instantiate_bound_regions_with_erased(bound_ty.into());
849853
field_ty_or_layout(TyAndLayout { ty, ..this }, cx, i)

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ fn layout_of_uncached<'tcx>(
350350
ty::PatternKind::Or(..) => bug!("patterns cannot have nested or patterns"),
351351
},
352352
}
353+
// Pattern types contain their base as their sole field.
354+
// This allows the rest of the compiler to process pattern types just like
355+
// single field transparent Adts, and only the parts of the compiler that
356+
// specifically care about pattern types will have to handle it.
357+
layout.fields = FieldsShape::Arbitrary {
358+
offsets: [Size::ZERO].into_iter().collect(),
359+
memory_index: [0].into_iter().collect(),
360+
};
353361
tcx.mk_layout(layout)
354362
}
355363

tests/ui/type/pattern_types/or_patterns.stderr

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ error: layout_of((i8) is (i8::MIN..=-1 | 1..)) = Layout {
5353
valid_range: 1..=255,
5454
},
5555
),
56-
fields: Primitive,
56+
fields: Arbitrary {
57+
offsets: [
58+
Size(0 bytes),
59+
],
60+
memory_index: [
61+
0,
62+
],
63+
},
5764
largest_niche: Some(
5865
Niche {
5966
offset: Size(0 bytes),
@@ -91,7 +98,14 @@ error: layout_of((i8) is (i8::MIN..=-2 | 0..)) = Layout {
9198
valid_range: 0..=254,
9299
},
93100
),
94-
fields: Primitive,
101+
fields: Arbitrary {
102+
offsets: [
103+
Size(0 bytes),
104+
],
105+
memory_index: [
106+
0,
107+
],
108+
},
95109
largest_niche: Some(
96110
Niche {
97111
offset: Size(0 bytes),

tests/ui/type/pattern_types/range_patterns.stderr

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ error: layout_of((u32) is 1..) = Layout {
5757
valid_range: 1..=4294967295,
5858
},
5959
),
60-
fields: Primitive,
60+
fields: Arbitrary {
61+
offsets: [
62+
Size(0 bytes),
63+
],
64+
memory_index: [
65+
0,
66+
],
67+
},
6168
largest_niche: Some(
6269
Niche {
6370
offset: Size(0 bytes),
@@ -390,7 +397,14 @@ error: layout_of((i8) is -10..=10) = Layout {
390397
valid_range: (..=10) | (246..),
391398
},
392399
),
393-
fields: Primitive,
400+
fields: Arbitrary {
401+
offsets: [
402+
Size(0 bytes),
403+
],
404+
memory_index: [
405+
0,
406+
],
407+
},
394408
largest_niche: Some(
395409
Niche {
396410
offset: Size(0 bytes),
@@ -428,7 +442,14 @@ error: layout_of((i8) is i8::MIN..=0) = Layout {
428442
valid_range: (..=0) | (128..),
429443
},
430444
),
431-
fields: Primitive,
445+
fields: Arbitrary {
446+
offsets: [
447+
Size(0 bytes),
448+
],
449+
memory_index: [
450+
0,
451+
],
452+
},
432453
largest_niche: Some(
433454
Niche {
434455
offset: Size(0 bytes),

0 commit comments

Comments
 (0)