From 8fc246251fad28715493d240e6a63a0db237ce7a Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Thu, 10 Dec 2020 00:00:00 +0000 Subject: Types with a hidden niche are not known to be non-null --- compiler/rustc_lint/src/types.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'compiler') diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 9ad9d53cd0d..c1b37f97d78 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -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) { -- cgit 1.4.1-3-g733a5