Skip to content

Commit 8d64264

Browse files
committed
Handle non-null pattern types in size skeleton
1 parent 82545d7 commit 8d64264

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,21 @@ impl<'tcx> SizeSkeleton<'tcx> {
508508
}
509509
}
510510

511-
// Pattern types are always the same size as their base.
512-
ty::Pat(base, _) => SizeSkeleton::compute(base, tcx, typing_env),
511+
ty::Pat(base, pat) => {
512+
// Pattern types are always the same size as their base.
513+
let base = SizeSkeleton::compute(base, tcx, typing_env);
514+
match *pat {
515+
ty::PatternKind::Range { .. } | ty::PatternKind::Or(_) => base,
516+
// But in the case of `!null` patterns we need to note that in the
517+
// raw pointer.
518+
ty::PatternKind::NotNull => match base? {
519+
SizeSkeleton::Known(..) | SizeSkeleton::Generic(_) => base,
520+
SizeSkeleton::Pointer { non_zero: _, tail } => {
521+
Ok(SizeSkeleton::Pointer { non_zero: true, tail })
522+
}
523+
},
524+
}
525+
}
513526

514527
_ => Err(err),
515528
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! After the use of pattern types inside `NonNull`,
22
//! transmuting between a niche optimized enum wrapping a
33
//! generic `NonNull` and raw pointers stopped working.
4+
//@ check-pass
45

56
use std::ptr::NonNull;
67
pub const fn is_null<'a, T: ?Sized>(ptr: *const T) -> bool {
78
unsafe { matches!(core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr), None) }
8-
//~^ ERROR: cannot transmute
99
}
1010

1111
fn main() {}

tests/ui/transmute/raw-ptr-non-null.stderr

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)