Skip to content

Commit baeb16e

Browse files
committed
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
1 parent dd8a75b commit baeb16e

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

crates/hir_ty/src/traits.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use base_db::CrateId;
55
use chalk_ir::cast::Cast;
66
use chalk_solve::{logging_db::LoggingRustIrDatabase, Solver};
77
use hir_def::{lang_item::LangItemTarget, TraitId};
8+
use stdx::panic_context;
89

910
use crate::{db::HirDatabase, DebruijnIndex, Substs};
1011

@@ -168,26 +169,41 @@ fn solve(
168169
};
169170

170171
let mut solve = || {
171-
if is_chalk_print() {
172-
let logging_db = LoggingRustIrDatabase::new(context);
173-
let solution = solver.solve_limited(&logging_db, goal, &should_continue);
174-
log::debug!("chalk program:\n{}", logging_db);
172+
let _ctx = if is_chalk_debug() || is_chalk_print() {
173+
Some(panic_context::enter(format!("solving {:?}", goal)))
174+
} else {
175+
None
176+
};
177+
let solution = if is_chalk_print() {
178+
let logging_db =
179+
LoggingRustIrDatabaseLoggingOnDrop(LoggingRustIrDatabase::new(context));
180+
let solution = solver.solve_limited(&logging_db.0, goal, &should_continue);
175181
solution
176182
} else {
177183
solver.solve_limited(&context, goal, &should_continue)
178-
}
184+
};
185+
186+
log::debug!("solve({:?}) => {:?}", goal, solution);
187+
188+
solution
179189
};
180190

181191
// don't set the TLS for Chalk unless Chalk debugging is active, to make
182192
// extra sure we only use it for debugging
183193
let solution =
184194
if is_chalk_debug() { chalk::tls::set_current_program(db, solve) } else { solve() };
185195

186-
log::debug!("solve({:?}) => {:?}", goal, solution);
187-
188196
solution
189197
}
190198

199+
struct LoggingRustIrDatabaseLoggingOnDrop<'a>(LoggingRustIrDatabase<Interner, ChalkContext<'a>>);
200+
201+
impl<'a> Drop for LoggingRustIrDatabaseLoggingOnDrop<'a> {
202+
fn drop(&mut self) {
203+
eprintln!("chalk program:\n{}", self.0);
204+
}
205+
}
206+
191207
fn is_chalk_debug() -> bool {
192208
std::env::var("CHALK_DEBUG").is_ok()
193209
}

0 commit comments

Comments
 (0)