Skip to content

Commit ba1d065

Browse files
committed
Auto merge of #43397 - GuillaumeGomez:unused-union-field, r=petrochenkov
Don't warn on unused field on union Fixes #43393.
2 parents be69520 + f94157e commit ba1d065

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

src/librustc/middle/dead.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// from live codes are live, and everything else is dead.
1414

1515
use hir::map as hir_map;
16-
use hir::{self, PatKind};
16+
use hir::{self, Item_, PatKind};
1717
use hir::intravisit::{self, Visitor, NestedVisitorMap};
1818
use hir::itemlikevisit::ItemLikeVisitor;
1919

@@ -189,6 +189,22 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
189189
self.struct_has_extern_repr = had_extern_repr;
190190
self.inherited_pub_visibility = had_inherited_pub_visibility;
191191
}
192+
193+
fn mark_as_used_if_union(&mut self, did: DefId, fields: &hir::HirVec<hir::Field>) {
194+
if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
195+
if let Some(hir_map::NodeItem(item)) = self.tcx.hir.find(node_id) {
196+
if let Item_::ItemUnion(ref variant, _) = item.node {
197+
if variant.fields().len() > 1 {
198+
for field in variant.fields() {
199+
if fields.iter().find(|x| x.name.node == field.name).is_some() {
200+
self.live_symbols.insert(field.id);
201+
}
202+
}
203+
}
204+
}
205+
}
206+
}
207+
}
192208
}
193209

194210
impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
@@ -231,6 +247,13 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
231247
hir::ExprTupField(ref lhs, idx) => {
232248
self.handle_tup_field_access(&lhs, idx.node);
233249
}
250+
hir::ExprStruct(_, ref fields, _) => {
251+
if let ty::TypeVariants::TyAdt(ref def, _) = self.tables.expr_ty(expr).sty {
252+
if def.is_union() {
253+
self.mark_as_used_if_union(def.did, fields);
254+
}
255+
}
256+
}
234257
_ => ()
235258
}
236259

@@ -561,7 +584,6 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
561584
self.warn_dead_code(field.id, field.span,
562585
field.name, "field");
563586
}
564-
565587
intravisit::walk_struct_field(self, field);
566588
}
567589

@@ -603,6 +625,9 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
603625
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
604626
let krate = tcx.hir.krate();
605627
let live_symbols = find_live(tcx, access_levels, krate);
606-
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
628+
let mut visitor = DeadVisitor {
629+
tcx: tcx,
630+
live_symbols: live_symbols,
631+
};
607632
intravisit::walk_crate(&mut visitor, krate);
608633
}

src/test/ui/union-fields.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(dead_code)]
12+
13+
union U1 {
14+
a: u8, // should not be reported
15+
b: u8, // should not be reported
16+
c: u8, // should be reported
17+
}
18+
union U2 {
19+
a: u8, // should be reported
20+
b: u8, // should not be reported
21+
c: u8, // should not be reported
22+
}
23+
union NoDropLike { a: u8 } // should be reported as unused
24+
25+
union U {
26+
a: u8, // should not be reported
27+
b: u8, // should not be reported
28+
c: u8, // should be reported
29+
}
30+
type A = U;
31+
32+
fn main() {
33+
let u = U1 { a: 0 };
34+
let _a = unsafe { u.b };
35+
36+
let u = U2 { c: 0 };
37+
let _b = unsafe { u.b };
38+
39+
let _u = NoDropLike { a: 10 };
40+
let u = A { a: 0 };
41+
let _b = unsafe { u.b };
42+
}

src/test/ui/union-fields.stderr

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: field is never used: `c`
2+
--> $DIR/union-fields.rs:16:5
3+
|
4+
16 | c: u8, // should be reported
5+
| ^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/union-fields.rs:11:9
9+
|
10+
11 | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: field is never used: `a`
14+
--> $DIR/union-fields.rs:19:5
15+
|
16+
19 | a: u8, // should be reported
17+
| ^^^^^
18+
19+
error: field is never used: `a`
20+
--> $DIR/union-fields.rs:23:20
21+
|
22+
23 | union NoDropLike { a: u8 } // should be reported as unused
23+
| ^^^^^
24+
25+
error: field is never used: `c`
26+
--> $DIR/union-fields.rs:28:5
27+
|
28+
28 | c: u8, // should be reported
29+
| ^^^^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)