Skip to content

Commit 8a48557

Browse files
Rollup merge of #99020 - fee1-dead-contrib:repr_transparent_non_exhaustive, r=oli-obk
check non_exhaustive attr and private fields for transparent types Fixes #78586.
2 parents 0b3644e + 1d26006 commit 8a48557

File tree

5 files changed

+362
-5
lines changed

5 files changed

+362
-5
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+55
Original file line numberDiff line numberDiff line change
@@ -3132,6 +3132,60 @@ declare_lint! {
31323132
"detects unexpected names and values in `#[cfg]` conditions",
31333133
}
31343134

3135+
declare_lint! {
3136+
/// The `repr_transparent_external_private_fields` lint
3137+
/// detects types marked `#[repr(transparent)]` that (transitively)
3138+
/// contain an external ZST type marked `#[non_exhaustive]` or containing
3139+
/// private fields
3140+
///
3141+
/// ### Example
3142+
///
3143+
/// ```rust,ignore (needs external crate)
3144+
/// #![deny(repr_transparent_external_private_fields)]
3145+
/// use foo::NonExhaustiveZst;
3146+
///
3147+
/// #[repr(transparent)]
3148+
/// struct Bar(u32, ([u32; 0], NonExhaustiveZst));
3149+
/// ```
3150+
///
3151+
/// This will produce:
3152+
///
3153+
/// ```text
3154+
/// error: zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
3155+
/// --> src/main.rs:5:28
3156+
/// |
3157+
/// 5 | struct Bar(u32, ([u32; 0], NonExhaustiveZst));
3158+
/// | ^^^^^^^^^^^^^^^^
3159+
/// |
3160+
/// note: the lint level is defined here
3161+
/// --> src/main.rs:1:9
3162+
/// |
3163+
/// 1 | #![deny(repr_transparent_external_private_fields)]
3164+
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3165+
/// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3166+
/// = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
3167+
/// = note: this struct contains `NonExhaustiveZst`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
3168+
/// ```
3169+
///
3170+
/// ### Explanation
3171+
///
3172+
/// Previous, Rust accepted fields that contain external private zero-sized types,
3173+
/// even though it should not be a breaking change to add a non-zero-sized field to
3174+
/// that private type.
3175+
///
3176+
/// This is a [future-incompatible] lint to transition this
3177+
/// to a hard error in the future. See [issue #78586] for more details.
3178+
///
3179+
/// [issue #78586]: https://github.com/rust-lang/rust/issues/78586
3180+
/// [future-incompatible]: ../index.md#future-incompatible-lints
3181+
pub REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
3182+
Warn,
3183+
"tranparent type contains an external ZST that is marked #[non_exhaustive] or contains private fields",
3184+
@future_incompatible = FutureIncompatibleInfo {
3185+
reference: "issue #78586 <https://github.com/rust-lang/rust/issues/78586>",
3186+
};
3187+
}
3188+
31353189
declare_lint_pass! {
31363190
/// Does nothing as a lint pass, but registers some `Lint`s
31373191
/// that are used by other parts of the compiler.
@@ -3237,6 +3291,7 @@ declare_lint_pass! {
32373291
DEPRECATED_WHERE_CLAUSE_LOCATION,
32383292
TEST_UNSTABLE_LINT,
32393293
FFI_UNWIND_CALLS,
3294+
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
32403295
]
32413296
}
32423297

compiler/rustc_typeck/src/check/check.rs

+66-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1717
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1818
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1919
use rustc_infer::traits::Obligation;
20+
use rustc_lint::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
2021
use rustc_middle::hir::nested_filter;
2122
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
2223
use rustc_middle::ty::subst::GenericArgKind;
@@ -1318,7 +1319,8 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13181319
}
13191320
}
13201321

