Skip to content

Commit 61b9407

Browse files
committed
Prevent #[promotable] const fn from doing anything interesting with unions
1 parent 1fa1fad commit 61b9407

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed

src/librustc_mir/transform/qualify_consts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
570570

571571
ProjectionElem::Field(..) |
572572
ProjectionElem::Index(_) => {
573-
if this.mode == Mode::Fn {
573+
if this.mode == Mode::Fn ||
574+
this.mode == (Mode::ConstFn { promotable: true }) {
574575
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
575576
if let Some(def) = base_ty.ty_adt_def() {
576577
if def.is_union() {

src/test/compile-fail/rustc-args-required-const.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(attr_literals, rustc_attrs, const_fn)]
11+
#![feature(attr_literals, rustc_attrs, const_fn, promotable_const_fn)]
1212

1313
#[rustc_args_required_const(0)]
1414
fn foo(_a: i32) {
@@ -20,6 +20,7 @@ fn bar(_a: i32, _b: i32) {
2020

2121
const A: i32 = 3;
2222

23+
#[promotable_const_fn]
2324
const fn baz() -> i32 {
2425
3
2526
}

src/test/ui/const-eval/promote-const-fn.rs

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ const fn foo() {}
1717
#[promotable_const_fn]
1818
const fn bar() {}
1919

20+
union Foo {
21+
a: &'static u32,
22+
b: usize,
23+
}
24+
25+
#[promotable_const_fn]
26+
const fn boo() -> bool {
27+
unsafe {
28+
Foo { a: &1 }.b == 42 //~ ERROR promotable constant function contains
29+
}
30+
}
31+
2032
fn main() {
2133
let x: &'static () = &foo(); //~ borrowed value does not live long enough
2234
let x: &'static () = &bar();
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
error[E0019]: promotable constant function contains unimplemented expression type
2+
--> $DIR/promote-const-fn.rs:28:9
3+
|
4+
LL | Foo { a: &1 }.b == 42 //~ ERROR promotable constant function contains
5+
| ^^^^^^^^^^^^^^^
6+
17
error[E0597]: borrowed value does not live long enough
2-
--> $DIR/promote-const-fn.rs:16:27
8+
--> $DIR/promote-const-fn.rs:33:27
39
|
410
LL | let x: &'static () = &foo(); //~ borrowed value does not live long enough
511
| ^^^^^ temporary value does not live long enough
12+
LL | let x: &'static () = &bar();
613
LL | }
714
| - temporary value only lives until here
815
|
916
= note: borrowed value must be valid for the static lifetime...
1017

11-
error: aborting due to previous error
18+
error: aborting due to 2 previous errors
1219

13-
For more information about this error, try `rustc --explain E0597`.
20+
Some errors occurred: E0019, E0597.
21+
For more information about an error, try `rustc --explain E0019`.

src/test/ui/const-eval/promoted_const_fn_fail.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ const fn bar() -> u8 {
3030
// is run on a system whose pointers need more
3131
// than 8 bits
3232
Bar { a: &42 }.b as u8
33-
//~^ ERROR this expression will panic at runtime
34-
//~| ERROR this expression will panic at runtime
33+
//~^ ERROR promotable constant function
3534
}
3635
}
3736

3837
fn main() {
39-
// FIXME(oli-obk): this should compile but panic at runtime
40-
// if we change the `const_err` lint to allow this will actually compile, but then
41-
// continue with undefined values.
4238
let x: &'static u8 = &(bar() + 1);
4339
let y = *x;
4440
unreachable!();
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
error: this expression will panic at runtime
1+
error[E0019]: promotable constant function contains unimplemented expression type
22
--> $DIR/promoted_const_fn_fail.rs:32:9
33
|
44
LL | Bar { a: &42 }.b as u8
5-
| ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
6-
|
7-
note: lint level defined here
8-
--> $DIR/promoted_const_fn_fail.rs:18:9
9-
|
10-
LL | #![deny(const_err)]
11-
| ^^^^^^^^^
12-
13-
error: this expression will panic at runtime
14-
--> $DIR/promoted_const_fn_fail.rs:32:9
15-
|
16-
LL | Bar { a: &42 }.b as u8
17-
| ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
5+
| ^^^^^^^^^^^^^^^^
186

19-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
208

9+
For more information about this error, try `rustc --explain E0019`.

0 commit comments

Comments
 (0)