Skip to content

Commit 4eea1a4

Browse files
committed
Don't const propagate the body of constants
1 parent 4bb9648 commit 4eea1a4

18 files changed

+94
-179
lines changed

src/librustc_mir/transform/const_prop.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ impl MirPass for ConstProp {
4545
return;
4646
}
4747
match tcx.describe_def(source.def_id) {
48-
// skip statics because they'll be evaluated by miri anyway
48+
// skip statics/consts because they'll be evaluated by miri anyway
49+
Some(Def::Const(..)) |
4950
Some(Def::Static(..)) => return,
51+
// we still run on associated constants, because they might not get evaluated
52+
// within the current crate
5053
_ => {},
5154
}
5255
trace!("ConstProp starting for {:?}", source.def_id);

src/test/compile-fail/const-err-early.rs

-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111
#![deny(const_err)]
1212

1313
pub const A: i8 = -std::i8::MIN; //~ ERROR const_err
14-
//~^ ERROR this constant cannot be used
15-
//~| ERROR this expression will panic at runtime
1614
pub const B: u8 = 200u8 + 200u8; //~ ERROR const_err
17-
//~^ ERROR this constant cannot be used
1815
pub const C: u8 = 200u8 * 4; //~ ERROR const_err
19-
//~^ ERROR this constant cannot be used
2016
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
21-
//~^ ERROR this constant cannot be used
2217
pub const E: u8 = [5u8][1]; //~ ERROR const_err
23-
//~| ERROR this constant cannot be used
2418

