Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always evaluate constant operands in const-prop #98426

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ jobs:
- name: x86_64-gnu-llvm-12
os: ubuntu-20.04-xl
env: {}
- name: dist-i586-gnu-i586-i686-musl
os: ubuntu-20.04-xl
env: {}
Comment on lines +49 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did this accidentally get added during a rebase?

- name: x86_64-gnu-tools
env:
CI_ONLY_WHEN_SUBMODULES_CHANGED: 1
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {

/// Returns the value, if any, of evaluating `c`.
fn eval_constant(&mut self, c: &Constant<'tcx>) -> Option<OpTy<'tcx>> {
// FIXME we need to revisit this for #67176
if c.needs_subst() {
return None;
}

oli-obk marked this conversation as resolved.
Show resolved Hide resolved
self.ecx.mir_const_to_op(&c.literal, None).ok()
}

Expand Down
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 @@ -514,11 +514,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
| Rvalue::NullaryOp(..) => {}
}

// FIXME we need to revisit this for #67176
if rvalue.needs_subst() {
return None;
}

self.use_ecx(source_info, |this| this.ecx.eval_rvalue_into_place(rvalue, place))
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/ci/github-actions/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ jobs:

- name: x86_64-gnu-llvm-12
<<: *job-linux-xl


- name: dist-i586-gnu-i586-i686-musl
<<: *job-linux-xl

Comment on lines -289 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

- name: x86_64-gnu-tools
env:
CI_ONLY_WHEN_SUBMODULES_CHANGED: 1
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/consts/const-eval/erroneous-const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ impl<T> PrintName<T> {
const VOID: () = [()][2]; //~WARN any use of this value will cause an error
//~^ WARN this operation will panic at runtime
//~| WARN this was previously accepted by the compiler but is being phased out
//~| WARN this was previously accepted by the compiler but is being phased out
//~| WARN any use of this value will cause an error
}

const fn no_codegen<T>() {
Expand Down
32 changes: 28 additions & 4 deletions src/test/ui/consts/const-eval/erroneous-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ LL | #![warn(const_err, unconditional_panic)]
= 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: any use of this value will cause an error
--> $DIR/erroneous-const.rs:6:22
|
LL | const VOID: () = [()][2];
| -------------- ^^^^^^^ index out of bounds: the length is 1 but the index is 2
|
= 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>

error[E0080]: could not evaluate static initializer
--> $DIR/erroneous-const.rs:15:17
--> $DIR/erroneous-const.rs:17:17
|
LL | let _ = PrintName::<T>::VOID;
| ^^^^^^^^^^^^^^^^^^^^
| |
| referenced constant has errors
| inside `no_codegen::<i32>` at $DIR/erroneous-const.rs:15:17
| inside `no_codegen::<i32>` at $DIR/erroneous-const.rs:17:17
...
LL | pub static FOO: () = no_codegen::<i32>();
| ------------------- inside `FOO` at $DIR/erroneous-const.rs:19:22
| ------------------- inside `FOO` at $DIR/erroneous-const.rs:21:22

error: aborting due to previous error; 2 warnings emitted
error: aborting due to previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0080`.
Future incompatibility report: Future breakage diagnostic:
Expand All @@ -54,3 +63,18 @@ LL | #![warn(const_err, unconditional_panic)]
= 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>

Future breakage diagnostic:
warning: any use of this value will cause an error
--> $DIR/erroneous-const.rs:6:22
|
LL | const VOID: () = [()][2];
| -------------- ^^^^^^^ index out of bounds: the length is 1 but the index is 2
|
note: the lint level is defined here
--> $DIR/erroneous-const.rs:2:9
|
LL | #![warn(const_err, unconditional_panic)]
| ^^^^^^^^^
= 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>

3 changes: 3 additions & 0 deletions src/test/ui/consts/const-eval/issue-50814.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ struct Sum<A,B>(A,B);
impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A,B> {
const MAX: u8 = A::MAX + B::MAX;
//~^ ERROR any use of this value will cause an error [const_err]
//~| ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
//~| WARN this was previously accepted by the compiler but is being phased out
}

fn foo<T>(_: T) -> &'static u8 {
&Sum::<U8,U8>::MAX
//~^ ERROR E0080
//~| ERROR evaluation of `foo::<i32>` failed
}

fn main() {
Expand Down
32 changes: 29 additions & 3 deletions src/test/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,34 @@ LL | const MAX: u8 = A::MAX + B::MAX;
= 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>

error[E0080]: evaluation of `foo::<T>` failed
--> $DIR/issue-50814.rs:23:6
|
LL | &Sum::<U8,U8>::MAX
| ^^^^^^^^^^^^^^^^^ referenced constant has errors

error: any use of this value will cause an error
--> $DIR/issue-50814.rs:15:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ------------- ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow
|
= 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>

error[E0080]: evaluation of `foo::<i32>` failed
--> $DIR/issue-50814.rs:21:6
--> $DIR/issue-50814.rs:23:6
|
LL | &Sum::<U8,U8>::MAX
| ^^^^^^^^^^^^^^^^^ referenced constant has errors

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

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

For more information about this error, try `rustc --explain E0080`.
Future incompatibility report: Future breakage diagnostic:
Expand All @@ -34,3 +49,14 @@ LL | const MAX: u8 = A::MAX + B::MAX;
= 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>

Future breakage diagnostic:
error: any use of this value will cause an error
--> $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: `#[deny(const_err)]` on by default
= 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>