Skip to content

Commit

Permalink
Auto merge of #119432 - gurry:117949-make-lint-run-on-promoteds, r=ol…
Browse files Browse the repository at this point in the history
…i-obk

Make `ConstPropLint` lint run on promoteds

Fixes #117949 wherein the lint didn't fire for the following promoteds:

- SHL or SHR operators in a non-optimized build
- any arithmetic operator in an optimized build

What I have done here is simply enabled `ConstPropLint` to run on promoted bodies by removing the relevant `if` check.

After this change _all_ promoted arithmetic operators will lint _in both non-optimized and optimized builds_. On the flip side programs containing the above mentioned overflowing promoteds that were accepted earlier will now be rejected. Hope that is okay from a backward compatibility standpoint.

I have added tests covering all overflowing promoted & non-promoted ops for both compile-time and runtime operations and for optimized as well as non-optimized builds.

I had to amend some existing tests to make them pass and had to delete a couple that were set to pass despite overflows.

This PR increases the number of duplicate diagnostics emitted (because the same operator might get linted in both the promoted MIR and the main MIR). I hope that is an acceptable trade-off given that we now lint overflows much more comprehensively than earlier.
  • Loading branch information
bors committed Feb 17, 2024
2 parents 4316d0c + 5010ca0 commit dfdbe30
Show file tree
Hide file tree
Showing 29 changed files with 6,967 additions and 738 deletions.
5 changes: 0 additions & 5 deletions compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
return;
}

// will be evaluated by miri and produce its errors there
if body.source.promoted.is_some() {
return;
}

let def_id = body.source.def_id().expect_local();
let def_kind = tcx.def_kind(def_id);
let is_fn_like = def_kind.is_fn_like();
Expand Down
7 changes: 6 additions & 1 deletion src/tools/miri/tests/pass/overflow_checks_off.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//@compile-flags: -C overflow-checks=off

// Check that we correctly implement the intended behavior of these operators
// when they are not being overflow-checked.
// when they are not being overflow-checked at runtime.

// FIXME: if we call the functions in `std::ops`, we still get the panics.
// Miri does not implement the codegen-time hack that backs `#[rustc_inherit_overflow_checks]`.
// use std::ops::*;


// Disable _compile-time_ overflow linting
// so that we can test runtime overflow checks
#![allow(arithmetic_overflow)]

fn main() {
assert_eq!(-{ -0x80i8 }, -0x80);

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-consts/defaults-cyclic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ note: ...which requires const-evaluating + checking `Tr::B`...
LL | const B: u8 = Self::A;
| ^^^^^^^
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
note: cycle used when const-evaluating + checking `main::promoted[1]`
note: cycle used when optimizing promoted MIR for `main`
--> $DIR/defaults-cyclic-fail.rs:16:16
|
LL | assert_eq!(<() as Tr>::A, 0);
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/associated-consts/defaults-not-assumed-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ note: erroneous constant encountered
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
| ^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/defaults-not-assumed-fail.rs:33:16
|
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
| ^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/defaults-not-assumed-fail.rs:33:5
|
Expand Down
48 changes: 0 additions & 48 deletions tests/ui/consts/const-err2.noopt.stderr

This file was deleted.

48 changes: 0 additions & 48 deletions tests/ui/consts/const-err2.opt.stderr

This file was deleted.

48 changes: 0 additions & 48 deletions tests/ui/consts/const-err2.opt_with_overflow_checks.stderr

This file was deleted.

39 changes: 0 additions & 39 deletions tests/ui/consts/const-err2.rs

This file was deleted.

8 changes: 8 additions & 0 deletions tests/ui/consts/const-eval/issue-44578.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ note: erroneous constant encountered
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-44578.rs:25:20
|
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-44578.rs:25:20
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/consts/const-eval/issue-50814.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Sum<A, B>(A, B);
impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A, B> {
const MAX: u8 = A::MAX + B::MAX;
//~^ ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
//~| ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
}

fn foo<T>(_: T) -> &'static u8 {
Expand Down
22 changes: 19 additions & 3 deletions tests/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@ LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow

note: erroneous constant encountered
--> $DIR/issue-50814.rs:20:6
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^

error[E0080]: evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
--> $DIR/issue-50814.rs:15:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: the above error was encountered while instantiating `fn foo::<i32>`
--> $DIR/issue-50814.rs:25:5
--> $DIR/issue-50814.rs:26:5
|
LL | foo(0);
| ^^^^^^

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

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

0 comments on commit dfdbe30

Please sign in to comment.