Use assert_matches around the compiler

This commit is contained in:
Michael Goulet 2024-08-11 12:10:36 -04:00
parent 68d2e8a66e
commit c361c924a0
39 changed files with 100 additions and 49 deletions

View file

@ -1,3 +1,5 @@
use std::assert_matches::debug_assert_matches;
use rustc_ast::InlineAsmTemplatePiece;
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir::{self as hir, LangItem};
@ -457,17 +459,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}
// Typeck has checked that Const operands are integers.
hir::InlineAsmOperand::Const { anon_const } => {
debug_assert!(matches!(
debug_assert_matches!(
self.tcx.type_of(anon_const.def_id).instantiate_identity().kind(),
ty::Error(_) | ty::Int(_) | ty::Uint(_)
));
);
}
// Typeck has checked that SymFn refers to a function.
hir::InlineAsmOperand::SymFn { anon_const } => {
debug_assert!(matches!(
debug_assert_matches!(
self.tcx.type_of(anon_const.def_id).instantiate_identity().kind(),
ty::Error(_) | ty::FnDef(..)
));
);
}
// AST lowering guarantees that SymStatic points to a static.
hir::InlineAsmOperand::SymStatic { .. } => {}

View file

@ -1,6 +1,7 @@
//! Check properties that are required by built-in traits and set
//! up data structures required by type-checking/codegen.
use std::assert_matches::assert_matches;
use std::collections::BTreeMap;
use rustc_data_structures::fx::FxHashSet;
@ -129,7 +130,7 @@ fn visit_implementation_of_const_param_ty(
checker: &Checker<'_>,
kind: LangItem,
) -> Result<(), ErrorGuaranteed> {
assert!(matches!(kind, LangItem::ConstParamTy | LangItem::UnsizedConstParamTy));
assert_matches!(kind, LangItem::ConstParamTy | LangItem::UnsizedConstParamTy);
let tcx = checker.tcx;
let header = checker.impl_header;

View file

@ -1,3 +1,4 @@
use std::assert_matches::assert_matches;
use std::ops::ControlFlow;
use hir::intravisit::{self, Visitor};
@ -207,9 +208,9 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
..
}) => {
if in_trait {
assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn))
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn);
} else {
assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn))
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);
}
Some(fn_def_id.to_def_id())
}
@ -218,9 +219,9 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
..
}) => {
if in_assoc_ty {
assert!(matches!(tcx.def_kind(parent), DefKind::AssocTy));
assert_matches!(tcx.def_kind(parent), DefKind::AssocTy);
} else {
assert!(matches!(tcx.def_kind(parent), DefKind::TyAlias));
assert_matches!(tcx.def_kind(parent), DefKind::TyAlias);
}
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
// Opaque types are always nested within another item, and

View file

@ -1,3 +1,5 @@
use std::assert_matches::assert_matches;
use hir::{HirId, Node};
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir;
@ -601,7 +603,7 @@ pub(super) fn implied_predicates_with_filter(
let Some(trait_def_id) = trait_def_id.as_local() else {
// if `assoc_name` is None, then the query should've been redirected to an
// external provider
assert!(matches!(filter, PredicateFilter::SelfThatDefines(_)));
assert_matches!(filter, PredicateFilter::SelfThatDefines(_));
return tcx.explicit_super_predicates_of(trait_def_id);
};

View file

@ -1,3 +1,5 @@
use std::assert_matches::debug_assert_matches;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
@ -63,7 +65,7 @@ enum FnKind {
}
fn fn_kind<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> FnKind {
debug_assert!(matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn));
debug_assert_matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn);
let parent = tcx.parent(def_id);
match tcx.def_kind(parent) {

View file

@ -8,6 +8,8 @@
//! specialization errors. These things can (and probably should) be
//! fixed, but for the moment it's easier to do these checks early.
use std::assert_matches::debug_assert_matches;
use min_specialization::check_min_specialization;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::codes::*;
@ -54,7 +56,7 @@ mod min_specialization;
pub fn check_impl_wf(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
let min_specialization = tcx.features().min_specialization;
let mut res = Ok(());
debug_assert!(matches!(tcx.def_kind(impl_def_id), DefKind::Impl { .. }));
debug_assert_matches!(tcx.def_kind(impl_def_id), DefKind::Impl { .. });
res = res.and(enforce_impl_params_are_constrained(tcx, impl_def_id));
if min_specialization {
res = res.and(check_min_specialization(tcx, impl_def_id));

View file

@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
#![allow(rustc::untranslatable_diagnostic)]
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
#![feature(assert_matches)]
#![feature(control_flow_enum)]
#![feature(if_let_guard)]
#![feature(iter_intersperse)]