diff options
| author | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2020-12-10 00:00:00 +0000 |
|---|---|---|
| committer | Tomasz Miąsko <tomasz.miasko@gmail.com> | 2020-12-10 00:00:00 +0000 |
| commit | 8fc246251fad28715493d240e6a63a0db237ce7a (patch) | |
| tree | 430cf8c24972d6c7cf93a3dc2bb11599f67e9911 /compiler/rustc_lint/src | |
| parent | e413d89aa706060ddc347e1e06d551ec86d3f471 (diff) | |
Types with a hidden niche are not known to be non-null
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/types.rs | 8 |
1 files changed, 7 insertions, 1 deletions
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) { |
