Skip to content

Commit

Permalink
Auto merge of #94934 - Lireer:const-prop-lint, r=oli-obk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Mar 24, 2022
2 parents 4b133a7 + 440946a commit 63b8f01
Show file tree
Hide file tree
Showing 30 changed files with 1,308 additions and 239 deletions.
200 changes: 23 additions & 177 deletions compiler/rustc_mir_transform/src/const_prop.rs

Large diffs are not rendered by default.

1,035 changes: 1,035 additions & 0 deletions compiler/rustc_mir_transform/src/const_prop_lint.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod cleanup_post_borrowck;
mod const_debuginfo;
mod const_goto;
mod const_prop;
mod const_prop_lint;
mod coverage;
mod deaggregator;
mod deduplicate_blocks;
Expand Down Expand Up @@ -430,6 +431,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
// and it can help optimizations.
&deaggregator::Deaggregator,
&Lint(const_prop_lint::ConstProp),
];

pm::run_passes(tcx, body, post_borrowck_cleanup);
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// CHECK: @STATIC = {{.*}}, align 4

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

// This checks the constants from {low,high}_align_const, they share the same
// constant, but the alignment differs, so the higher one should be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ trait Foo {
const BAR: u32;
}

const IMPL_REF_BAR: u32 = GlobalImplRef::BAR; //~ ERROR E0391
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;

struct GlobalImplRef;

