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

Clean up diagnostics for arithmetic operation errors #76754

Merged
merged 7 commits into from
Sep 29, 2020
Merged
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
56 changes: 30 additions & 26 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,49 +1300,49 @@ impl<O> AssertKind<O> {
match self {
BoundsCheck { ref len, ref index } => write!(
f,
"\"index out of bounds: the len is {{}} but the index is {{}}\", {:?}, {:?}",
"\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}",
len, index
),

OverflowNeg(op) => {
write!(f, "\"attempt to negate {{}} which would overflow\", {:?}", op)
write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op)
}
DivisionByZero(op) => write!(f, "\"attempt to divide {{}} by zero\", {:?}", op),
DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op),
RemainderByZero(op) => write!(
f,
"\"attempt to calculate the remainder of {{}} with a divisor of zero\", {:?}",
"\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}",
op
),
Overflow(BinOp::Add, l, r) => write!(
f,
"\"attempt to compute `{{}} + {{}}` which would overflow\", {:?}, {:?}",
"\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}",
l, r
),
Overflow(BinOp::Sub, l, r) => write!(
f,
"\"attempt to compute `{{}} - {{}}` which would overflow\", {:?}, {:?}",
"\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}",
l, r
),
Overflow(BinOp::Mul, l, r) => write!(
f,
"\"attempt to compute `{{}} * {{}}` which would overflow\", {:?}, {:?}",
"\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}",
l, r
),
Overflow(BinOp::Div, l, r) => write!(
f,
"\"attempt to compute `{{}} / {{}}` which would overflow\", {:?}, {:?}",
"\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}",
l, r
),
Overflow(BinOp::Rem, l, r) => write!(
f,
"\"attempt to compute the remainder of `{{}} % {{}}` which would overflow\", {:?}, {:?}",
"\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}",
l, r
),
Overflow(BinOp::Shr, _, r) => {
write!(f, "\"attempt to shift right by {{}} which would overflow\", {:?}", r)
write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r)
}
Overflow(BinOp::Shl, _, r) => {
write!(f, "\"attempt to shift left by {{}} which would overflow\", {:?}", r)
write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r)
}
_ => write!(f, "\"{}\"", self.description()),
}
Expand All @@ -1353,36 +1353,40 @@ impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use AssertKind::*;
match self {
BoundsCheck { ref len, ref index } => {
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index)
}
OverflowNeg(op) => write!(f, "attempt to negate {:#?} which would overflow", op),
DivisionByZero(op) => write!(f, "attempt to divide {:#?} by zero", op),
RemainderByZero(op) => {
write!(f, "attempt to calculate the remainder of {:#?} with a divisor of zero", op)
}
BoundsCheck { ref len, ref index } => write!(
f,
"index out of bounds: the length is {:?} but the index is {:?}",
len, index
),
OverflowNeg(op) => write!(f, "attempt to negate `{:#?}`, which would overflow", op),
DivisionByZero(op) => write!(f, "attempt to divide `{:#?}` by zero", op),
RemainderByZero(op) => write!(
f,
"attempt to calculate the remainder of `{:#?}` with a divisor of zero",
op
),
Overflow(BinOp::Add, l, r) => {
write!(f, "attempt to compute `{:#?} + {:#?}` which would overflow", l, r)
write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r)
}
Overflow(BinOp::Sub, l, r) => {
write!(f, "attempt to compute `{:#?} - {:#?}` which would overflow", l, r)
write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r)
}
Overflow(BinOp::Mul, l, r) => {
write!(f, "attempt to compute `{:#?} * {:#?}` which would overflow", l, r)
write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r)
}
Overflow(BinOp::Div, l, r) => {
write!(f, "attempt to compute `{:#?} / {:#?}` which would overflow", l, r)
write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r)
}
Overflow(BinOp::Rem, l, r) => write!(
f,
"attempt to compute the remainder of `{:#?} % {:#?}` which would overflow",
"attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow",
l, r
),
Overflow(BinOp::Shr, _, r) => {
write!(f, "attempt to shift right by {:#?} which would overflow", r)
write!(f, "attempt to shift right by `{:#?}`, which would overflow", r)
}
Overflow(BinOp::Shl, _, r) => {
write!(f, "attempt to shift left by {:#?} which would overflow", r)
write!(f, "attempt to shift left by `{:#?}`, which would overflow", r)
}
_ => write!(f, "{}", self.description()),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.sess,
span,
E0029,
"only char and numeric types are allowed in range patterns"
"only `char` and numeric types are allowed in range patterns"
);
let msg = |ty| format!("this is of type `{}` but it should be `char` or numeric", ty);
let mut one_side_err = |first_span, first_ty, second: Option<(bool, Ty<'tcx>, Span)>| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() -> () {
_7 = _2; // scope 3 at $DIR/array-index-is-temporary.rs:16:7: 16:8
_8 = Len(_1); // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
_9 = Lt(_7, _8); // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() -> () {
_7 = _2; // scope 3 at $DIR/array-index-is-temporary.rs:16:7: 16:8
_8 = Len(_1); // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
_9 = Lt(_7, _8); // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
- _4 = Len(_1); // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
+ _4 = const 2_usize; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
_5 = Lt(_3, _4); // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
}

