Skip to content

Commit

Permalink
Enable ConstPropLint for promoteds
Browse files Browse the repository at this point in the history
This fixes the issue wherein the lint didn't fire for promoteds
in the case of SHL/SHR operators in non-optimized builds
and all arithmetic operators in optimized builds
  • Loading branch information
gurry committed Dec 31, 2023
1 parent dfb1f5e commit c1f91b7
Show file tree
Hide file tree
Showing 17 changed files with 5,697 additions and 23 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 @@ -42,11 +42,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
8 changes: 4 additions & 4 deletions tests/ui/associated-consts/defaults-cyclic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ 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]`
--> $DIR/defaults-cyclic-fail.rs:16:16
note: cycle used when simplifying constant for the type system `Tr::A`
--> $DIR/defaults-cyclic-fail.rs:5:5
|
LL | assert_eq!(<() as Tr>::A, 0);
| ^^^^^^^^^^^^^
LL | const A: u8 = Self::B;
| ^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: aborting due to 1 previous error
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
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`.
3 changes: 1 addition & 2 deletions tests/ui/consts/promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ fn main() {
assert_static(&["d", "e", "f"]);
assert_eq!(C, 42);

// make sure that these do not cause trouble despite overflowing
// make sure that this does not cause trouble despite overflowing
assert_static(&(0-1));
assert_static(&-i32::MIN);

// div-by-non-0 is okay
assert_static(&(1/1));
Expand Down
Loading

0 comments on commit c1f91b7

Please sign in to comment.