Skip to content

Commit

Permalink
Handle structs with zst members.
Browse files Browse the repository at this point in the history
  • Loading branch information
jumbatm committed Jul 14, 2020
1 parent bc4819d commit 9182910
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_index::vec::Idx;
use rustc_middle::mir::interpret::{sign_extend, truncate};
use rustc_middle::ty::layout::{IntegerExt, SizeSkeleton};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, Ty, TyCtxt, TypeFoldable};
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
use rustc_span::source_map;
use rustc_span::symbol::sym;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -550,25 +550,26 @@ fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
_ => false,
}
}
/// Given a potentially non-null type `ty`, return its default, nullable type.
fn get_nullable_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
match ty.kind {
/// Given a non-null scalar (or transparent) type `ty`, return the nullable version of that type.
/// If the type passed in was not scalar, returns None.
fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
let tcx = cx.tcx;
Some(match ty.kind {
ty::Adt(field_def, field_substs) => {
let field_variants = &field_def.variants;
// We hit this case for #[repr(transparent)] structs with a single
// field.
debug_assert!(
field_variants.len() == 1 && field_variants[VariantIdx::new(0)].fields.len() == 1,
"inner ty not a newtype struct"
);
debug_assert!(field_def.repr.transparent(), "inner ty not transparent");
// As it's easy to get this wrong, it's worth noting that
// `inner_field_ty` is not the same as `field_ty`: Given Option<S>,
// where S is a transparent newtype of some type T, `field_ty`
// gives us S, while `inner_field_ty` is T.
let inner_field_ty =
field_def.variants[VariantIdx::new(0)].fields[0].ty(tcx, field_substs);
get_nullable_type(tcx, inner_field_ty)
let inner_field_ty = {
let mut first_non_zst_ty = field_def.all_fields().filter_map(|f| {
let field_ty =
tcx.normalize_erasing_regions(cx.param_env, f.ty(tcx, field_substs));
if !field_ty.is_zst(tcx, f.did) { Some(field_ty) } else { None }
});
debug_assert_eq!(
first_non_zst_ty.clone().count(),
1,
"Wrong number of fields for transparent type"
);
first_non_zst_ty.next().expect("No non-zst fields in transparent type.")
};
return get_nullable_type(cx, inner_field_ty);
}
ty::Int(ty) => tcx.mk_mach_int(ty),
ty::Uint(ty) => tcx.mk_mach_uint(ty),
Expand All @@ -585,9 +586,13 @@ fn get_nullable_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
// We should only ever reach this case if ty_is_known_nonnull is extended
// to other types.
ref unhandled => {
unreachable!("Unhandled scalar kind: {:?} while checking {:?}", unhandled, ty)
debug!(
"get_nullable_type: Unhandled scalar kind: {:?} while checking {:?}",
unhandled, ty
);
return None;
}
}
})
}

/// Check if this `ty` can be safely exported based on the "nullable pointer optimization".
Expand Down Expand Up @@ -633,9 +638,9 @@ crate fn repr_nullable_ptr<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option
let field_ty_abi = &cx.layout_of(field_ty).unwrap().abi;
if let Abi::Scalar(field_ty_scalar) = field_ty_abi {
match (field_ty_scalar.valid_range.start(), field_ty_scalar.valid_range.end()) {
(0, _) => bug!("Non-null optimisation extended to a non-zero value."),
(0, _) => unreachable!("Non-null optimisation extended to a non-zero value."),
(1, _) => {
return Some(get_nullable_type(cx.tcx, field_ty));
return Some(get_nullable_type(cx, field_ty).unwrap());
}
(start, end) => unreachable!("Unhandled start and end range: ({}, {})", start, end),
};
Expand Down

0 comments on commit 9182910

Please sign in to comment.