Skip to content

Commit b216fa9

Browse files
oli-obkpietroalbini
authored andcommitted
Do not promote union field accesses
1 parent 3ecb3a7 commit b216fa9

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/librustc_mir/transform/qualify_consts.rs

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

557557
ProjectionElem::Field(..) |
558558
ProjectionElem::Index(_) => {
559-
if this.mode != Mode::Fn &&
560-
this.qualif.intersects(Qualif::STATIC) {
559+
if this.mode == Mode::Fn {
560+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
561+
if let Some(def) = base_ty.ty_adt_def() {
562+
if def.is_union() {
563+
this.not_const();
564+
}
565+
}
566+
} else if this.qualif.intersects(Qualif::STATIC) {
561567
span_err!(this.tcx.sess, this.span, E0494,
562568
"cannot refer to the interior of another \
563569
static, use a constant instead");

src/librustc_passes/rvalue_promotion.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,16 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
424424
}
425425
}
426426

427-
hir::ExprBlock(_) |
427+
hir::ExprField(ref expr, _) => {
428+
if let Some(def) = v.tables.expr_ty(expr).ty_adt_def() {
429+
if def.is_union() {
430+
v.promotable = false
431+
}
432+
}
433+
}
434+
435+
hir::ExprBlock(..) |
428436
hir::ExprIndex(..) |
429-
hir::ExprField(..) |
430437
hir::ExprArray(_) |
431438
hir::ExprType(..) |
432439
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)