bb1: {
Expand All @@ -44,7 +44,7 @@
- _8 = Len(_1); // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
+ _8 = const 2_usize; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
_9 = Lt(_7, _8); // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
- _4 = Len(_1); // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
+ _4 = const 2_usize; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
_5 = Lt(_3, _4); // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
}

bb1: {
Expand All @@ -44,7 +44,7 @@
- _8 = Len(_1); // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
+ _8 = const 2_usize; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
_9 = Lt(_7, _8); // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
_3 = const 2_usize; // scope 0 at $DIR/array_index.rs:5:31: 5:32
_4 = const 4_usize; // scope 0 at $DIR/array_index.rs:5:18: 5:33
- _5 = Lt(_3, _4); // scope 0 at $DIR/array_index.rs:5:18: 5:33
- assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ _5 = const true; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ assert(const true, "index out of bounds: the len is {} but the index is {}", const 4_usize, const 2_usize) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
_3 = const 2_usize; // scope 0 at $DIR/array_index.rs:5:31: 5:32
_4 = const 4_usize; // scope 0 at $DIR/array_index.rs:5:18: 5:33
- _5 = Lt(_3, _4); // scope 0 at $DIR/array_index.rs:5:18: 5:33
- assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ _5 = const true; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ assert(const true, "index out of bounds: the len is {} but the index is {}", const 4_usize, const 2_usize) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> bb1; // scope 0 at $DIR/array_index.rs:5:18: 5:33
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
StorageLive(_3); // scope 1 at $DIR/bad_op_div_by_zero.rs:5:18: 5:19
- _3 = _1; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:18: 5:19
- _4 = Eq(_3, const 0_i32); // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- assert(!move _4, "attempt to divide {} by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- assert(!move _4, "attempt to divide `{}` by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ _3 = const 0_i32; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:18: 5:19
+ _4 = const true; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ assert(!const true, "attempt to divide {} by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ assert(!const true, "attempt to divide `{}` by zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
}

bb1: {
- _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- assert(!move _7, "attempt to compute `{} / {}` which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
- assert(!move _7, "attempt to compute `{} / {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ _5 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ _6 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ _7 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ assert(!const false, "attempt to compute `{} / {}` which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
+ assert(!const false, "attempt to compute `{} / {}`, which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:5:14: 5:19
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
StorageLive(_3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:18: 5:19
- _3 = _1; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:18: 5:19
- _4 = Eq(_3, const 0_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- assert(!move _4, "attempt to calculate the remainder of {} with a divisor of zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- assert(!move _4, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ _3 = const 0_i32; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:18: 5:19
+ _4 = const true; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ assert(!const true, "attempt to calculate the remainder of {} with a divisor of zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_i32) -> bb1; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
}

bb1: {
- _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- assert(!move _7, "attempt to compute the remainder of `{} % {}` which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
- assert(!move _7, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ _5 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ _6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ _7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ assert(!const false, "attempt to compute the remainder of `{} % {}` which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
+ assert(!const false, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:5:14: 5:19
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
_6 = const 3_usize; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:23: 7:24
_7 = Len((*_1)); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- _8 = Lt(_6, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ _8 = Lt(const 3_usize, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
_6 = const 3_usize; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:23: 7:24
_7 = Len((*_1)); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- _8 = Lt(_6, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ _8 = Lt(const 3_usize, _7); // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 3_usize) -> bb1; // scope 2 at $DIR/bad_op_unsafe_oob_for_slices.rs:7:18: 7:25
}

bb1: {
Expand Down
Loading