2519
fn main() {
2620
let _a = A;

src/test/compile-fail/const-err-multi.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,16 @@
1111
#![deny(const_err)]
1212

1313
pub const A: i8 = -std::i8::MIN;
14-
//~^ ERROR attempt to negate with overflow
15-
//~| ERROR this expression will panic at runtime
16-
//~| ERROR this constant cannot be used
14+
//~^ ERROR this constant cannot be used
1715
pub const B: i8 = A;
1816
//~^ ERROR const_err
1917
//~| ERROR const_err
20-
//~| ERROR const_err
21-
//~| ERROR const_err
2218
pub const C: u8 = A as u8;
2319
//~^ ERROR const_err
2420
//~| ERROR const_err
25-
//~| ERROR const_err
26-
//~| ERROR const_err
2721
pub const D: i8 = 50 - A;
2822
//~^ ERROR const_err
2923
//~| ERROR const_err
30-
//~| ERROR const_err
31-
//~| ERROR const_err
3224

3325
fn main() {
3426
let _ = (A, B, C, D);

src/test/compile-fail/const-eval-overflow2.rs

-8
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,46 @@ const VALS_I8: (i8,) =
2525
//~^ ERROR this constant cannot be used
2626
(
2727
i8::MIN - 1,
28-
//~^ ERROR attempt to subtract with overflow
2928
);
3029

3130
const VALS_I16: (i16,) =
3231
//~^ ERROR this constant cannot be used
3332
(
3433
i16::MIN - 1,
35-
//~^ ERROR attempt to subtract with overflow
3634
);
3735

3836
const VALS_I32: (i32,) =
3937
//~^ ERROR this constant cannot be used
4038
(
4139
i32::MIN - 1,
42-
//~^ ERROR attempt to subtract with overflow
4340
);
4441

4542
const VALS_I64: (i64,) =
4643
//~^ ERROR this constant cannot be used
4744
(
4845
i64::MIN - 1,
49-
//~^ ERROR attempt to subtract with overflow
5046
);
5147

5248
const VALS_U8: (u8,) =
5349
//~^ ERROR this constant cannot be used
5450
(
5551
u8::MIN - 1,
56-
//~^ ERROR attempt to subtract with overflow
5752
);
5853

5954
const VALS_U16: (u16,) = (
6055
//~^ ERROR this constant cannot be used
6156
u16::MIN - 1,
62-
//~^ ERROR attempt to subtract with overflow
6357
);
6458

6559
const VALS_U32: (u32,) = (
6660
//~^ ERROR this constant cannot be used
6761
u32::MIN - 1,
68-
//~^ ERROR attempt to subtract with overflow
6962
);
7063

7164
const VALS_U64: (u64,) =
7265
//~^ ERROR this constant cannot be used
7366
(
7467
u64::MIN - 1,
75-
//~^ ERROR attempt to subtract with overflow
7668
);
7769

7870
fn main() {

src/test/compile-fail/const-eval-overflow2b.rs

-8
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,46 @@ const VALS_I8: (i8,) =
2525
//~^ ERROR this constant cannot be used
2626
(
2727
i8::MAX + 1,
28-
//~^ ERROR attempt to add with overflow
2928
);
3029

3130
const VALS_I16: (i16,) =
3231
//~^ ERROR this constant cannot be used
3332
(
3433
i16::MAX + 1,
35-
//~^ ERROR attempt to add with overflow
3634
);
3735

3836
const VALS_I32: (i32,) =
3937
//~^ ERROR this constant cannot be used
4038
(
4139
i32::MAX + 1,
42-
//~^ ERROR attempt to add with overflow
4340
);
4441

4542
const VALS_I64: (i64,) =
4643
//~^ ERROR this constant cannot be used
4744
(
4845
i64::MAX + 1,
49-
//~^ ERROR attempt to add with overflow
5046
);
5147

5248
const VALS_U8: (u8,) =
5349
//~^ ERROR this constant cannot be used
5450
(
5551
u8::MAX + 1,
56-
//~^ ERROR attempt to add with overflow
5752
);
5853

5954
const VALS_U16: (u16,) = (
6055
//~^ ERROR this constant cannot be used
6156
u16::MAX + 1,
62-
//~^ ERROR attempt to add with overflow
6357
);
6458

6559
const VALS_U32: (u32,) = (
6660
//~^ ERROR this constant cannot be used
6761
u32::MAX + 1,
68-
//~^ ERROR attempt to add with overflow
6962
);
7063

7164
const VALS_U64: (u64,) =
7265
//~^ ERROR this constant cannot be used
7366
(
7467
u64::MAX + 1,
75-
//~^ ERROR attempt to add with overflow
7668
);
7769

7870
fn main() {

src/test/compile-fail/const-eval-overflow2c.rs

-8
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,46 @@ const VALS_I8: (i8,) =
2525
//~^ ERROR this constant cannot be used
2626
(
2727
i8::MIN * 2,
28-
//~^ ERROR attempt to multiply with overflow
2928
);
3029

3130
const VALS_I16: (i16,) =
3231
//~^ ERROR this constant cannot be used
3332
(
3433
i16::MIN * 2,
35-
//~^ ERROR attempt to multiply with overflow
3634
);
3735

3836
const VALS_I32: (i32,) =
3937
//~^ ERROR this constant cannot be used
4038
(
4139
i32::MIN * 2,
42-
//~^ ERROR attempt to multiply with overflow
4340
);
4441

4542
const VALS_I64: (i64,) =
4643
//~^ ERROR this constant cannot be used
4744
(
4845
i64::MIN * 2,
49-
//~^ ERROR attempt to multiply with overflow
5046
);
5147

5248
const VALS_U8: (u8,) =
5349
//~^ ERROR this constant cannot be used
5450
(
5551
u8::MAX * 2,
56-
//~^ ERROR attempt to multiply with overflow
5752
);
5853

5954
const VALS_U16: (u16,) = (
6055
//~^ ERROR this constant cannot be used
6156
u16::MAX * 2,
62-
//~^ ERROR attempt to multiply with overflow
6357
);
6458

6559
const VALS_U32: (u32,) = (
6660
//~^ ERROR this constant cannot be used
6761
u32::MAX * 2,
68-
//~^ ERROR attempt to multiply with overflow
6962
);
7063

7164
const VALS_U64: (u64,) =
7265
//~^ ERROR this constant cannot be used
7366
(
7467
u64::MAX * 2,
75-
//~^ ERROR attempt to multiply with overflow
7668
);
7769

7870
fn main() {

src/test/ui/const-eval/conditional_array_execution.nll.stderr

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
warning: attempt to subtract with overflow
2-
--> $DIR/conditional_array_execution.rs:15:19
1+
warning: this constant cannot be used
2+
--> $DIR/conditional_array_execution.rs:15:1
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5-
| ^^^^^
5+
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| attempt to subtract with overflow
68
|
79
note: lint level defined here
810
--> $DIR/conditional_array_execution.rs:11:9
911
|
1012
LL | #![warn(const_err)]
1113
| ^^^^^^^^^
1214

13-
warning: this constant cannot be used
14-
--> $DIR/conditional_array_execution.rs:15:1
15-
|
16-
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
17-
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
18-
| |
19-
| attempt to subtract with overflow
20-
2115
warning: referenced constant
22-
--> $DIR/conditional_array_execution.rs:20:20
16+
--> $DIR/conditional_array_execution.rs:19:20
2317
|
2418
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
2519
| ----- attempt to subtract with overflow
@@ -28,13 +22,13 @@ LL | println!("{}", FOO);
2822
| ^^^
2923

3024
warning: this expression will panic at runtime
31-
--> $DIR/conditional_array_execution.rs:20:20
25+
--> $DIR/conditional_array_execution.rs:19:20
3226
|
3327
LL | println!("{}", FOO);
3428
| ^^^ referenced constant has errors
3529

3630
error[E0080]: referenced constant
37-
--> $DIR/conditional_array_execution.rs:20:5
31+
--> $DIR/conditional_array_execution.rs:19:5
3832
|
3933
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
4034
| ----- attempt to subtract with overflow
@@ -45,7 +39,7 @@ LL | println!("{}", FOO);
4539
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
4640

4741
error[E0080]: erroneous constant used
48-
--> $DIR/conditional_array_execution.rs:20:5
42+
--> $DIR/conditional_array_execution.rs:19:5
4943
|
5044
LL | println!("{}", FOO);
5145
| ^^^^^^^^^^^^^^^---^^
@@ -55,7 +49,7 @@ LL | println!("{}", FOO);
5549
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
5650

5751
error[E0080]: referenced constant
58-
--> $DIR/conditional_array_execution.rs:20:20
52+
--> $DIR/conditional_array_execution.rs:19:20
5953
|
6054
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
6155
| ----- attempt to subtract with overflow
@@ -64,7 +58,7 @@ LL | println!("{}", FOO);
6458
| ^^^
6559

6660
error[E0080]: erroneous constant used
67-
--> $DIR/conditional_array_execution.rs:20:20
61+
--> $DIR/conditional_array_execution.rs:19:20
6862
|
6963
LL | println!("{}", FOO);
7064
| ^^^ referenced constant has errors

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
const X: u32 = 5;
1414
const Y: u32 = 6;
1515
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
16-
//~^ WARN attempt to subtract with overflow
17-
//~| WARN this constant cannot be used
16+
//~^ WARN this constant cannot be used
1817

1918
fn main() {
2019
println!("{}", FOO);

src/test/ui/const-eval/conditional_array_execution.stderr

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
warning: attempt to subtract with overflow
2-
--> $DIR/conditional_array_execution.rs:15:19
1+
warning: this constant cannot be used
2+
--> $DIR/conditional_array_execution.rs:15:1
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5-
| ^^^^^
5+
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| attempt to subtract with overflow
68
|
79
note: lint level defined here
810
--> $DIR/conditional_array_execution.rs:11:9
911
|
1012
LL | #![warn(const_err)]
1113
| ^^^^^^^^^
1214

13-
warning: this constant cannot be used
14-
--> $DIR/conditional_array_execution.rs:15:1
15-
|
16-
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
17-
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
18-
| |
19-
| attempt to subtract with overflow
20-
2115
warning: referenced constant
22-
--> $DIR/conditional_array_execution.rs:20:20
16+
--> $DIR/conditional_array_execution.rs:19:20
2317
|
2418
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
2519
| ----- attempt to subtract with overflow
@@ -28,13 +22,13 @@ LL | println!("{}", FOO);
2822
| ^^^
2923

3024
warning: this expression will panic at runtime
31-
--> $DIR/conditional_array_execution.rs:20:20
25+
--> $DIR/conditional_array_execution.rs:19:20
3226
|
3327
LL | println!("{}", FOO);
3428
| ^^^ referenced constant has errors
3529

3630
error[E0080]: referenced constant
37-
--> $DIR/conditional_array_execution.rs:20:20
31+
--> $DIR/conditional_array_execution.rs:19:20
3832
|
3933
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
4034
| ----- attempt to subtract with overflow
@@ -43,7 +37,7 @@ LL | println!("{}", FOO);
4337
| ^^^
4438

4539
error[E0080]: erroneous constant used
46-
--> $DIR/conditional_array_execution.rs:20:20
40+
--> $DIR/conditional_array_execution.rs:19:20
4741
|
4842
LL | println!("{}", FOO);
4943
| ^^^ referenced constant has errors

0 commit comments

Comments
 (0)