Skip to content

Commit c4f39f6

Browse files
committed
Implement wide not-null pointer patterns
1 parent dceee83 commit c4f39f6

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,20 @@ where
822822
| ty::FnDef(..)
823823
| ty::CoroutineWitness(..)
824824
| ty::Foreign(..)
825-
| ty::Pat(_, _)
826825
| ty::Dynamic(_, _, ty::Dyn) => {
827826
bug!("TyAndLayout::field({:?}): not applicable", this)
828827
}
829828

829+
// May contain wide pointers
830+
ty::Pat(base, pat) => match *pat {
831+
ty::PatternKind::NotNull => {
832+
field_ty_or_layout(TyAndLayout { ty: base, ..this }, cx, i)
833+
}
834+
ty::PatternKind::Range { .. } | ty::PatternKind::Or(_) => {
835+
bug!("TyAndLayout::field({this:?}): only applicable to !null patterns")
836+
}
837+
},
838+
830839
ty::UnsafeBinder(bound_ty) => {
831840
let ty = tcx.instantiate_bound_regions_with_erased(bound_ty.into());
832841
field_ty_or_layout(TyAndLayout { ty, ..this }, cx, i)

tests/ui/type/pattern_types/non_null.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type NonNull<T> = pattern_type!(*const T is !null); //~ ERROR layout_of
1010
#[rustc_layout(debug)]
1111
type Test = Option<NonNull<()>>; //~ ERROR layout_of
1212

13+
#[rustc_layout(debug)]
14+
type Wide = pattern_type!(*const [u8] is !null); //~ ERROR layout_of
15+
1316
const _: () = assert!(size_of::<NonNull<()>>() == size_of::<Option<NonNull<()>>>());
1417

1518
fn main() {}

tests/ui/type/pattern_types/non_null.stderr

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,62 @@ error: layout_of(Option<(*const ()) is !null>) = Layout {
157157
LL | type Test = Option<NonNull<()>>;
158158
| ^^^^^^^^^
159159

160-
error: aborting due to 2 previous errors
160+
error: layout_of((*const [u8]) is !null) = Layout {
161+
size: Size(16 bytes),
162+
align: AbiAndPrefAlign {
163+
abi: Align(8 bytes),
164+
pref: Align(8 bytes),
165+
},
166+
backend_repr: ScalarPair(
167+
Initialized {
168+
value: Pointer(
169+
AddressSpace(
170+
0,
171+
),
172+
),
173+
valid_range: 1..=18446744073709551615,
174+
},
175+
Initialized {
176+
value: Int(
177+
I64,
178+
false,
179+
),
180+
valid_range: 0..=18446744073709551615,
181+
},
182+
),
183+
fields: Arbitrary {
184+
offsets: [
185+
Size(0 bytes),
186+
Size(8 bytes),
187+
],
188+
memory_index: [
189+
0,
190+
1,
191+
],
192+
},
193+
largest_niche: Some(
194+
Niche {
195+
offset: Size(0 bytes),
196+
value: Pointer(
197+
AddressSpace(
198+
0,
199+
),
200+
),
201+
valid_range: 1..=18446744073709551615,
202+
},
203+
),
204+
uninhabited: false,
205+
variants: Single {
206+
index: 0,
207+
},
208+
max_repr_align: None,
209+
unadjusted_abi_align: Align(8 bytes),
210+
randomization_seed: 16,
211+
}
212+
--> $DIR/non_null.rs:14:1
213+
|
214+
LL | type Wide = pattern_type!(*const [u8] is !null);
215+
| ^^^^^^^^^
216+
217+
error: aborting due to 3 previous errors
161218

0 commit comments

Comments
 (0)