Skip to content

Commit 746c93f

Browse files
authored
Unrolled build for rust-lang#120107
Rollup merge of rust-lang#120107 - shepmaster:dead-code-repr-transparent, r=Nilstrieb dead_code treats #[repr(transparent)] the same as #[repr(C)] In rust-lang#92972 we enabled linting on unused fields in tuple structs. In rust-lang#118297 that lint was enabled by default. That exposed issues like rust-lang#119659, where the fields of a struct marked `#[repr(transparent)]` were reported by the `dead_code` lint. The language team [decided](rust-lang#119659 (comment)) that the lint should treat `repr(transparent)` the same as `#[repr(C)]`. Fixes rust-lang#119659
2 parents 92d7277 + aeeaed9 commit 746c93f

File tree

8 files changed

+29
-14
lines changed

8 files changed

+29
-14
lines changed

compiler/rustc_passes/src/dead.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct MarkSymbolVisitor<'tcx> {
5757
tcx: TyCtxt<'tcx>,
5858
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
5959
live_symbols: LocalDefIdSet,
60-
repr_has_repr_c: bool,
60+
repr_unconditionally_treats_fields_as_live: bool,
6161
repr_has_repr_simd: bool,
6262
in_pat: bool,
6363
ignore_variant_stack: Vec<DefId>,
@@ -365,15 +365,17 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
365365
return;
366366
}
367367

368-
let had_repr_c = self.repr_has_repr_c;
368+
let unconditionally_treated_fields_as_live =
369+
self.repr_unconditionally_treats_fields_as_live;
369370
let had_repr_simd = self.repr_has_repr_simd;
370-
self.repr_has_repr_c = false;
371+
self.repr_unconditionally_treats_fields_as_live = false;
371372
self.repr_has_repr_simd = false;
372373
match node {
373374
Node::Item(item) => match item.kind {
374375
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
375376
let def = self.tcx.adt_def(item.owner_id);
376-
self.repr_has_repr_c = def.repr().c();
377+
self.repr_unconditionally_treats_fields_as_live =
378+
def.repr().c() || def.repr().transparent();
377379
self.repr_has_repr_simd = def.repr().simd();
378380

379381
intravisit::walk_item(self, item)
@@ -411,7 +413,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
411413
_ => {}
412414
}
413415
self.repr_has_repr_simd = had_repr_simd;
414-
self.repr_has_repr_c = had_repr_c;
416+
self.repr_unconditionally_treats_fields_as_live = unconditionally_treated_fields_as_live;
415417
}
416418

417419
fn mark_as_used_if_union(&mut self, adt: ty::AdtDef<'tcx>, fields: &[hir::ExprField<'_>]) {
@@ -435,11 +437,11 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
435437

436438
fn visit_variant_data(&mut self, def: &'tcx hir::VariantData<'tcx>) {
437439
let tcx = self.tcx;
438-
let has_repr_c = self.repr_has_repr_c;
440+
let unconditionally_treat_fields_as_live = self.repr_unconditionally_treats_fields_as_live;
439441
let has_repr_simd = self.repr_has_repr_simd;
440442
let live_fields = def.fields().iter().filter_map(|f| {
441443
let def_id = f.def_id;
442-
if has_repr_c || (f.is_positional() && has_repr_simd) {
444+
if unconditionally_treat_fields_as_live || (f.is_positional() && has_repr_simd) {
443445
return Some(def_id);
444446
}
445447
if !tcx.visibility(f.hir_id.owner.def_id).is_public() {
@@ -741,7 +743,7 @@ fn live_symbols_and_ignored_derived_traits(
741743
tcx,
742744
maybe_typeck_results: None,
743745
live_symbols: Default::default(),
744-
repr_has_repr_c: false,
746+
repr_unconditionally_treats_fields_as_live: false,
745747
repr_has_repr_simd: false,
746748
in_pat: false,
747749
ignore_variant_stack: vec![],

library/alloc/src/boxed/thin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ struct WithHeader<H>(NonNull<u8>, PhantomData<H>);
171171
/// An opaque representation of `WithHeader<H>` to avoid the
172172
/// projection invariance of `<T as Pointee>::Metadata`.
173173
#[repr(transparent)]
174-
#[allow(dead_code)] // Field only used through `WithHeader` type above.
175174
struct WithOpaqueHeader(NonNull<u8>);
176175

177176
impl WithOpaqueHeader {

src/tools/miri/tests/fail/issue-miri-1112.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
trait Empty {}
22

33
#[repr(transparent)]
4-
pub struct FunnyPointer(#[allow(dead_code)] dyn Empty);
4+
pub struct FunnyPointer(dyn Empty);
55

66
#[repr(C)]
77
pub struct Meta {

src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Cdebug-assertions=no
22

33
#[repr(transparent)]
4-
struct HasDrop(#[allow(dead_code)] u8);
4+
struct HasDrop(u8);
55

66
impl Drop for HasDrop {
77
fn drop(&mut self) {}

tests/ui/consts/transmute-const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::mem;
44

55
#[repr(transparent)]
6-
struct Foo(#[allow(dead_code)] u32);
6+
struct Foo(u32);
77

88
const TRANSMUTED_U32: u32 = unsafe { mem::transmute(Foo(3)) };
99

tests/ui/layout/unsafe-cell-hides-niche.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::sync::{Mutex, RwLock};
1717
struct Wrapper<T>(#[allow(dead_code)] T);
1818

1919
#[repr(transparent)]
20-
struct Transparent<T>(#[allow(dead_code)] T);
20+
struct Transparent<T>(T);
2121

2222
struct NoNiche<T>(UnsafeCell<T>);
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Verify that we do not warn on fields that are part of transparent types.
2+
// check-pass
3+
#![deny(dead_code)]
4+
5+
#[repr(transparent)]
6+
struct NamedStruct { field: u8 }
7+
8+
#[repr(transparent)]
9+
struct TupleStruct(u8);
10+
11+
fn main() {
12+
let _ = NamedStruct { field: 1 };
13+
let _ = TupleStruct(1);
14+
}

tests/ui/packed/packed-struct-drop-aligned.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'a> Drop for Aligned<'a> {
2424
}
2525

2626
#[repr(transparent)]
27-
struct NotCopy(#[allow(dead_code)] u8);
27+
struct NotCopy(u8);
2828

2929
#[repr(packed)]
3030
struct Packed<'a>(NotCopy, Aligned<'a>);

0 commit comments

Comments
 (0)