Skip to content

Commit aa25f9e

Browse files
authored
Rollup merge of #75099 - davidtwco:is-zst-abstraction-violation, r=eddyb
lint/ty: move fns to avoid abstraction violation This PR moves `transparent_newtype_field` and `is_zst` to `LateContext` where they are used, rather than being on the `VariantDef` and `TyS` types, hopefully addressing @eddyb's concern [from this comment](#74340 (comment)).
2 parents 44be933 + 0f2bd56 commit aa25f9e

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

compiler/rustc_lint/src/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
//! `late_lint_methods!` invocation in `lib.rs`.
2222
2323
use crate::{
24-
types::CItemKind, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
24+
types::{transparent_newtype_field, CItemKind},
25+
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
2526
};
2627
use rustc_ast::attr::{self, HasAttrs};
2728
use rustc_ast::tokenstream::{TokenStream, TokenTree};
@@ -2688,8 +2689,7 @@ impl ClashingExternDeclarations {
26882689
if is_transparent && !is_non_null {
26892690
debug_assert!(def.variants.len() == 1);
26902691
let v = &def.variants[VariantIdx::new(0)];
2691-
ty = v
2692-
.transparent_newtype_field(tcx)
2692+
ty = transparent_newtype_field(tcx, v)
26932693
.expect(
26942694
"single-variant transparent structure with zero-sized field",
26952695
)

compiler/rustc_lint/src/types.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,26 @@ crate fn nonnull_optimization_guaranteed<'tcx>(tcx: TyCtxt<'tcx>, def: &ty::AdtD
639639
.any(|a| tcx.sess.check_name(a, sym::rustc_nonnull_optimization_guaranteed))
640640
}
641641

642+
/// `repr(transparent)` structs can have a single non-ZST field, this function returns that
643+
/// field.
644+
pub fn transparent_newtype_field<'a, 'tcx>(
645+
tcx: TyCtxt<'tcx>,
646+
variant: &'a ty::VariantDef,
647+
) -> Option<&'a ty::FieldDef> {
648+
let param_env = tcx.param_env(variant.def_id);
649+
for field in &variant.fields {
650+
let field_ty = tcx.type_of(field.did);
651+
let is_zst =
652+
tcx.layout_of(param_env.and(field_ty)).map(|layout| layout.is_zst()).unwrap_or(false);
653+
654+
if !is_zst {
655+
return Some(field);
656+
}
657+
}
658+
659+
None
660+
}
661+
642662
/// Is type known to be non-null?
643663
crate fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: CItemKind) -> bool {
644664
let tcx = cx.tcx;
@@ -654,7 +674,7 @@ crate fn ty_is_known_nonnull<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, mode: C
654674
}
655675

656676
for variant in &def.variants {
657-
if let Some(field) = variant.transparent_newtype_field(tcx) {
677+
if let Some(field) = transparent_newtype_field(cx.tcx, variant) {
658678
if ty_is_known_nonnull(cx, field.ty(tcx, substs), mode) {
659679
return true;
660680
}
@@ -675,7 +695,7 @@ fn get_nullable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
675695
ty::Adt(field_def, field_substs) => {
676696
let inner_field_ty = {
677697
let first_non_zst_ty =
678-
field_def.variants.iter().filter_map(|v| v.transparent_newtype_field(tcx));
698+
field_def.variants.iter().filter_map(|v| transparent_newtype_field(cx.tcx, v));
679699
debug_assert_eq!(
680700
first_non_zst_ty.clone().count(),
681701
1,
@@ -816,7 +836,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
816836
if def.repr.transparent() {
817837
// Can assume that only one field is not a ZST, so only check
818838
// that field's type for FFI-safety.
819-
if let Some(field) = variant.transparent_newtype_field(self.cx.tcx) {
839+
if let Some(field) = transparent_newtype_field(self.cx.tcx, variant) {
820840
self.check_field_type_for_ffi(cache, field, substs)
821841
} else {
822842
bug!("malformed transparent type");

compiler/rustc_middle/src/ty/mod.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ pub struct VariantDef {
19991999
flags: VariantFlags,
20002000
}
20012001

2002-
impl<'tcx> VariantDef {
2002+
impl VariantDef {
20032003
/// Creates a new `VariantDef`.
20042004
///
20052005
/// `variant_did` is the `DefId` that identifies the enum variant (if this `VariantDef`
@@ -2065,19 +2065,6 @@ impl<'tcx> VariantDef {
20652065
pub fn is_recovered(&self) -> bool {
20662066
self.flags.intersects(VariantFlags::IS_RECOVERED)
20672067
}
2068-
2069-
/// `repr(transparent)` structs can have a single non-ZST field, this function returns that
2070-
/// field.
2071-
pub fn transparent_newtype_field(&self, tcx: TyCtxt<'tcx>) -> Option<&FieldDef> {
2072-
for field in &self.fields {
2073-
let field_ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, self.def_id));
2074-
if !field_ty.is_zst(tcx, self.def_id) {
2075-
return Some(field);
2076-
}
2077-
}
2078-
2079-
None
2080-
}
20812068
}
20822069

20832070
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]

compiler/rustc_middle/src/ty/sty.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2322,9 +2322,4 @@ impl<'tcx> TyS<'tcx> {
23222322
}
23232323
}
23242324
}
2325-
2326-
/// Is this a zero-sized type?
2327-
pub fn is_zst(&'tcx self, tcx: TyCtxt<'tcx>, did: DefId) -> bool {
2328-
tcx.layout_of(tcx.param_env(did).and(self)).map(|layout| layout.is_zst()).unwrap_or(false)
2329-
}
23302325
}

0 commit comments

Comments
 (0)