impl GlobalImplRef {
const BAR: u32 = IMPL_REF_BAR;
const BAR: u32 = IMPL_REF_BAR; //~ ERROR E0391
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
error[E0391]: cycle detected when simplifying constant for the type system `IMPL_REF_BAR`
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which requires normalizing `IMPL_REF_BAR`...
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
Expand Down Expand Up @@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires normalizing `IMPL_REF_BAR`...
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
= 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
= note: cycle used when running analysis passes on this crate

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ trait Foo {
}

trait FooDefault {
const BAR: u32 = DEFAULT_REF_BAR;
const BAR: u32 = DEFAULT_REF_BAR; //~ ERROR E0391
}

const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR; //~ ERROR E0391
const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;

struct GlobalDefaultRef;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
error[E0391]: cycle detected when simplifying constant for the type system `DEFAULT_REF_BAR`
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
Expand Down Expand Up @@ -35,8 +41,7 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
= note: ...which again requires simplifying constant for the type system `DEFAULT_REF_BAR`, completing the cycle
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
= note: cycle used when running analysis passes on this crate

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ trait Foo {
const BAR: u32;
}

const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR; //~ ERROR E0391
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;

struct GlobalTraitRef;

impl Foo for GlobalTraitRef {
const BAR: u32 = TRAIT_REF_BAR;
const BAR: u32 = TRAIT_REF_BAR; //~ ERROR E0391
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
error[E0391]: cycle detected when simplifying constant for the type system `TRAIT_REF_BAR`
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
|
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which requires normalizing `TRAIT_REF_BAR`...
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
Expand Down Expand Up @@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires normalizing `TRAIT_REF_BAR`...
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
= 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
= note: cycle used when running analysis passes on this crate

error: aborting due to previous error
Expand Down
9 changes: 3 additions & 6 deletions src/test/ui/const_prop/inline_spans.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// build-fail
// build-pass
// compile-flags: -Zmir-opt-level=3
// Overflow can't be detected by const prop
// could only be detected after optimizations

#![deny(warnings)]

fn main() {
let _ = add(u8::MAX, 1);
//~^ NOTE in this expansion of inlined source
//~| NOTE in this expansion of inlined source
}

#[inline(always)]
fn add(x: u8, y: u8) -> u8 {
x + y
//~^ ERROR this arithmetic operation will overflow
//~| NOTE attempt to compute `u8::MAX + 1_u8`, which would overflow
//~| NOTE `#[deny(arithmetic_overflow)]` on by default
}
13 changes: 0 additions & 13 deletions src/test/ui/const_prop/inline_spans.stderr

This file was deleted.

5 changes: 3 additions & 2 deletions src/test/ui/consts/const-eval/const-eval-query-stack.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LL | let x: &'static i32 = &X;
| ^ referenced constant has errors
query stack during panic:
#0 [try_normalize_mir_const_after_erasing_regions] normalizing `main::promoted[1]`
#1 [optimized_mir] optimizing MIR for `main`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `main`
#2 [optimized_mir] optimizing MIR for `main`
#3 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-49296.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
--> $DIR/issue-49296.rs:9:16
|
LL | const X: u64 = *wat(42);
| ^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed
| ^^^^^^^^ pointer to alloc3 was dereferenced after this allocation got freed

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/panic-assoc-never-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ impl PrintName {

fn main() {
let _ = PrintName::VOID;
//~^ ERROR erroneous constant used [E0080]
}
8 changes: 7 additions & 1 deletion src/test/ui/consts/const-eval/panic-assoc-never-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ LL | const VOID: ! = panic!();
|
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)

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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
48 changes: 45 additions & 3 deletions src/test/ui/consts/const-eval/promoted_errors.noopt.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
warning: this arithmetic operation will overflow
--> $DIR/promoted_errors.rs:15:5
|
LL | 0 - 1
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
|
note: the lint level is defined here
--> $DIR/promoted_errors.rs:11:20
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
| ^^^^^^^^^^^^^^^^^^^

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:21:5
|
LL | 1 / 0
| ^^^^^ attempt to divide `1_i32` by zero
|
note: the lint level is defined here
--> $DIR/promoted_errors.rs:11:41
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
| ^^^^^^^^^^^^^^^^^^^

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:27:5
|
LL | 1 / (1 - 1)
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:31:5
|
LL | 1 / (false as i32)
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:35:5
|
LL | [1, 2, 3][4]
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4

warning: any use of this value will cause an error
--> $DIR/promoted_errors.rs:15:5
|
Expand All @@ -6,7 +48,7 @@ LL | 0 - 1
| |
| attempt to compute `0_u32 - 1_u32`, which would overflow
| inside `overflow` at $DIR/promoted_errors.rs:15:5
| inside `X` at $DIR/promoted_errors.rs:38:29
| inside `X` at $DIR/promoted_errors.rs:43:29
...
LL | / const X: () = {
LL | | let _x: &'static u32 = &overflow();
Expand All @@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

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

warning: 2 warnings emitted
warning: 7 warnings emitted

52 changes: 47 additions & 5 deletions src/test/ui/consts/const-eval/promoted_errors.opt.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
warning: this arithmetic operation will overflow
--> $DIR/promoted_errors.rs:15:5
|
LL | 0 - 1
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
|
note: the lint level is defined here
--> $DIR/promoted_errors.rs:11:20
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
| ^^^^^^^^^^^^^^^^^^^

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:21:5
|
LL | 1 / 0
| ^^^^^ attempt to divide `1_i32` by zero
|
note: the lint level is defined here
--> $DIR/promoted_errors.rs:11:41
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
| ^^^^^^^^^^^^^^^^^^^

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:27:5
|
LL | 1 / (1 - 1)
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:31:5
|
LL | 1 / (false as i32)
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero

warning: this operation will panic at runtime
--> $DIR/promoted_errors.rs:35:5
|
LL | [1, 2, 3][4]
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4

warning: any use of this value will cause an error
--> $DIR/promoted_errors.rs:20:5
--> $DIR/promoted_errors.rs:21:5
|
LL | 1 / 0
| ^^^^^
| |
| attempt to divide `1_i32` by zero
| inside `div_by_zero1` at $DIR/promoted_errors.rs:20:5
| inside `X` at $DIR/promoted_errors.rs:41:29
| inside `div_by_zero1` at $DIR/promoted_errors.rs:21:5
| inside `X` at $DIR/promoted_errors.rs:46:29
...
LL | / const X: () = {
LL | | let _x: &'static u32 = &overflow();
Expand All @@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

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

warning: 2 warnings emitted
warning: 7 warnings emitted

Loading

0 comments on commit 63b8f01

Please sign in to comment.