Skip to content

Commit e16f413

Browse files
committed
eprint panic context
1 parent f571b62 commit e16f413

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,18 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
357357
infer: &infer,
358358
db,
359359
pattern_arena: &pattern_arena,
360+
eprint_panic_context: &|| {
361+
use syntax::AstNode;
362+
if let Ok(scrutinee_sptr) = source_map.expr_syntax(match_expr) {
363+
let root = scrutinee_sptr.file_syntax(db.upcast());
364+
if let Some(match_ast) = scrutinee_sptr.value.to_node(&root).syntax().parent() {
365+
eprintln!(
366+
"Match checking is about to panic on this expression:\n{}",
367+
match_ast.to_string(),
368+
);
369+
}
370+
}
371+
},
360372
};
361373
let report = compute_match_usefulness(&cx, &m_arms);
362374

crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl Constructor {
328328
PatKind::Leaf { .. } | PatKind::Deref { .. } => Single,
329329
&PatKind::Variant { enum_variant, .. } => Variant(enum_variant),
330330
&PatKind::LiteralBool { value } => IntRange(IntRange::from_bool(value)),
331-
PatKind::Or { .. } => panic!("bug: Or-pattern should have been expanded earlier on."),
331+
PatKind::Or { .. } => cx.bug("Or-pattern should have been expanded earlier on."),
332332
}
333333
}
334334

@@ -375,7 +375,7 @@ impl Constructor {
375375
/// this checks for inclusion.
376376
// We inline because this has a single call site in `Matrix::specialize_constructor`.
377377
#[inline]
378-
pub(super) fn is_covered_by(&self, _pcx: PatCtxt<'_>, other: &Self) -> bool {
378+
pub(super) fn is_covered_by(&self, pcx: PatCtxt<'_>, other: &Self) -> bool {
379379
// This must be kept in sync with `is_covered_by_any`.
380380
match (self, other) {
381381
// Wildcards cover anything
@@ -400,17 +400,17 @@ impl Constructor {
400400
// Only a wildcard pattern can match the special extra constructor.
401401
(NonExhaustive, _) => false,
402402

403-
_ => panic!(
404-
"bug: trying to compare incompatible constructors {:?} and {:?}",
403+
_ => pcx.cx.bug(&format!(
404+
"trying to compare incompatible constructors {:?} and {:?}",
405405
self, other
406-
),
406+
)),
407407
}
408408
}
409409

410410
/// Faster version of `is_covered_by` when applied to many constructors. `used_ctors` is
411411
/// assumed to be built from `matrix.head_ctors()` with wildcards filtered out, and `self` is
412412
/// assumed to have been split from a wildcard.
413-
fn is_covered_by_any(&self, _pcx: PatCtxt<'_>, used_ctors: &[Constructor]) -> bool {
413+
fn is_covered_by_any(&self, pcx: PatCtxt<'_>, used_ctors: &[Constructor]) -> bool {
414414
if used_ctors.is_empty() {
415415
return false;
416416
}
@@ -431,7 +431,7 @@ impl Constructor {
431431
// This constructor is never covered by anything else
432432
NonExhaustive => false,
433433
Str(..) | FloatRange(..) | Opaque | Missing | Wildcard => {
434-
panic!("bug: found unexpected ctor in all_ctors: {:?}", self)
434+
pcx.cx.bug(&format!("found unexpected ctor in all_ctors: {:?}", self))
435435
}
436436
}
437437
}
@@ -683,7 +683,9 @@ impl Fields {
683683
}
684684
}
685685
}
686-
_ => panic!("Unexpected type for `Single` constructor: {:?}", ty),
686+
ty_kind => {
687+
cx.bug(&format!("Unexpected type for `Single` constructor: {:?}", ty_kind))
688+
}
687689
},
688690
Slice(..) => {
689691
unimplemented!()
@@ -748,7 +750,7 @@ impl Fields {
748750
// can ignore this issue.
749751
TyKind::Ref(..) => PatKind::Deref { subpattern: subpatterns.next().unwrap() },
750752
TyKind::Slice(..) | TyKind::Array(..) => {
751-
panic!("bug: bad slice pattern {:?} {:?}", ctor, pcx.ty)
753+
pcx.cx.bug(&format!("bad slice pattern {:?} {:?}", ctor, pcx.ty))
752754
}
753755
_ => PatKind::Wild,
754756
},
@@ -758,10 +760,11 @@ impl Fields {
758760
Constructor::IntRange(_) => UNHANDLED,
759761
NonExhaustive => PatKind::Wild,
760762
Wildcard => return Pat::wildcard_from_ty(pcx.ty),
761-
Opaque => panic!("bug: we should not try to apply an opaque constructor"),
762-
Missing => {
763-
panic!("bug: trying to apply the `Missing` constructor; this should have been done in `apply_constructors`")
764-
}
763+
Opaque => pcx.cx.bug("we should not try to apply an opaque constructor"),
764+
Missing => pcx.cx.bug(
765+
"trying to apply the `Missing` constructor;\
766+
this should have been done in `apply_constructors`",
767+
),
765768
};
766769

767770
Pat { ty: pcx.ty.clone(), kind: Box::new(pat) }

crates/hir_ty/src/diagnostics/match_check/usefulness.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ pub(crate) struct MatchCheckCtx<'a> {
295295
pub(crate) db: &'a dyn HirDatabase,
296296
/// Lowered patterns from arms plus generated by the check.
297297
pub(crate) pattern_arena: &'a RefCell<PatternArena>,
298+
pub(crate) eprint_panic_context: &'a dyn Fn(),
298299
}
299300

300301
impl<'a> MatchCheckCtx<'a> {
@@ -327,6 +328,12 @@ impl<'a> MatchCheckCtx<'a> {
327328
pub(super) fn type_of(&self, pat: PatId) -> Ty {
328329
self.pattern_arena.borrow()[pat].ty.clone()
329330
}
331+
332+
#[track_caller]
333+
pub(super) fn bug(&self, info: &str) -> ! {
334+
(self.eprint_panic_context)();
335+
panic!("bug: {}", info);
336+
}
330337
}
331338

332339
#[derive(Copy, Clone)]
@@ -737,7 +744,7 @@ impl SubPatSet {
737744
}
738745
Seq { subpats: new_subpats }
739746
}
740-
Alt { .. } => panic!("bug"),
747+
Alt { .. } => panic!("bug"), // `self` is a patstack
741748
}
742749
}
743750

0 commit comments

Comments
 (0)