Skip to content

Commit a44d4f0

Browse files
committed
repr_transparent_external_private_fields: special-case some std types
1 parent cdb53d8 commit a44d4f0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::unord::{UnordMap, UnordSet};
55
use rustc_errors::codes::*;
66
use rustc_errors::MultiSpan;
77
use rustc_hir::def::{CtorKind, DefKind};
8-
use rustc_hir::Node;
8+
use rustc_hir::{LangItem, Node};
99
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1010
use rustc_infer::traits::Obligation;
1111
use rustc_lint_defs::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
@@ -1259,6 +1259,17 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
12591259
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
12601260
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
12611261
ty::Adt(def, args) => {
1262+
if matches!(
1263+
tcx.as_lang_item(def.did()),
1264+
Some(LangItem::UnsafeCell | LangItem::ManuallyDrop | LangItem::MaybeUninit)
1265+
) || matches!(
1266+
tcx.get_diagnostic_name(def.did()),
1267+
Some(sym::Cell | sym::SyncUnsafeCell)
1268+
) {
1269+
// These are the types from `std` that guarantee that
1270+
// they have the same layout as `T`.
1271+
return check_non_exhaustive(tcx, args.type_at(0));
1272+
}
12621273
if !def.did().is_local() {
12631274
let non_exhaustive = def.is_variant_list_non_exhaustive()
12641275
|| def
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ check-pass
2+
3+
#![feature(sync_unsafe_cell)]
4+
#![deny(repr_transparent_external_private_fields)]
5+
6+
struct ZST;
7+
8+
#[repr(transparent)]
9+
struct TransparentWithManuallyDropZST {
10+
value: i32,
11+
md: std::mem::ManuallyDrop<ZST>,
12+
mu: std::mem::MaybeUninit<ZST>,
13+
pd: std::marker::PhantomData<ZST>,
14+
pp: std::marker::PhantomPinned,
15+
c: std::cell::Cell<ZST>,
16+
uc: std::cell::UnsafeCell<ZST>,
17+
suc: std::cell::SyncUnsafeCell<ZST>,
18+
zst: ZST,
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)