Skip to content

Commit eddad5f

Browse files
authored
Rollup merge of #155340 - oli-obk:size-skeleton-pat-tys, r=mati865
Handle nonnull pattern types in size skeleton The original comment was correct, the size is always the same, but we have more information now. In theory there was an additional bug that would have allowed transmuting things of different sizes, but I don't see how that would have been actually doable as the `tail` types would always have differed. fixes #155330
2 parents cbd9361 + 8d64264 commit eddad5f

2 files changed

Lines changed: 26 additions & 2 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! After the use of pattern types inside `NonNull`,
2+
//! transmuting between a niche optimized enum wrapping a
3+
//! generic `NonNull` and raw pointers stopped working.
4+
//@ check-pass
5+
6+
use std::ptr::NonNull;
7+
pub const fn is_null<'a, T: ?Sized>(ptr: *const T) -> bool {
8+
unsafe { matches!(core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr), None) }
9+
}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)