From e16f413582ea45d6b318337ea2c7838fdc3b82a4 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Wed, 19 May 2021 18:00:25 +0500 Subject: eprint panic context --- crates/hir_ty/src/diagnostics/expr.rs | 12 +++++++++ .../src/diagnostics/match_check/deconstruct_pat.rs | 29 ++++++++++++---------- .../src/diagnostics/match_check/usefulness.rs | 9 ++++++- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 0a7e6ee52..44d5f6b22 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -357,6 +357,18 @@ impl<'a, 'b> ExprValidator<'a, 'b> { infer: &infer, db, pattern_arena: &pattern_arena, + eprint_panic_context: &|| { + use syntax::AstNode; + if let Ok(scrutinee_sptr) = source_map.expr_syntax(match_expr) { + let root = scrutinee_sptr.file_syntax(db.upcast()); + if let Some(match_ast) = scrutinee_sptr.value.to_node(&root).syntax().parent() { + eprintln!( + "Match checking is about to panic on this expression:\n{}", + match_ast.to_string(), + ); + } + } + }, }; let report = compute_match_usefulness(&cx, &m_arms); diff --git a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs index 6711fbb4a..15ec5cf45 100644 --- a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs +++ b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs @@ -328,7 +328,7 @@ impl Constructor { PatKind::Leaf { .. } | PatKind::Deref { .. } => Single, &PatKind::Variant { enum_variant, .. } => Variant(enum_variant), &PatKind::LiteralBool { value } => IntRange(IntRange::from_bool(value)), - PatKind::Or { .. } => panic!("bug: Or-pattern should have been expanded earlier on."), + PatKind::Or { .. } => cx.bug("Or-pattern should have been expanded earlier on."), } } @@ -375,7 +375,7 @@ impl Constructor { /// this checks for inclusion. // We inline because this has a single call site in `Matrix::specialize_constructor`. #[inline] - pub(super) fn is_covered_by(&self, _pcx: PatCtxt<'_>, other: &Self) -> bool { + pub(super) fn is_covered_by(&self, pcx: PatCtxt<'_>, other: &Self) -> bool { // This must be kept in sync with `is_covered_by_any`. match (self, other) { // Wildcards cover anything @@ -400,17 +400,17 @@ impl Constructor { // Only a wildcard pattern can match the special extra constructor. (NonExhaustive, _) => false, - _ => panic!( - "bug: trying to compare incompatible constructors {:?} and {:?}", + _ => pcx.cx.bug(&format!( + "trying to compare incompatible constructors {:?} and {:?}", self, other - ), + )), } } /// Faster version of `is_covered_by` when applied to many constructors. `used_ctors` is /// assumed to be built from `matrix.head_ctors()` with wildcards filtered out, and `self` is /// assumed to have been split from a wildcard. - fn is_covered_by_any(&self, _pcx: PatCtxt<'_>, used_ctors: &[Constructor]) -> bool { + fn is_covered_by_any(&self, pcx: PatCtxt<'_>, used_ctors: &[Constructor]) -> bool { if used_ctors.is_empty() { return false; } @@ -431,7 +431,7 @@ impl Constructor { // This constructor is never covered by anything else NonExhaustive => false, Str(..) | FloatRange(..) | Opaque | Missing | Wildcard => { - panic!("bug: found unexpected ctor in all_ctors: {:?}", self) + pcx.cx.bug(&format!("found unexpected ctor in all_ctors: {:?}", self)) } } } @@ -683,7 +683,9 @@ impl Fields { } } } - _ => panic!("Unexpected type for `Single` constructor: {:?}", ty), + ty_kind => { + cx.bug(&format!("Unexpected type for `Single` constructor: {:?}", ty_kind)) + } }, Slice(..) => { unimplemented!() @@ -748,7 +750,7 @@ impl Fields { // can ignore this issue. TyKind::Ref(..) => PatKind::Deref { subpattern: subpatterns.next().unwrap() }, TyKind::Slice(..) | TyKind::Array(..) => { - panic!("bug: bad slice pattern {:?} {:?}", ctor, pcx.ty) + pcx.cx.bug(&format!("bad slice pattern {:?} {:?}", ctor, pcx.ty)) } _ => PatKind::Wild, }, @@ -758,10 +760,11 @@ impl Fields { Constructor::IntRange(_) => UNHANDLED, NonExhaustive => PatKind::Wild, Wildcard => return Pat::wildcard_from_ty(pcx.ty), - Opaque => panic!("bug: we should not try to apply an opaque constructor"), - Missing => { - panic!("bug: trying to apply the `Missing` constructor; this should have been done in `apply_constructors`") - } + Opaque => pcx.cx.bug("we should not try to apply an opaque constructor"), + Missing => pcx.cx.bug( + "trying to apply the `Missing` constructor;\ + this should have been done in `apply_constructors`", + ), }; Pat { ty: pcx.ty.clone(), kind: Box::new(pat) } diff --git a/crates/hir_ty/src/diagnostics/match_check/usefulness.rs b/crates/hir_ty/src/diagnostics/match_check/usefulness.rs index 44e08b6e9..cb322a3de 100644 --- a/crates/hir_ty/src/diagnostics/match_check/usefulness.rs +++ b/crates/hir_ty/src/diagnostics/match_check/usefulness.rs @@ -295,6 +295,7 @@ pub(crate) struct MatchCheckCtx<'a> { pub(crate) db: &'a dyn HirDatabase, /// Lowered patterns from arms plus generated by the check. pub(crate) pattern_arena: &'a RefCell, + pub(crate) eprint_panic_context: &'a dyn Fn(), } impl<'a> MatchCheckCtx<'a> { @@ -327,6 +328,12 @@ impl<'a> MatchCheckCtx<'a> { pub(super) fn type_of(&self, pat: PatId) -> Ty { self.pattern_arena.borrow()[pat].ty.clone() } + + #[track_caller] + pub(super) fn bug(&self, info: &str) -> ! { + (self.eprint_panic_context)(); + panic!("bug: {}", info); + } } #[derive(Copy, Clone)] @@ -737,7 +744,7 @@ impl SubPatSet { } Seq { subpats: new_subpats } } - Alt { .. } => panic!("bug"), + Alt { .. } => panic!("bug"), // `self` is a patstack } } -- cgit v1.2.3