1
Fork 0

Types with a hidden niche are not known to be non-null

This commit is contained in:
Tomasz Miąsko 2020-12-10 00:00:00 +00:00
parent e413d89aa7
commit 8fc246251f
5 changed files with 154 additions and 47 deletions

View file

@ -659,7 +659,7 @@ pub fn transparent_newtype_field<'a, 'tcx>(
}
/// Is type known to be non-null?
crate fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKind) -> bool {
fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKind) -> bool {
let tcx = cx.tcx;
match ty.kind() {
ty::FnPtr(_) => true,
@ -672,6 +672,12 @@ crate fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: C
return true;
}
// Types with a `#[repr(no_niche)]` attribute have their niche hidden.
// The attribute is used by the UnsafeCell for example (the only use so far).
if def.repr.hide_niche() {
return false;
}
for variant in &def.variants {
if let Some(field) = transparent_newtype_field(cx.tcx, variant) {
if ty_is_known_nonnull(cx, field.ty(tcx, substs), mode) {