From 99516bbd67a0018c3c0bf94cb895896857d4263c Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Tue, 1 Jun 2021 17:21:00 +0500 Subject: minor: Avoid eprintln on panic --- crates/hir_ty/src/diagnostics/expr.rs | 23 ++++++++++++---------- .../src/diagnostics/match_check/usefulness.rs | 5 ++--- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 3efbce773..a2a4d61db 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -357,17 +357,20 @@ impl<'a, 'b> ExprValidator<'a, 'b> { infer: &infer, db, pattern_arena: &pattern_arena, - eprint_panic_context: &|| { + 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 match_expr_text = source_map + .expr_syntax(match_expr) + .ok() + .and_then(|scrutinee_sptr| { + let root = scrutinee_sptr.file_syntax(db.upcast()); + scrutinee_sptr.value.to_node(&root).syntax().parent() + }) + .map(|node| node.to_string()); + format!( + "expression:\n{}", + match_expr_text.as_deref().unwrap_or("") + ) }, }; let report = compute_match_usefulness(&cx, &m_arms); diff --git a/crates/hir_ty/src/diagnostics/match_check/usefulness.rs b/crates/hir_ty/src/diagnostics/match_check/usefulness.rs index 83b094a89..bd76a606c 100644 --- a/crates/hir_ty/src/diagnostics/match_check/usefulness.rs +++ b/crates/hir_ty/src/diagnostics/match_check/usefulness.rs @@ -295,7 +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(), + pub(crate) panic_context: &'a dyn Fn() -> String, } impl<'a> MatchCheckCtx<'a> { @@ -331,8 +331,7 @@ impl<'a> MatchCheckCtx<'a> { #[track_caller] pub(super) fn bug(&self, info: &str) -> ! { - (self.eprint_panic_context)(); - panic!("bug: {}", info); + panic!("bug: {}\n{}", info, (self.panic_context)()); } } -- cgit v1.2.3