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

Fix const_err with -(-0.0) #64063

Merged
merged 7 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 8 additions & 4 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
}

let arg = self.eval_operand(arg, source_info)?;
let oflo_check = self.tcx.sess.overflow_checks();
let val = self.use_ecx(source_info, |this| {
let prim = this.ecx.read_immediate(arg)?;
match op {
UnOp::Neg => {
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
JohnTitor marked this conversation as resolved.
Show resolved Hide resolved
// Need to do overflow check here: For actual CTFE, MIR
// generation emits code that does this before calling the op.
if prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
// We check overflow in debug mode already
// so should only check in release mode.
if !oflo_check
&& prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
throw_panic!(OverflowNeg)
}
}
Expand Down Expand Up @@ -485,7 +487,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
Scalar::from_bool(overflow).into(),
)
} else {
if overflow {
// We check overflow in debug mode already
// so should only check in release mode.
if !self.tcx.sess.overflow_checks() && overflow {
let err = err_panic!(Overflow(op)).into();
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
return None;
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/consts/issue-64059.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let _ = -(-0.0);
//~^ ERROR: this expression will panic at runtime
JohnTitor marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions src/test/ui/consts/issue-64059.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: this expression will panic at runtime
--> $DIR/issue-64059.rs:2:13
|
LL | let _ = -(-0.0);
| ^^^^^^^ attempt to negate with overflow
|
= note: `#[deny(const_err)]` on by default

error: aborting due to previous error