Skip to content

Commit 99516bb

Browse files
committed
minor: Avoid eprintln on panic
1 parent 71117e6 commit 99516bb

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,20 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
357357
infer: &infer,
358358
db,
359359
pattern_arena: &pattern_arena,
360-
eprint_panic_context: &|| {
360+
panic_context: &|| {
361361
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-
}
362+
let match_expr_text = source_map
363+
.expr_syntax(match_expr)
364+
.ok()
365+
.and_then(|scrutinee_sptr| {
366+
let root = scrutinee_sptr.file_syntax(db.upcast());
367+
scrutinee_sptr.value.to_node(&root).syntax().parent()
368+
})
369+
.map(|node| node.to_string());
370+
format!(
371+
"expression:\n{}",
372+
match_expr_text.as_deref().unwrap_or("<synthesized expr>")
373+
)
371374
},
372375
};
373376
let report = compute_match_usefulness(&cx, &m_arms);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +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(),
298+
pub(crate) panic_context: &'a dyn Fn() -> String,
299299
}
300300

301301
impl<'a> MatchCheckCtx<'a> {
@@ -331,8 +331,7 @@ impl<'a> MatchCheckCtx<'a> {
331331

332332
#[track_caller]
333333
pub(super) fn bug(&self, info: &str) -> ! {
334-
(self.eprint_panic_context)();
335-
panic!("bug: {}", info);
334+
panic!("bug: {}\n{}", info, (self.panic_context)());
336335
}
337336
}
338337

0 commit comments

Comments
 (0)