Skip to content

Commit d8dcdec

Browse files
authored
Rollup merge of #71021 - robojumper:71000-mir-assert-syntax, r=jonas-schievink
Use write!-style syntax for MIR assert terminator Fixes #71000. r? @jonas-schievink
2 parents b794cb2 + b78ff99 commit d8dcdec

File tree

15 files changed

+42
-25
lines changed

15 files changed

+42
-25
lines changed

src/librustc_middle/mir/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,21 @@ impl<O> AssertKind<O> {
14031403
BoundsCheck { .. } => bug!("Unexpected AssertKind"),
14041404
}
14051405
}
1406+
1407+
/// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
1408+
fn fmt_assert_args<W: Write>(&self, f: &mut W) -> fmt::Result
1409+
where
1410+
O: Debug,
1411+
{
1412+
match self {
1413+
AssertKind::BoundsCheck { ref len, ref index } => write!(
1414+
f,
1415+
"\"index out of bounds: the len is {{}} but the index is {{}}\", {:?}, {:?}",
1416+
len, index
1417+
),
1418+
_ => write!(f, "\"{}\"", self.description()),
1419+
}
1420+
}
14061421
}
14071422

14081423
impl<O: fmt::Debug> fmt::Debug for AssertKind<O> {
@@ -1480,7 +1495,9 @@ impl<'tcx> TerminatorKind<'tcx> {
14801495
if !expected {
14811496
write!(fmt, "!")?;
14821497
}
1483-
write!(fmt, "{:?}, \"{:?}\")", cond, msg)
1498+
write!(fmt, "{:?}, ", cond)?;
1499+
msg.fmt_assert_args(fmt)?;
1500+
write!(fmt, ")")
14841501
}
14851502
FalseEdges { .. } => write!(fmt, "falseEdges"),
14861503
FalseUnwind { .. } => write!(fmt, "falseUnwind"),

src/test/mir-opt/array-index-is-temporary/32bit/rustc.main.SimplifyCfg-elaborate-drops.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> () {
7575
_7 = _2; // bb1[2]: scope 3 at $DIR/array-index-is-temporary.rs:16:7: 16:8
7676
_8 = Len(_1); // bb1[3]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
7777
_9 = Lt(_7, _8); // bb1[4]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
78-
assert(move _9, "index out of bounds: the len is move _8 but the index is _7") -> bb2; // bb1[5]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
78+
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // bb1[5]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
7979
}
8080

8181
bb2: {

src/test/mir-opt/array-index-is-temporary/64bit/rustc.main.SimplifyCfg-elaborate-drops.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> () {
7575
_7 = _2; // bb1[2]: scope 3 at $DIR/array-index-is-temporary.rs:16:7: 16:8
7676
_8 = Len(_1); // bb1[3]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
7777
_9 = Lt(_7, _8); // bb1[4]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
78-
assert(move _9, "index out of bounds: the len is move _8 but the index is _7") -> bb2; // bb1[5]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
78+
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // bb1[5]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:9
7979
}
8080

8181
bb2: {

src/test/mir-opt/combine_array_len/32bit/rustc.norm2.InstCombine.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
+ // + span: $DIR/combine_array_len.rs:5:13: 5:17
4545
+ // + literal: Const { ty: usize, val: Value(Scalar(0x00000002)) }
4646
_5 = Lt(_3, _4); // bb0[4]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
47-
assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[5]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
47+
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[5]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
4848
}
4949

5050
bb1: {
@@ -68,7 +68,7 @@
6868
+ // + span: $DIR/combine_array_len.rs:6:13: 6:17
6969
+ // + literal: Const { ty: usize, val: Value(Scalar(0x00000002)) }
7070
_9 = Lt(_7, _8); // bb1[6]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
71-
assert(move _9, "index out of bounds: the len is move _8 but the index is _7") -> bb2; // bb1[7]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
71+
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // bb1[7]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
7272
}
7373

7474
bb2: {

src/test/mir-opt/combine_array_len/64bit/rustc.norm2.InstCombine.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
+ // + span: $DIR/combine_array_len.rs:5:13: 5:17
4545
+ // + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000002)) }
4646
_5 = Lt(_3, _4); // bb0[4]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
47-
assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[5]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
47+
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[5]: scope 0 at $DIR/combine_array_len.rs:5:13: 5:17
4848
}
4949

5050
bb1: {
@@ -68,7 +68,7 @@
6868
+ // + span: $DIR/combine_array_len.rs:6:13: 6:17
6969
+ // + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000002)) }
7070
_9 = Lt(_7, _8); // bb1[6]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
71-
assert(move _9, "index out of bounds: the len is move _8 but the index is _7") -> bb2; // bb1[7]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
71+
assert(move _9, "index out of bounds: the len is {} but the index is {}", move _8, _7) -> bb2; // bb1[7]: scope 1 at $DIR/combine_array_len.rs:6:13: 6:17
7272
}
7373

7474
bb2: {

src/test/mir-opt/const_prop/array_index/32bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
// + span: $DIR/array_index.rs:5:18: 5:33
5757
// + literal: Const { ty: usize, val: Value(Scalar(0x00000004)) }
5858
- _5 = Lt(_3, _4); // bb0[6]: scope 0 at $DIR/array_index.rs:5:18: 5:33
59-
- assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
59+
- assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6060
+ _5 = const true; // bb0[6]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6161
+ // ty::Const
6262
+ // + ty: bool
6363
+ // + val: Value(Scalar(0x01))
6464
+ // mir::Constant
6565
+ // + span: $DIR/array_index.rs:5:18: 5:33
6666
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
67-
+ assert(const true, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
67+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6868
+ // ty::Const
6969
+ // + ty: bool
7070
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/array_index/64bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@
5656
// + span: $DIR/array_index.rs:5:18: 5:33
5757
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000004)) }
5858
- _5 = Lt(_3, _4); // bb0[6]: scope 0 at $DIR/array_index.rs:5:18: 5:33
59-
- assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
59+
- assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6060
+ _5 = const true; // bb0[6]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6161
+ // ty::Const
6262
+ // + ty: bool
6363
+ // + val: Value(Scalar(0x01))
6464
+ // mir::Constant
6565
+ // + span: $DIR/array_index.rs:5:18: 5:33
6666
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
67-
+ assert(const true, "index out of bounds: the len is move _4 but the index is _3") -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
67+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> bb1; // bb0[7]: scope 0 at $DIR/array_index.rs:5:18: 5:33
6868
+ // ty::Const
6969
+ // + ty: bool
7070
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/optimizes_into_variable/32bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@
119119
// + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
120120
// + literal: Const { ty: usize, val: Value(Scalar(0x00000006)) }
121121
- _7 = Lt(_5, _6); // bb1[7]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
122-
- assert(move _7, "index out of bounds: the len is move _6 but the index is _5") -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
122+
- assert(move _7, "index out of bounds: the len is {} but the index is {}", move _6, _5) -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
123123
+ _7 = const true; // bb1[7]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
124124
+ // ty::Const
125125
+ // + ty: bool
126126
+ // + val: Value(Scalar(0x01))
127127
+ // mir::Constant
128128
+ // + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
129129
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
130-
+ assert(const true, "index out of bounds: the len is move _6 but the index is _5") -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
130+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _6, _5) -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
131131
+ // ty::Const
132132
+ // + ty: bool
133133
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/optimizes_into_variable/64bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@
119119
// + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
120120
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000006)) }
121121
- _7 = Lt(_5, _6); // bb1[7]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
122-
- assert(move _7, "index out of bounds: the len is move _6 but the index is _5") -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
122+
- assert(move _7, "index out of bounds: the len is {} but the index is {}", move _6, _5) -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
123123
+ _7 = const true; // bb1[7]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
124124
+ // ty::Const
125125
+ // + ty: bool
126126
+ // + val: Value(Scalar(0x01))
127127
+ // mir::Constant
128128
+ // + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
129129
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
130-
+ assert(const true, "index out of bounds: the len is move _6 but the index is _5") -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
130+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _6, _5) -> bb2; // bb1[8]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
131131
+ // ty::Const
132132
+ // + ty: bool
133133
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/repeat/32bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
// + span: $DIR/repeat.rs:6:18: 6:28
4141
// + literal: Const { ty: usize, val: Value(Scalar(0x00000008)) }
4242
- _6 = Lt(_4, _5); // bb0[7]: scope 0 at $DIR/repeat.rs:6:18: 6:28
43-
- assert(move _6, "index out of bounds: the len is move _5 but the index is _4") -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
43+
- assert(move _6, "index out of bounds: the len is {} but the index is {}", move _5, _4) -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
4444
+ _6 = const true; // bb0[7]: scope 0 at $DIR/repeat.rs:6:18: 6:28
4545
+ // ty::Const
4646
+ // + ty: bool
4747
+ // + val: Value(Scalar(0x01))
4848
+ // mir::Constant
4949
+ // + span: $DIR/repeat.rs:6:18: 6:28
5050
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
51-
+ assert(const true, "index out of bounds: the len is move _5 but the index is _4") -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
51+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _5, _4) -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
5252
+ // ty::Const
5353
+ // + ty: bool
5454
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/repeat/64bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
// + span: $DIR/repeat.rs:6:18: 6:28
4141
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000008)) }
4242
- _6 = Lt(_4, _5); // bb0[7]: scope 0 at $DIR/repeat.rs:6:18: 6:28
43-
- assert(move _6, "index out of bounds: the len is move _5 but the index is _4") -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
43+
- assert(move _6, "index out of bounds: the len is {} but the index is {}", move _5, _4) -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
4444
+ _6 = const true; // bb0[7]: scope 0 at $DIR/repeat.rs:6:18: 6:28
4545
+ // ty::Const
4646
+ // + ty: bool
4747
+ // + val: Value(Scalar(0x01))
4848
+ // mir::Constant
4949
+ // + span: $DIR/repeat.rs:6:18: 6:28
5050
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
51-
+ assert(const true, "index out of bounds: the len is move _5 but the index is _4") -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
51+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _5, _4) -> bb1; // bb0[8]: scope 0 at $DIR/repeat.rs:6:18: 6:28
5252
+ // ty::Const
5353
+ // + ty: bool
5454
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/slice_len/32bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// + literal: Const { ty: usize, val: Value(Scalar(0x00000001)) }
4040
- _7 = Len((*_2)); // bb0[11]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4141
- _8 = Lt(_6, _7); // bb0[12]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
42-
- assert(move _8, "index out of bounds: the len is move _7 but the index is _6") -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
42+
- assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4343
+ _7 = const 3usize; // bb0[11]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4444
+ // ty::Const
4545
+ // + ty: usize
@@ -54,7 +54,7 @@
5454
+ // mir::Constant
5555
+ // + span: $DIR/slice_len.rs:5:5: 5:33
5656
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
57-
+ assert(const true, "index out of bounds: the len is move _7 but the index is _6") -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
57+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
5858
+ // ty::Const
5959
+ // + ty: bool
6060
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/const_prop/slice_len/64bit/rustc.main.ConstProp.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000001)) }
4040
- _7 = Len((*_2)); // bb0[11]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4141
- _8 = Lt(_6, _7); // bb0[12]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
42-
- assert(move _8, "index out of bounds: the len is move _7 but the index is _6") -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
42+
- assert(move _8, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4343
+ _7 = const 3usize; // bb0[11]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
4444
+ // ty::Const
4545
+ // + ty: usize
@@ -54,7 +54,7 @@
5454
+ // mir::Constant
5555
+ // + span: $DIR/slice_len.rs:5:5: 5:33
5656
+ // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
57-
+ assert(const true, "index out of bounds: the len is move _7 but the index is _6") -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
57+
+ assert(const true, "index out of bounds: the len is {} but the index is {}", move _7, _6) -> bb1; // bb0[13]: scope 0 at $DIR/slice_len.rs:5:5: 5:33
5858
+ // ty::Const
5959
+ // + ty: bool
6060
+ // + val: Value(Scalar(0x01))

src/test/mir-opt/nll/region-subtyping-basic/32bit/rustc.main.nll.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> () {
7575
// + literal: Const { ty: usize, val: Value(Scalar(0x00000000)) }
7676
_4 = Len(_1); // bb0[6]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
7777
_5 = Lt(_3, _4); // bb0[7]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
78-
assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> [success: bb2, unwind: bb1]; // bb0[8]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
78+
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> [success: bb2, unwind: bb1]; // bb0[8]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
7979
}
8080

8181
bb1 (cleanup): {

src/test/mir-opt/nll/region-subtyping-basic/64bit/rustc.main.nll.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> () {
7575
// + literal: Const { ty: usize, val: Value(Scalar(0x0000000000000000)) }
7676
_4 = Len(_1); // bb0[6]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
7777
_5 = Lt(_3, _4); // bb0[7]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
78-
assert(move _5, "index out of bounds: the len is move _4 but the index is _3") -> [success: bb2, unwind: bb1]; // bb0[8]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
78+
assert(move _5, "index out of bounds: the len is {} but the index is {}", move _4, _3) -> [success: bb2, unwind: bb1]; // bb0[8]: scope 1 at $DIR/region-subtyping-basic.rs:16:14: 16:18
7979
}
8080

8181
bb1 (cleanup): {

0 commit comments

Comments
 (0)