Skip to content

Commit 9d613c2

Browse files
committed
Do not promote union field accesses
1 parent 3575be6 commit 9d613c2

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/librustc_mir/transform/qualify_consts.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,14 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
566566

567567
ProjectionElem::Field(..) |
568568
ProjectionElem::Index(_) => {
569-
if this.mode != Mode::Fn &&
570-
this.qualif.intersects(Qualif::STATIC) {
569+
if this.mode == Mode::Fn {
570+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
571+
if let Some(def) = base_ty.ty_adt_def() {
572+
if def.is_union() {
573+
this.not_const();
574+
}
575+
}
576+
} else if this.qualif.intersects(Qualif::STATIC) {
571577
span_err!(this.tcx.sess, this.span, E0494,
572578
"cannot refer to the interior of another \
573579
static, use a constant instead");

src/librustc_passes/rvalue_promotion.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,16 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
445445
}
446446
}
447447

448+
hir::ExprField(ref expr, _) => {
449+
if let Some(def) = v.tables.expr_ty(expr).ty_adt_def() {
450+
if def.is_union() {
451+
v.promotable = false
452+
}
453+
}
454+
}
455+
448456
hir::ExprBlock(..) |
449457
hir::ExprIndex(..) |
450-
hir::ExprField(..) |
451458
hir::ExprArray(_) |
452459
hir::ExprType(..) |
453460
hir::ExprTup(..) => {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 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+
#![allow(const_err)]
12+
13+
union Foo {
14+
a: &'static u32,
15+
b: usize,
16+
}
17+
18+
fn main() {
19+
let x: &'static bool = &unsafe { //~ borrowed value does not live long enough
20+
Foo { a: &1 }.b == Foo { a: &2 }.b
21+
};
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/union_promotion.rs:19:29
3+
|
4+
LL | let x: &'static bool = &unsafe { //~ borrowed value does not live long enough
5+
| _____________________________^
6+
LL | | Foo { a: &1 }.b == Foo { a: &2 }.b
7+
LL | | };
8+
| |_____^ temporary value does not live long enough
9+
LL | }
10+
| - temporary value only lives until here
11+
|
12+
= note: borrowed value must be valid for the static lifetime...
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)