diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 6532cde9715..a525b4e13b7 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -247,9 +247,12 @@ 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); - self.mark_as_used_if_union(def.def_id(), fields); + hir::ExprStruct(_, ref fields, _) => { + if let ty::TypeVariants::TyAdt(ref def, _) = self.tables.expr_ty(expr).sty { + if def.is_union() { + self.mark_as_used_if_union(def.did, fields); + } + } } _ => () } diff --git a/src/test/ui/union-fields.rs b/src/test/ui/union-fields.rs index 7b39a548fe9..021f57e3eee 100644 --- a/src/test/ui/union-fields.rs +++ b/src/test/ui/union-fields.rs @@ -22,6 +22,13 @@ union U2 { } union NoDropLike { a: u8 } // should be reported as unused +union U { + a: u8, // should not be reported + b: u8, // should not be reported + c: u8, // should be reported +} +type A = U; + fn main() { let u = U1 { a: 0 }; let _a = unsafe { u.b }; @@ -30,4 +37,6 @@ fn main() { let _b = unsafe { u.b }; let _u = NoDropLike { a: 10 }; + let u = A { a: 0 }; + let _b = unsafe { u.b }; } diff --git a/src/test/ui/union-fields.stderr b/src/test/ui/union-fields.stderr index 5c47ba388a4..f3a2702d5ae 100644 --- a/src/test/ui/union-fields.stderr +++ b/src/test/ui/union-fields.stderr @@ -22,5 +22,11 @@ error: field is never used: `a` 23 | union NoDropLike { a: u8 } // should be reported as unused | ^^^^^ -error: aborting due to 3 previous errors +error: field is never used: `c` + --> $DIR/union-fields.rs:28:5 + | +28 | c: u8, // should be reported + | ^^^^^ + +error: aborting due to 4 previous errors