Skip to content

Don't warn on unused field on union #43397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 28 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,24 @@ 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) {
if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
match self.tcx.hir.find(node_id) {
Some(hir_map::NodeItem(item)) => match item.node {
Copy link
Member Author

Choose a reason for hiding this comment

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

@petrochenkov: For now it still doesn't work and I think it's because of here: it never enter in here. Any idea what I did wrong?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, sorry, I meant hir::ExprStruct in #43397 (comment) of course, not hir::ExprPath.
mark_as_used_if_union is good except that it should add only fields used in ExprStruct to the live_symbols set, not all fields from the union.
Nit: mark_as_used_if_union could also use some if lets.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! That makes more sense!

Item_::ItemUnion(ref variant, _) => {
if variant.fields().len() > 1 {
for field in variant.fields() {
self.live_symbols.insert(field.id);
}
}
}
_ => {}
},
_ => {}
}
}
}
}

impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
Expand Down Expand Up @@ -221,6 +239,11 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
hir::ExprPath(ref qpath @ hir::QPath::TypeRelative(..)) => {
let def = self.tables.qpath_def(qpath, expr.id);
self.handle_definition(def);
self.mark_as_used_if_union(def.def_id());
}
hir::ExprPath(ref qpath @ hir::QPath::Resolved(..)) => {
let def = self.tables.qpath_def(qpath, expr.id);
self.mark_as_used_if_union(def.def_id());
}
hir::ExprMethodCall(..) => {
self.lookup_and_handle_method(expr.id);
Expand Down Expand Up @@ -561,7 +584,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 +625,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 };
}
14 changes: 14 additions & 0 deletions src/test/ui/union-fields.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: field is never used: `y`
--> $DIR/union-fields.rs:20:5
|
20 | y: u32,
| ^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

Diagnostics for unions are not actually tested here.
Could you remove the struct V (it's not especially relevant to the test) and test this instead:

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
}

let u = U1 { a: 0 };
let a = u.b;

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

|
note: lint level defined here
--> $DIR/union-fields.rs:11:9
|
11 | #![deny(dead_code)]
| ^^^^^^^^^

error: aborting due to previous error