Skip to content

Commit 63b8f01

Browse files
committed
Auto merge of #94934 - Lireer:const-prop-lint, r=oli-obk
Separate const prop lints from optimizations r? `@oli-obk` Separates lints and optimizations during const prop by moving the lints into their own file and checking them during post borrowck cleanup. Thanks to `@oli-obk` for mentoring me.
2 parents 4b133a7 + 440946a commit 63b8f01

30 files changed

+1308
-239
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+23-177
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1,035
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub mod cleanup_post_borrowck;
4949
mod const_debuginfo;
5050
mod const_goto;
5151
mod const_prop;
52+
mod const_prop_lint;
5253
mod coverage;
5354
mod deaggregator;
5455
mod deduplicate_blocks;
@@ -430,6 +431,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
430431
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
431432
// and it can help optimizations.
432433
&deaggregator::Deaggregator,
434+
&Lint(const_prop_lint::ConstProp),
433435
];
434436

435437
pm::run_passes(tcx, body, post_borrowck_cleanup);

src/test/codegen/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// CHECK: @STATIC = {{.*}}, align 4
1111

1212
// This checks the constants from inline_enum_const
13-
// CHECK: @alloc9 = {{.*}}, align 2
13+
// CHECK: @alloc12 = {{.*}}, align 2
1414

1515
// This checks the constants from {low,high}_align_const, they share the same
1616
// constant, but the alignment differs, so the higher one should be used

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ trait Foo {
44
const BAR: u32;
55
}
66

7-
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR; //~ ERROR E0391
7+
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
88

99
struct GlobalImplRef;
1010

1111
impl GlobalImplRef {
12-
const BAR: u32 = IMPL_REF_BAR;
12+
const BAR: u32 = IMPL_REF_BAR; //~ ERROR E0391
1313
}
1414

1515
fn main() {}

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
error[E0391]: cycle detected when simplifying constant for the type system `IMPL_REF_BAR`
1+
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`
2+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
3+
|
4+
LL | const BAR: u32 = IMPL_REF_BAR;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: ...which requires normalizing `IMPL_REF_BAR`...
8+
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
29
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
310
|
411
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
512
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
713
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
814
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
915
|
@@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
3541
|
3642
LL | const BAR: u32 = IMPL_REF_BAR;
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
= note: ...which requires normalizing `IMPL_REF_BAR`...
39-
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
44+
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`, completing the cycle
4045
= note: cycle used when running analysis passes on this crate
4146

4247
error: aborting due to previous error

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ trait Foo {
55
}
66

77
trait FooDefault {
8-
const BAR: u32 = DEFAULT_REF_BAR;
8+
const BAR: u32 = DEFAULT_REF_BAR; //~ ERROR E0391
99
}
1010

11-
const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR; //~ ERROR E0391
11+
const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
1212

1313
struct GlobalDefaultRef;
1414

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
error[E0391]: cycle detected when simplifying constant for the type system `DEFAULT_REF_BAR`
1+
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
2+
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
3+
|
4+
LL | const BAR: u32 = DEFAULT_REF_BAR;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
8+
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
29
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
310
|
411
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
512
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
713
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
814
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
915
|
@@ -35,8 +41,7 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
3541
|
3642
LL | const BAR: u32 = DEFAULT_REF_BAR;
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
39-
= note: ...which again requires simplifying constant for the type system `DEFAULT_REF_BAR`, completing the cycle
44+
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
4045
= note: cycle used when running analysis passes on this crate
4146

4247
error: aborting due to previous error

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ trait Foo {
44
const BAR: u32;
55
}
66

7-
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR; //~ ERROR E0391
7+
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
88

99
struct GlobalTraitRef;
1010

1111
impl Foo for GlobalTraitRef {
12-
const BAR: u32 = TRAIT_REF_BAR;
12+
const BAR: u32 = TRAIT_REF_BAR; //~ ERROR E0391
1313
}
1414