1321-
// For each field, figure out if it's known to be a ZST and align(1)
1322+
// For each field, figure out if it's known to be a ZST and align(1), with "known"
1323+
// respecting #[non_exhaustive] attributes.
13221324
let field_infos = adt.all_fields().map(|field| {
13231325
let ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, field.did));
13241326
let param_env = tcx.param_env(field.did);
@@ -1327,16 +1329,56 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13271329
let span = tcx.hir().span_if_local(field.did).unwrap();
13281330
let zst = layout.map_or(false, |layout| layout.is_zst());
13291331
let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
1330-
(span, zst, align1)
1332+
if !zst {
1333+
return (span, zst, align1, None);
1334+
}
1335+
1336+
fn check_non_exhaustive<'tcx>(
1337+
tcx: TyCtxt<'tcx>,
1338+
t: Ty<'tcx>,
1339+
) -> ControlFlow<(&'static str, DefId, SubstsRef<'tcx>, bool)> {
1340+
match t.kind() {
1341+
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
1342+
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
1343+
ty::Adt(def, subst) => {
1344+
if !def.did().is_local() {
1345+
let non_exhaustive = def.is_variant_list_non_exhaustive()
1346+
|| def
1347+
.variants()
1348+
.iter()
1349+
.any(ty::VariantDef::is_field_list_non_exhaustive);
1350+
let has_priv = def.all_fields().any(|f| !f.vis.is_public());
1351+
if non_exhaustive || has_priv {
1352+
return ControlFlow::Break((
1353+
def.descr(),
1354+
def.did(),
1355+
subst,
1356+
non_exhaustive,
1357+
));
1358+
}
1359+
}
1360+
def.all_fields()
1361+
.map(|field| field.ty(tcx, subst))
1362+
.try_for_each(|t| check_non_exhaustive(tcx, t))
1363+
}
1364+
_ => ControlFlow::Continue(()),
1365+
}
1366+
}
1367+
1368+
(span, zst, align1, check_non_exhaustive(tcx, ty).break_value())
13311369
});
13321370

