Skip to content

Commit 7c6dca9

Browse files
Rollup merge of #128978 - compiler-errors:assert-matches, r=jieyouxu
Use `assert_matches` around the compiler more It's a useful assertion, especially since it actually prints out the LHS.
2 parents bb35b88 + c361c92 commit 7c6dca9

File tree

39 files changed

+100
-49
lines changed

39 files changed

+100
-49
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![allow(rustc::diagnostic_outside_of_impl)]
44
#![allow(rustc::untranslatable_diagnostic)]
55

6+
use std::assert_matches::assert_matches;
7+
68
use rustc_errors::{Applicability, Diag};
79
use rustc_hir as hir;
810
use rustc_hir::intravisit::Visitor;
@@ -116,7 +118,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
116118
// path_span must be `Some` as otherwise the if condition is true
117119
let path_span = path_span.unwrap();
118120
// path_span is only present in the case of closure capture
119-
assert!(matches!(later_use_kind, LaterUseKind::ClosureCapture));
121+
assert_matches!(later_use_kind, LaterUseKind::ClosureCapture);
120122
if !borrow_span.is_some_and(|sp| sp.overlaps(var_or_use_span)) {
121123
let path_label = "used here by closure";
122124
let capture_kind_label = message;
@@ -147,7 +149,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
147149
// path_span must be `Some` as otherwise the if condition is true
148150
let path_span = path_span.unwrap();
149151
// path_span is only present in the case of closure capture
150-
assert!(matches!(later_use_kind, LaterUseKind::ClosureCapture));
152+
assert_matches!(later_use_kind, LaterUseKind::ClosureCapture);
151153
if borrow_span.map(|sp| !sp.overlaps(var_or_use_span)).unwrap_or(true) {
152154
let path_label = "used here by closure";
153155
let capture_kind_label = message;

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::assert_matches::assert_matches;
2+
13
use libc::{c_char, c_uint};
24
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
35
use rustc_codegen_ssa::mir::operand::OperandValue;
@@ -89,7 +91,7 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
8991
// if the target feature needed by the register class is
9092
// disabled. This is necessary otherwise LLVM will try
9193
// to actually allocate a register for the dummy output.
92-
assert!(matches!(reg, InlineAsmRegOrRegClass::Reg(_)));
94+
assert_matches!(reg, InlineAsmRegOrRegClass::Reg(_));
9395
clobbers.push(format!("~{}", reg_to_llvm(reg, None)));
9496
continue;
9597
} else {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::cmp::Ordering;
23

34
use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh, wants_wasm_eh};
@@ -1142,7 +1143,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
11421143
if cfg!(debug_assertions) {
11431144
for (ty, arg) in arg_tys.iter().zip(args) {
11441145
if ty.is_simd() {
1145-
assert!(matches!(arg.val, OperandValue::Immediate(_)));
1146+
assert_matches!(arg.val, OperandValue::Immediate(_));
11461147
}
11471148
}
11481149
}

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![allow(internal_features)]
99
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1010
#![doc(rust_logo)]
11+
#![feature(assert_matches)]
1112
#![feature(exact_size_is_empty)]
1213
#![feature(extern_types)]
1314
#![feature(hash_raw_entry)]

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::any::Any;
2+
use std::assert_matches::assert_matches;
23
use std::marker::PhantomData;
34
use std::path::{Path, PathBuf};
45
use std::sync::mpsc::{channel, Receiver, Sender};
@@ -1963,7 +1964,7 @@ impl SharedEmitterMain {
19631964
sess.dcx().abort_if_errors();
19641965
}
19651966
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
1966-
assert!(matches!(level, Level::Error | Level::Warning | Level::Note));
1967+
assert_matches!(level, Level::Error | Level::Warning | Level::Note);
19671968
let msg = msg.strip_prefix("error: ").unwrap_or(&msg).to_string();
19681969
let mut err = Diag::<()>::new(sess.dcx(), level, msg);
19691970

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(rustc::untranslatable_diagnostic)]
55
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
66
#![doc(rust_logo)]
7+
#![feature(assert_matches)]
78
#![feature(box_patterns)]
89
#![feature(if_let_guard)]
910
#![feature(let_chains)]

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::fmt;
23

34
use arrayvec::ArrayVec;
@@ -389,7 +390,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
389390
}
390391
// Newtype vector of array, e.g. #[repr(simd)] struct S([i32; 4]);
391392
(OperandValue::Immediate(llval), Abi::Aggregate { sized: true }) => {
392-
assert!(matches!(self.layout.abi, Abi::Vector { .. }));
393+
assert_matches!(self.layout.abi, Abi::Vector { .. });
393394

394395
let llfield_ty = bx.cx().backend_type(field);
395396

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::assert_matches::assert_matches;
2+
13
use arrayvec::ArrayVec;
24
use rustc_middle::ty::adjustment::PointerCoercion;
35
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
@@ -220,7 +222,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
220222
match operand.val {
221223
OperandValue::Ref(source_place_val) => {
222224
assert_eq!(source_place_val.llextra, None);
223-
assert!(matches!(operand_kind, OperandValueKind::Ref));
225+
assert_matches!(operand_kind, OperandValueKind::Ref);
224226
Some(bx.load_operand(source_place_val.with_type(cast)).val)
225227
}
226228
OperandValue::ZeroSized => {

compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::assert_matches::assert_matches;
2+
13
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
24
use rustc_middle::ty::layout::{HasParamEnv, TyAndLayout};
35
use rustc_middle::ty::{Instance, Ty};
@@ -254,10 +256,10 @@ pub trait BuilderMethods<'a, 'tcx>:
254256
} else {
255257
(in_ty, dest_ty)
256258
};
257-
assert!(matches!(
259+
assert_matches!(
258260
self.cx().type_kind(float_ty),
259261
TypeKind::Half | TypeKind::Float | TypeKind::Double | TypeKind::FP128
260-
));
262+
);
261263
assert_eq!(self.cx().type_kind(int_ty), TypeKind::Integer);
262264

263265
if let Some(false) = self.cx().sess().opts.unstable_opts.saturating_float_casts {

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3+
use std::assert_matches::assert_matches;
34
use std::mem;
45
use std::ops::Deref;
56

@@ -590,7 +591,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
590591
if is_int_bool_or_char(lhs_ty) && is_int_bool_or_char(rhs_ty) {
591592
// Int, bool, and char operations are fine.
592593
} else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() {
593-
assert!(matches!(
594+
assert_matches!(
594595
op,
595596
BinOp::Eq
596597
| BinOp::Ne
@@ -599,7 +600,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
599600
| BinOp::Ge
600601
| BinOp::Gt
601602
| BinOp::Offset
602-
));
603+
);
603604

604605
self.check_op(ops::RawPtrComparison);
605606
} else if lhs_ty.is_floating_point() || rhs_ty.is_floating_point() {

0 commit comments

Comments
 (0)