From ab04526ede59373bd322688dc6c57e96a85e6ecf Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 15 Dec 2023 03:34:37 +0000 Subject: [PATCH] Annotate some more bugs --- compiler/rustc_ast_lowering/src/asm.rs | 4 ++-- compiler/rustc_ast_lowering/src/item.rs | 9 +++++++-- compiler/rustc_hir_analysis/src/check/wfcheck.rs | 9 --------- compiler/rustc_hir_typeck/src/cast.rs | 2 +- compiler/rustc_middle/src/ty/consts/int.rs | 4 ++-- compiler/rustc_middle/src/ty/util.rs | 7 ++----- compiler/rustc_mir_build/src/build/mod.rs | 8 ++++++-- compiler/rustc_mir_build/src/thir/cx/expr.rs | 2 +- .../rustc_mir_build/src/thir/pattern/const_to_pat.rs | 5 +++-- 9 files changed, 24 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/asm.rs b/compiler/rustc_ast_lowering/src/asm.rs index 7111daa2ea9ab..4c81983c24296 100644 --- a/compiler/rustc_ast_lowering/src/asm.rs +++ b/compiler/rustc_ast_lowering/src/asm.rs @@ -336,7 +336,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymFn { .. } | hir::InlineAsmOperand::SymStatic { .. } => { - unreachable!() + unreachable!("{op:?} is not a register operand"); } }; @@ -380,7 +380,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { { reg_sym.as_str() } else { - unreachable!(); + unreachable!("{op:?} is not a register operand"); } }; diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 80854c8a6c08b..e6df9398c0cc2 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -415,8 +415,13 @@ impl<'hir> LoweringContext<'_, 'hir> { } ItemKind::MacroDef(MacroDef { body, macro_rules }) => { let body = P(self.lower_delim_args(body)); - let DefKind::Macro(macro_kind) = self.tcx.def_kind(self.local_def_id(id)) else { - unreachable!() + let def_id = self.local_def_id(id); + let def_kind = self.tcx.def_kind(def_id); + let DefKind::Macro(macro_kind) = def_kind else { + unreachable!( + "expected DefKind::Macro for macro item, found {}", + def_kind.descr(def_id.to_def_id()) + ); }; let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules }); hir::ItemKind::Macro(macro_def, macro_kind) diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 5edfe080e84ff..9016195b8db0c 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -994,15 +994,6 @@ fn check_associated_item( }) } -fn item_adt_kind(kind: &ItemKind<'_>) -> Option { - match kind { - ItemKind::Struct(..) => Some(AdtKind::Struct), - ItemKind::Union(..) => Some(AdtKind::Union), - ItemKind::Enum(..) => Some(AdtKind::Enum), - _ => None, - } -} - /// In a type definition, we check that to ensure that the types of the fields are well-formed. fn check_type_defn<'tcx>( tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index e88dc4be97b2a..0de0365364c6a 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -149,7 +149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub enum CastError { ErrorGuaranteed(ErrorGuaranteed), diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 9d99344d5bdfb..310cf113b11b6 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -71,7 +71,7 @@ impl std::fmt::Debug for ConstInt { (4, _) => write!(fmt, "_i32")?, (8, _) => write!(fmt, "_i64")?, (16, _) => write!(fmt, "_i128")?, - _ => bug!(), + (sz, _) => bug!("unexpected int size i{sz}"), } } Ok(()) @@ -105,7 +105,7 @@ impl std::fmt::Debug for ConstInt { (4, _) => write!(fmt, "_u32")?, (8, _) => write!(fmt, "_u64")?, (16, _) => write!(fmt, "_u128")?, - _ => bug!(), + (sz, _) => bug!("unexpected unsigned int size u{sz}"), } } Ok(()) diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 70252a4dc67b9..fc5d11eb4709a 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -421,13 +421,10 @@ impl<'tcx> TyCtxt<'tcx> { let impl_args = match *self.type_of(impl_def_id).instantiate_identity().kind() { ty::Adt(def_, args) if def_ == def => args, - _ => bug!(), + _ => span_bug!(self.def_span(impl_def_id), "expected ADT for self type of `Drop` impl"), }; - let item_args = match *self.type_of(def.did()).instantiate_identity().kind() { - ty::Adt(def_, args) if def_ == def => args, - _ => bug!(), - }; + let item_args = ty::GenericArgs::identity_for_item(self, def.did()); let result = iter::zip(item_args, impl_args) .filter(|&(_, k)| { diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 19b6496b102ef..bd0b3d4640454 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -640,7 +640,9 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) - } DefKind::Closure if coroutine_kind.is_some() => { let coroutine_ty = tcx.type_of(def_id).instantiate_identity(); - let ty::Coroutine(_, args, _) = coroutine_ty.kind() else { bug!() }; + let ty::Coroutine(_, args, _) = coroutine_ty.kind() else { + bug!("expected type of coroutine-like closure to be a coroutine") + }; let args = args.as_coroutine(); let yield_ty = args.yield_ty(); let return_ty = args.return_ty(); @@ -648,7 +650,9 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) - } DefKind::Closure => { let closure_ty = tcx.type_of(def_id).instantiate_identity(); - let ty::Closure(_, args) = closure_ty.kind() else { bug!() }; + let ty::Closure(_, args) = closure_ty.kind() else { + bug!("expected type of closure to be a closure") + }; let args = args.as_closure(); let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), args.sig()); let self_ty = match args.kind() { diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 9759c72bf580d..c6d0e59a7d93a 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -798,7 +798,7 @@ impl<'tcx> Cx<'tcx> { hir::ExprKind::Tup(fields) => ExprKind::Tuple { fields: self.mirror_exprs(fields) }, hir::ExprKind::Yield(v, _) => ExprKind::Yield { value: self.mirror_expr(v) }, - hir::ExprKind::Err(_) => unreachable!(), + hir::ExprKind::Err(_) => unreachable!("cannot lower a `hir::ExprKind::Err` to THIR"), }; Expr { temp_lifetime, ty: expr_ty, span: expr.span, kind } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 4e2c932d6c6a3..a39c09700a993 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -492,8 +492,9 @@ impl<'tcx> ConstToPat<'tcx> { PatKind::Constant { value: mir::Const::Ty(ty::Const::new_value(tcx, cv, ty)) } } ty::FnPtr(..) => { - // Valtree construction would never succeed for these, so this is unreachable. - unreachable!() + unreachable!( + "Valtree construction would never succeed for FnPtr, so this is unreachable." + ) } _ => { let err = InvalidPattern { span, non_sm_ty: ty };