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

Lines changed: 23 additions & 177 deletions
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 1035 additions & 0 deletions
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 4 deletions
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

0 commit comments

Comments
 (0)