Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// from live codes are live, and everything else is dead.

use hir::map as hir_map;
use hir::{self, PatKind};
use hir::{self, Item_, PatKind};
use hir::intravisit::{self, Visitor, NestedVisitorMap};
use hir::itemlikevisit::ItemLikeVisitor;

Expand Down Expand Up @@ -189,6 +189,22 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
self.struct_has_extern_repr = had_extern_repr;
self.inherited_pub_visibility = had_inherited_pub_visibility;
}

fn mark_as_used_if_union(&mut self, did: DefId, fields: &hir::HirVec<hir::Field>) {
if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
if let Some(hir_map::NodeItem(item)) = self.tcx.hir.find(node_id) {
if let Item_::ItemUnion(ref variant, _) = item.node {
if variant.fields().len() > 1 {
for field in variant.fields() {
if fields.iter().find(|x| x.name.node == field.name).is_some() {
self.live_symbols.insert(field.id);
}
}
}
}
}
}
}
}

impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
Expand Down Expand Up @@ -231,6 +247,10 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
hir::ExprTupField(ref lhs, idx) => {
self.handle_tup_field_access(&lhs, idx.node);
}
hir::ExprStruct(ref qpath, ref fields, _) => {
let def = self.tables.qpath_def(qpath, expr.id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last thing is to make it work for type aliases (it'll also fix the ICE with Self).

You can use self.tcx.expr_ty(expr) to get the struct expression's type and then check that it's a union.

Test:

union U {
    a: u8, // should not be reported
    b: u8, // should not be reported
    c: u8, // should be reported
}
type A = U;

let u = A { a: 0 };
let b = unsafe { u.b };

self.mark_as_used_if_union(def.def_id(), fields);
}
_ => ()
}

Expand Down Expand Up @@ -561,7 +581,6 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
self.warn_dead_code(field.id, field.span,
field.name, "field");
}

intravisit::walk_struct_field(self, field);
}

Expand Down Expand Up @@ -603,6 +622,9 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
let krate = tcx.hir.krate();
let live_symbols = find_live(tcx, access_levels, krate);
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
let mut visitor = DeadVisitor {
tcx: tcx,
live_symbols: live_symbols,
};
intravisit::walk_crate(&mut visitor, krate);
}
33 changes: 33 additions & 0 deletions src/test/ui/union-fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(dead_code)]

union U1 {
a: u8, // should not be reported
b: u8, // should not be reported
c: u8, // should be reported
}
union U2 {
a: u8, // should be reported
b: u8, // should not be reported
c: u8, // should not be reported
}
union NoDropLike { a: u8 } // should be reported as unused

fn main() {
let u = U1 { a: 0 };
let _a = unsafe { u.b };

let u = U2 { c: 0 };
let _b = unsafe { u.b };

let _u = NoDropLike { a: 10 };
}
26 changes: 26 additions & 0 deletions src/test/ui/union-fields.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: field is never used: `c`
--> $DIR/union-fields.rs:16:5
|
16 | c: u8, // should be reported
| ^^^^^
|
note: lint level defined here
--> $DIR/union-fields.rs:11:9
|
11 | #![deny(dead_code)]
| ^^^^^^^^^

error: field is never used: `a`
--> $DIR/union-fields.rs:19:5
|
19 | a: u8, // should be reported
| ^^^^^

error: field is never used: `a`
--> $DIR/union-fields.rs:23:20
|
23 | union NoDropLike { a: u8 } // should be reported as unused
| ^^^^^

error: aborting due to 3 previous errors