Skip to content

Commit cdc193d

Browse files
committed
Auto merge of #51328 - oli-obk:no_union_promotion, r=eddyb
Do not promote union field accesses r? @eddyb technically a breaking change, but the code errored on the previous stable and produces UB + a warning on the current stable. I don't think we need a crater run in that case.
2 parents 6eafab0 + eb31ae2 commit cdc193d

File tree

6 files changed

+65
-51
lines changed

6 files changed

+65
-51
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(..) => {}

src/test/run-pass/ctfe/union-ice.rs

-45
This file was deleted.

src/test/run-pass/union/union-const-eval-field.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![feature(const_fn)]
1212

13-
type Field1 = i32;
13+
type Field1 = (i32, u32);
1414
type Field2 = f32;
1515
type Field3 = i64;
1616

@@ -21,7 +21,7 @@ union DummyUnion {
2121
}
2222

2323
const FLOAT1_AS_I32: i32 = 1065353216;
24-
const UNION: DummyUnion = DummyUnion { field1: FLOAT1_AS_I32 };
24+
const UNION: DummyUnion = DummyUnion { field1: (FLOAT1_AS_I32, 0) };
2525

2626
const fn read_field1() -> Field1 {
2727
const FIELD1: Field1 = unsafe { UNION.field1 };
@@ -39,7 +39,15 @@ const fn read_field3() -> Field3 {
3939
}
4040

4141
fn main() {
42-
assert_eq!(read_field1(), FLOAT1_AS_I32);
42+
let foo = FLOAT1_AS_I32;
43+
assert_eq!(read_field1().0, foo);
44+
assert_eq!(read_field1().0, FLOAT1_AS_I32);
45+
46+
let foo = 1.0;
47+
assert_eq!(read_field2(), foo);
4348
assert_eq!(read_field2(), 1.0);
49+
4450
assert_eq!(read_field3(), unsafe { UNION.field3 });
51+
let foo = unsafe { UNION.field3 };
52+
assert_eq!(read_field3(), foo);
4553
}
+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)