1515
fn main() {}

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
error[E0391]: cycle detected when simplifying constant for the type system `TRAIT_REF_BAR`
1+
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`
2+
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
3+
|
4+
LL | const BAR: u32 = TRAIT_REF_BAR;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: ...which requires normalizing `TRAIT_REF_BAR`...
8+
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
29
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
310
|
411
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
512
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
713
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
814
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
915
|
@@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
3541
|
3642
LL | const BAR: u32 = TRAIT_REF_BAR;
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
= note: ...which requires normalizing `TRAIT_REF_BAR`...
39-
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
44+
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`, completing the cycle
4045
= note: cycle used when running analysis passes on this crate
4146

4247
error: aborting due to previous error
+3-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
// build-fail
1+
// build-pass
22
// compile-flags: -Zmir-opt-level=3
3+
// Overflow can't be detected by const prop
4+
// could only be detected after optimizations
35

46
#![deny(warnings)]
57

68
fn main() {
79
let _ = add(u8::MAX, 1);
8-
//~^ NOTE in this expansion of inlined source
9-
//~| NOTE in this expansion of inlined source
1010
}
1111

1212
#[inline(always)]
1313
fn add(x: u8, y: u8) -> u8 {
1414
x + y
15-
//~^ ERROR this arithmetic operation will overflow
16-
//~| NOTE attempt to compute `u8::MAX + 1_u8`, which would overflow
17-
//~| NOTE `#[deny(arithmetic_overflow)]` on by default
1815
}

src/test/ui/const_prop/inline_spans.stderr

-13
This file was deleted.

src/test/ui/consts/const-eval/const-eval-query-stack.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LL | let x: &'static i32 = &X;
2121
| ^ referenced constant has errors
2222
query stack during panic:
2323
#0 [try_normalize_mir_const_after_erasing_regions] normalizing `main::promoted[1]`
24-
#1 [optimized_mir] optimizing MIR for `main`
25-
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
24+
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `main`
25+
#2 [optimized_mir] optimizing MIR for `main`
26+
#3 [collect_and_partition_mono_items] collect_and_partition_mono_items
2627
end of query stack

src/test/ui/consts/const-eval/issue-49296.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/issue-49296.rs:9:16
33
|
44
LL | const X: u64 = *wat(42);
5-
| ^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed
5+
| ^^^^^^^^ pointer to alloc3 was dereferenced after this allocation got freed
66

77
error: aborting due to previous error
88

