diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 2553237170..1c3abb18f0 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use base_db::CrateId; use chalk_ir::cast::Cast; -use chalk_solve::Solver; +use chalk_solve::{logging_db::LoggingRustIrDatabase, Solver}; use hir_def::{lang_item::LangItemTarget, TraitId}; use crate::{db::HirDatabase, DebruijnIndex, Substs}; @@ -166,16 +166,25 @@ fn solve( } remaining > 0 }; + let mut solve = || { - let solution = solver.solve_limited(&context, goal, should_continue); - log::debug!("solve({:?}) => {:?}", goal, solution); - solution + 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); + solution + } else { + solver.solve_limited(&context, goal, should_continue) + } }; + // don't set the TLS for Chalk unless Chalk debugging is active, to make // extra sure we only use it for debugging let solution = if is_chalk_debug() { chalk::tls::set_current_program(db, solve) } else { solve() }; + log::debug!("solve({:?}) => {:?}", goal, solution); + solution } @@ -183,6 +192,10 @@ fn is_chalk_debug() -> bool { std::env::var("CHALK_DEBUG").is_ok() } +fn is_chalk_print() -> bool { + std::env::var("CHALK_PRINT").is_ok() +} + fn solution_from_chalk( db: &dyn HirDatabase, solution: chalk_solve::Solution, diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index b336534179..17c83b6a46 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -240,20 +240,23 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { Substs::empty().to_chalk(self.db) } - fn trait_name(&self, _trait_id: chalk_ir::TraitId) -> String { - unimplemented!() + fn trait_name(&self, trait_id: chalk_ir::TraitId) -> String { + let id = from_chalk(self.db, trait_id); + self.db.trait_data(id).name.to_string() } - fn adt_name(&self, _struct_id: chalk_ir::AdtId) -> String { - unimplemented!() + // FIXME: lookup names + fn adt_name(&self, struct_id: chalk_ir::AdtId) -> String { + let datum = self.db.struct_datum(self.krate, struct_id); + format!("{:?}", datum.name(&Interner)) } - fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId) -> String { - unimplemented!() + fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId) -> String { + format!("Assoc_{}", assoc_ty_id.0) } - fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId) -> String { - unimplemented!() + fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId) -> String { + format!("Opaque_{}", opaque_ty_id.0) } - fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId) -> String { - unimplemented!() + fn fn_def_name(&self, fn_def_id: chalk_ir::FnDefId) -> String { + format!("fn_{}", fn_def_id.0) } }