From baeb16e83f7b2beee8211e2c3ad5e4a7dc3a5434 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 23 Oct 2020 15:25:22 +0200 Subject: Improve Chalk debugging - add panic context for the trait goal if CHALK_DEBUG is set - print the Chalk program even if we're panicking - log goal/solution while TLS is still set --- crates/hir_ty/src/traits.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 14cd3a2b4..ce1174cbe 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -5,6 +5,7 @@ use base_db::CrateId; use chalk_ir::cast::Cast; use chalk_solve::{logging_db::LoggingRustIrDatabase, Solver}; use hir_def::{lang_item::LangItemTarget, TraitId}; +use stdx::panic_context; use crate::{db::HirDatabase, DebruijnIndex, Substs}; @@ -168,14 +169,23 @@ fn solve( }; let mut solve = || { - if is_chalk_print() { - let logging_db = LoggingRustIrDatabase::new(context); - let solution = solver.solve_limited(&logging_db, goal, &should_continue); - log::debug!("chalk program:\n{}", logging_db); + let _ctx = if is_chalk_debug() || is_chalk_print() { + Some(panic_context::enter(format!("solving {:?}", goal))) + } else { + None + }; + let solution = if is_chalk_print() { + let logging_db = + LoggingRustIrDatabaseLoggingOnDrop(LoggingRustIrDatabase::new(context)); + let solution = solver.solve_limited(&logging_db.0, goal, &should_continue); solution } else { solver.solve_limited(&context, goal, &should_continue) - } + }; + + log::debug!("solve({:?}) => {:?}", goal, solution); + + solution }; // don't set the TLS for Chalk unless Chalk debugging is active, to make @@ -183,11 +193,17 @@ fn solve( let solution = if is_chalk_debug() { chalk::tls::set_current_program(db, solve) } else { solve() }; - log::debug!("solve({:?}) => {:?}", goal, solution); - solution } +struct LoggingRustIrDatabaseLoggingOnDrop<'a>(LoggingRustIrDatabase>); + +impl<'a> Drop for LoggingRustIrDatabaseLoggingOnDrop<'a> { + fn drop(&mut self) { + eprintln!("chalk program:\n{}", self.0); + } +} + fn is_chalk_debug() -> bool { std::env::var("CHALK_DEBUG").is_ok() } -- cgit v1.2.3