Skip to content

Commit 27e710f

Browse files
committed
Add a test showing the erroneous promoted bug
1 parent d81651e commit 27e710f

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/librustc_codegen_llvm/mir/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
407407
.unwrap_or_else(|err| {
408408
match constant.literal {
409409
mir::Literal::Promoted { .. } => {
410-
// don't report errors inside promoteds, just warnings.
410+
// FIXME: generate a panic here
411411
},
412412
mir::Literal::Value { .. } => {
413-
err.report(bx.tcx(), constant.span, "const operand")
413+
err.report(bx.tcx(), constant.span, "const operand");
414414
},
415415
}
416416
// We've errored, so we don't have to produce working code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#![feature(const_fn)]
12+
13+
#![deny(const_err)]
14+
15+
union Bar {
16+
a: &'static u8,
17+
b: usize,
18+
}
19+
20+
const fn bar() -> u8 {
21+
unsafe {
22+
// this will error as long as this test
23+
// is run on a system whose pointers need more
24+
// than 8 bits
25+
Bar { a: &42 }.b as u8
26+
//~^ constant evaluation error
27+
//~| constant evaluation error
28+
}
29+
}
30+
31+
fn main() {
32+
// FIXME(oli-obk): this should compile but panic at runtime
33+
// if we change the `const_err` lint to allow this will actually compile, but then
34+
// continue with undefined values.
35+
let x: &'static u8 = &(bar() + 1);
36+
let y = *x;
37+
unreachable!();
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: constant evaluation error
2+
--> $DIR/promoted_const_fn_fail.rs:25:9
3+
|
4+
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:13:9
9+
|
10+
LL | #![deny(const_err)]
11+
| ^^^^^^^^^
12+
note: inside call to `bar`
13+
--> $DIR/promoted_const_fn_fail.rs:35:28
14+
|
15+
LL | let x: &'static u8 = &(bar() + 1);
16+
| ^^^^^
17+
18+
error: constant evaluation error
19+
--> $DIR/promoted_const_fn_fail.rs:25:9
20+
|
21+
LL | Bar { a: &42 }.b as u8
22+
| ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
23+
|
24+
note: inside call to `bar`
25+
--> $DIR/promoted_const_fn_fail.rs:35:28
26+
|
27+
LL | let x: &'static u8 = &(bar() + 1);
28+
| ^^^^^
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)