1333-
let non_zst_fields =
1334-
field_infos.clone().filter_map(|(span, zst, _align1)| if !zst { Some(span) } else { None });
1371+
let non_zst_fields = field_infos
1372+
.clone()
1373+
.filter_map(|(span, zst, _align1, _non_exhaustive)| if !zst { Some(span) } else { None });
13351374
let non_zst_count = non_zst_fields.clone().count();
13361375
if non_zst_count >= 2 {
13371376
bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, sp);
13381377
}
1339-
for (span, zst, align1) in field_infos {
1378+
let incompatible_zst_fields =
1379+
field_infos.clone().filter(|(_, _, _, opt)| opt.is_some()).count();
1380+
let incompat = incompatible_zst_fields + non_zst_count >= 2 && non_zst_count < 2;
1381+
for (span, zst, align1, non_exhaustive) in field_infos {
13401382
if zst && !align1 {
13411383
struct_span_err!(
13421384
tcx.sess,
@@ -1348,6 +1390,25 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13481390
.span_label(span, "has alignment larger than 1")
13491391
.emit();
13501392
}
1393+
if incompat && let Some((descr, def_id, substs, non_exhaustive)) = non_exhaustive {
1394+
tcx.struct_span_lint_hir(
1395+
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
1396+
tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()),
1397+
span,
1398+
|lint| {
1399+
let note = if non_exhaustive {
1400+
"is marked with `#[non_exhaustive]`"
1401+
} else {
1402+
"contains private fields"
1403+
};
1404+
let field_ty = tcx.def_path_str_with_substs(def_id, substs);
1405+
lint.build("zero-sized fields in repr(transparent) cannot contain external non-exhaustive types")
1406+
.note(format!("this {descr} contains `{field_ty}`, which {note}, \
1407+
and makes it not a breaking change to become non-zero-sized in the future."))
1408+
.emit();
1409+
},
1410+
)
1411+
}
13511412
}
13521413
}
13531414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_type = "lib"]
2+
3+
pub struct Private { _priv: () }
4+
5+
#[non_exhaustive]
6+
pub struct NonExhaustive {}
7+
8+
#[non_exhaustive]
9+
pub enum NonExhaustiveEnum {}
10+
11+
pub enum NonExhaustiveVariant {
12+
#[non_exhaustive]
13+
A,
14+
}
15+
16+
pub struct ExternalIndirection<T> {
17+
pub x: T,
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#![deny(repr_transparent_external_private_fields)]
2+
3+
// aux-build: repr-transparent-non-exhaustive.rs
4+
extern crate repr_transparent_non_exhaustive;
5+
6+
use repr_transparent_non_exhaustive::{
7+
Private,
8+
NonExhaustive,
9+
NonExhaustiveEnum,
10+
NonExhaustiveVariant,
11+
ExternalIndirection,
12+
};
13+
14+
pub struct InternalPrivate {
15+
_priv: (),
16+
}
17+
18+
#[non_exhaustive]
19+
pub struct InternalNonExhaustive;
20+
21+
pub struct InternalIndirection<T> {
22+
x: T,
23+
}
24+
25+
pub type Sized = i32;
26+
27+
#[repr(transparent)]
28+
pub struct T1(Sized, InternalPrivate);
29+
#[repr(transparent)]
30+
pub struct T2(Sized, InternalNonExhaustive);
31+
#[repr(transparent)]
32+
pub struct T3(Sized, InternalIndirection<(InternalPrivate, InternalNonExhaustive)>);
33+
#[repr(transparent)]
34+
pub struct T4(Sized, ExternalIndirection<(InternalPrivate, InternalNonExhaustive)>);
35+
36+
#[repr(transparent)]
37+
pub struct T5(Sized, Private);
38+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
39+
//~| WARN this was previously accepted by the compiler
40+
41+
#[repr(transparent)]
42+
pub struct T6(Sized, NonExhaustive);
43+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
44+
//~| WARN this was previously accepted by the compiler
45+
46+
#[repr(transparent)]
47+
pub struct T7(Sized, NonExhaustiveEnum);
48+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
49+
//~| WARN this was previously accepted by the compiler
50+
51+
#[repr(transparent)]
52+
pub struct T8(Sized, NonExhaustiveVariant);
53+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
54+
//~| WARN this was previously accepted by the compiler
55+
56+
#[repr(transparent)]
57+
pub struct T9(Sized, InternalIndirection<Private>);
58+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
59+
//~| WARN this was previously accepted by the compiler
60+
61+
#[repr(transparent)]
62+
pub struct T10(Sized, InternalIndirection<NonExhaustive>);
63+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
64+
//~| WARN this was previously accepted by the compiler
65+
66+
#[repr(transparent)]
67+
pub struct T11(Sized, InternalIndirection<NonExhaustiveEnum>);
68+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
69+
//~| WARN this was previously accepted by the compiler
70+
71+
#[repr(transparent)]
72+
pub struct T12(Sized, InternalIndirection<NonExhaustiveVariant>);
73+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
74+
//~| WARN this was previously accepted by the compiler
75+
76+
#[repr(transparent)]
77+
pub struct T13(Sized, ExternalIndirection<Private>);
78+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
79+
//~| WARN this was previously accepted by the compiler
80+
81+
#[repr(transparent)]
82+
pub struct T14(Sized, ExternalIndirection<NonExhaustive>);
83+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
84+
//~| WARN this was previously accepted by the compiler
85+
86+
#[repr(transparent)]
87+
pub struct T15(Sized, ExternalIndirection<NonExhaustiveEnum>);
88+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
89+
//~| WARN this was previously accepted by the compiler
90+
91+
#[repr(transparent)]
92+
pub struct T16(Sized, ExternalIndirection<NonExhaustiveVariant>);
93+
//~^ ERROR zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
94+
//~| WARN this was previously accepted by the compiler
95+
96+
fn main() {}

0 commit comments

Comments
 (0)