Auto merge of #79804 - tmiasko:improper-ctypes-no-niche, r=pnkfelix

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

Fixes #79787.
This commit is contained in:
bors 2021-02-10 12:56:09 +00:00
commit 07194ffcd2
5 changed files with 154 additions and 47 deletions

View file

@ -672,7 +672,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,
@ -685,6 +685,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) {