src/test/ui/consts/const-eval/panic-assoc-never-type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ impl PrintName {
1313

1414
fn main() {
1515
let _ = PrintName::VOID;
16+
//~^ ERROR erroneous constant used [E0080]
1617
}

src/test/ui/consts/const-eval/panic-assoc-never-type.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | const VOID: ! = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9-
error: aborting due to previous error
9+
error[E0080]: erroneous constant used
10+
--> $DIR/panic-assoc-never-type.rs:15:13
11+
|
12+
LL | let _ = PrintName::VOID;
13+
| ^^^^^^^^^^^^^^^ referenced constant has errors
14+
15+
error: aborting due to 2 previous errors
1016

1117
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/promoted_errors.noopt.stderr

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
warning: this arithmetic operation will overflow
2+
--> $DIR/promoted_errors.rs:15:5
3+
|
4+
LL | 0 - 1
5+
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/promoted_errors.rs:11:20
9+
|
10+
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
warning: this operation will panic at runtime
14+
--> $DIR/promoted_errors.rs:21:5
15+
|
16+
LL | 1 / 0
17+
| ^^^^^ attempt to divide `1_i32` by zero
18+
|
19+
note: the lint level is defined here
20+
--> $DIR/promoted_errors.rs:11:41
21+
|
22+
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
23+
| ^^^^^^^^^^^^^^^^^^^
24+
25+
warning: this operation will panic at runtime
26+
--> $DIR/promoted_errors.rs:27:5
27+
|
28+
LL | 1 / (1 - 1)
29+
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero
30+
31+
warning: this operation will panic at runtime
32+
--> $DIR/promoted_errors.rs:31:5
33+
|
34+
LL | 1 / (false as i32)
35+
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero
36+
37+
warning: this operation will panic at runtime
38+
--> $DIR/promoted_errors.rs:35:5
39+
|
40+
LL | [1, 2, 3][4]
41+
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
42+
143
warning: any use of this value will cause an error
244
--> $DIR/promoted_errors.rs:15:5
345
|
@@ -6,7 +48,7 @@ LL | 0 - 1
648
| |
749
| attempt to compute `0_u32 - 1_u32`, which would overflow
850
| inside `overflow` at $DIR/promoted_errors.rs:15:5
9-
| inside `X` at $DIR/promoted_errors.rs:38:29
51+
| inside `X` at $DIR/promoted_errors.rs:43:29
1052
...
1153
LL | / const X: () = {
1254
LL | | let _x: &'static u32 = &overflow();
@@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
2668
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
2769

2870
warning: any use of this value will cause an error
29-
--> $DIR/promoted_errors.rs:38:28
71+
--> $DIR/promoted_errors.rs:43:28
3072
|
3173
LL | / const X: () = {
3274
LL | | let _x: &'static u32 = &overflow();
@@ -41,5 +83,5 @@ LL | | };
4183
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4284
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
4385

44-
warning: 2 warnings emitted
86+
warning: 7 warnings emitted
4587

src/test/ui/consts/const-eval/promoted_errors.opt.stderr

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
1+
warning: this arithmetic operation will overflow
2+
--> $DIR/promoted_errors.rs:15:5
3+
|
4+
LL | 0 - 1
5+
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/promoted_errors.rs:11:20
9+
|
10+
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
warning: this operation will panic at runtime
14+
--> $DIR/promoted_errors.rs:21:5
15+
|
16+
LL | 1 / 0
17+
| ^^^^^ attempt to divide `1_i32` by zero
18+
|
19+
note: the lint level is defined here
20+
--> $DIR/promoted_errors.rs:11:41
21+
|
22+
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
23+
| ^^^^^^^^^^^^^^^^^^^
24+
25+
warning: this operation will panic at runtime
26+
--> $DIR/promoted_errors.rs:27:5
27+
|
28+
LL | 1 / (1 - 1)
29+
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero
30+
31+
warning: this operation will panic at runtime
32+
--> $DIR/promoted_errors.rs:31:5
33+
|
34+
LL | 1 / (false as i32)
35+
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero
36+
37+
warning: this operation will panic at runtime
38+
--> $DIR/promoted_errors.rs:35:5
39+
|
40+
LL | [1, 2, 3][4]
41+
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
42+
143
warning: any use of this value will cause an error
2-
--> $DIR/promoted_errors.rs:20:5
44+
--> $DIR/promoted_errors.rs:21:5
345
|
446
LL | 1 / 0
547
| ^^^^^
648
| |
749
| attempt to divide `1_i32` by zero
8-
| inside `div_by_zero1` at $DIR/promoted_errors.rs:20:5
9-
| inside `X` at $DIR/promoted_errors.rs:41:29
50+
| inside `div_by_zero1` at $DIR/promoted_errors.rs:21:5
51+
| inside `X` at $DIR/promoted_errors.rs:46:29
1052
...
1153
LL | / const X: () = {
1254
LL | | let _x: &'static u32 = &overflow();
@@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
2668
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
2769

2870
warning: any use of this value will cause an error
29-
--> $DIR/promoted_errors.rs:41:28
71+
--> $DIR/promoted_errors.rs:46:28
3072
|
3173
LL | / const X: () = {
3274
LL | | let _x: &'static u32 = &overflow();
@@ -42,5 +84,5 @@ LL | | };
4284
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4385
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
4486

45-
warning: 2 warnings emitted
87+
warning: 7 warnings emitted
4688

0 commit comments

Comments
 (0)