Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 70408a0

Browse files
authoredJul 12, 2022
Rollup merge of rust-lang#99020 - fee1-dead-contrib:repr_transparent_non_exhaustive, r=oli-obk
check non_exhaustive attr and private fields for transparent types Fixes rust-lang#78586.
2 parents 4ef94c9 + d812850 commit 70408a0

File tree

5 files changed

+361
-5
lines changed

5 files changed

+361
-5
lines changed
 

‎compiler/rustc_lint_defs/src/builtin.rs

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

‎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;
@@ -1347,7 +1348,8 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13471348
}
13481349
}
13491350

1350-
// For each field, figure out if it's known to be a ZST and align(1)
1351+
// For each field, figure out if it's known to be a ZST and align(1), with "known"
1352+
// respecting #[non_exhaustive] attributes.
13511353
let field_infos = adt.all_fields().map(|field| {
13521354
let ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, field.did));
13531355
let param_env = tcx.param_env(field.did);
@@ -1356,16 +1358,56 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13561358
let span = tcx.hir().span_if_local(field.did).unwrap();
13571359
let zst = layout.map_or(false, |layout| layout.is_zst());
13581360
let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
1359-
(span, zst, align1)
1361+
if !zst {
1362+
return (span, zst, align1, None);
1363+
}
1364+
1365+
fn check_non_exhaustive<'tcx>(
1366+
tcx: TyCtxt<'tcx>,
1367+
t: Ty<'tcx>,
1368+
) -> ControlFlow<(&'static str, DefId, SubstsRef<'tcx>, bool)> {
1369+
match t.kind() {
1370+
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
1371+
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
1372+
ty::Adt(def, subst) => {
1373+
if !def.did().is_local() {
1374+
let non_exhaustive = def.is_variant_list_non_exhaustive()
1375+
|| def
1376+
.variants()
1377+
.iter()
1378+
.any(ty::VariantDef::is_field_list_non_exhaustive);
1379+
let has_priv = def.all_fields().any(|f| !f.vis.is_public());
1380+
if non_exhaustive || has_priv {
1381+
return ControlFlow::Break((
1382+
def.descr(),
1383+
def.did(),
1384+
subst,
1385+
non_exhaustive,
1386+
));
1387+
}
1388+
}
1389+
def.all_fields()
1390+
.map(|field| field.ty(tcx, subst))
1391+
.try_for_each(|t| check_non_exhaustive(tcx, t))
1392+
}
1393+
_ => ControlFlow::Continue(()),
1394+
}
1395+
}
1396+
1397+
(span, zst, align1, check_non_exhaustive(tcx, ty).break_value())
13601398
});
13611399

1362-
let non_zst_fields =
1363-
field_infos.clone().filter_map(|(span, zst, _align1)| if !zst { Some(span) } else { None });
1400+
let non_zst_fields = field_infos
1401+
.clone()
1402+
.filter_map(|(span, zst, _align1, _non_exhaustive)| if !zst { Some(span) } else { None });
13641403
let non_zst_count = non_zst_fields.clone().count();
13651404
if non_zst_count >= 2 {
13661405
bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, sp);
13671406
}
1368-
for (span, zst, align1) in field_infos {
1407+
let incompatible_zst_fields =
1408+
field_infos.clone().filter(|(_, _, _, opt)| opt.is_some()).count();
1409+
let incompat = incompatible_zst_fields + non_zst_count >= 2 && non_zst_count < 2;
1410+
for (span, zst, align1, non_exhaustive) in field_infos {
13691411
if zst && !align1 {
13701412
struct_span_err!(
13711413
tcx.sess,
@@ -1377,6 +1419,25 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
13771419
.span_label(span, "has alignment larger than 1")
13781420
.emit();
13791421
}
1422+
if incompat && let Some((descr, def_id, substs, non_exhaustive)) = non_exhaustive {
1423+
tcx.struct_span_lint_hir(
1424+
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
1425+
tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()),
1426+
span,
1427+
|lint| {
1428+
let note = if non_exhaustive {
1429+
"is marked with `#[non_exhaustive]`"
1430+
} else {
1431+
"contains private fields"
1432+
};
1433+
let field_ty = tcx.def_path_str_with_substs(def_id, substs);
1434+
lint.build("zero-sized fields in repr(transparent) cannot contain external non-exhaustive types")
1435+
.note(format!("this {descr} contains `{field_ty}`, which {note}, \
1436+
and makes it not a breaking change to become non-zero-sized in the future."))
1437+
.emit();
1438+
},
1439+
)
1440+
}
13801441
}
13811442
}
13821443

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)