Skip to content

Commit c2594da

Browse files
bors[bot]detrumi
andauthored
Merge #5347
5347: Chalk writer integration r=flodiebold a=detrumi ~~This adds a `rust-analyzer dump-chalk` command, similar to analysis-stats, which writes out the whole chalk progam (see [chalk#365](rust-lang/chalk#365) for more info about the .chalk writer)~~ Write out chalk programs in debug output if chalk debugging is active (using `CHALK_DEBUG`). Example output: ``` [DEBUG ra_hir_ty::traits] solve(UCanonical { canonical: Canonical { value: InEnvironment { environment: Env([]), goal: Implemented(SeparatorTraitRef(?)) }, binders: [] }, universes: 1 }) => None [INFO ra_hir_ty::traits] trait_solve_query(Implements(fn min<?0.0>(?0.0, ?0.0) -> ?0.0: Deref)) [DEBUG ra_hir_ty::traits] solve goal: UCanonical { canonical: Canonical { value: InEnvironment { environment: Env([]), goal: Implemented(SeparatorTraitRef(?)) }, binders: [U0 with kind type] }, universes: 1 } [DEBUG ra_hir_ty::traits::chalk] impls_for_trait Deref [DEBUG ra_hir_ty::traits::chalk] impls_for_trait returned 0 impls [DEBUG ra_hir_ty::traits::chalk] trait_datum Ord [DEBUG ra_hir_ty::traits::chalk] trait Ord = Name(Text("Ord")) [DEBUG ra_hir_ty::traits] chalk program: #[upstream] #[non_enumerable] #[object_safe] trait Ord {} #[upstream] #[non_enumerable] #[object_safe] #[lang(sized)] trait Sized {} fn fn_0<_1_0>(arg_0: _1_0, arg_1: _1_0) -> _1_0 where _1_0: Ord; #[upstream] #[non_enumerable] #[object_safe] trait Deref { type Assoc_1829: Sized; } [DEBUG ra_hir_ty::traits] solve(UCanonical { canonical: Canonical { value: InEnvironment { environment: Env([]), goal: Implemented(SeparatorTraitRef(?)) }, binders: [U0 with kind type] }, universes: 1 }) => None [INFO ra_hir_ty::traits] trait_solve_query(Implements(?0.0: Ord)) ``` Co-authored-by: Wilco Kusee <[email protected]>
2 parents f7abd16 + de282dd commit c2594da

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

crates/hir_ty/src/traits.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use base_db::CrateId;
55
use chalk_ir::cast::Cast;
6-
use chalk_solve::Solver;
6+
use chalk_solve::{logging_db::LoggingRustIrDatabase, Solver};
77
use hir_def::{lang_item::LangItemTarget, TraitId};
88

99
use crate::{db::HirDatabase, DebruijnIndex, Substs};
@@ -166,23 +166,36 @@ fn solve(
166166
}
167167
remaining > 0
168168
};
169+
169170
let mut solve = || {
170-
let solution = solver.solve_limited(&context, goal, should_continue);
171-
log::debug!("solve({:?}) => {:?}", goal, solution);
172-
solution
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);
175+
solution
176+
} else {
177+
solver.solve_limited(&context, goal, should_continue)
178+
}
173179
};
180+
174181
// don't set the TLS for Chalk unless Chalk debugging is active, to make
175182
// extra sure we only use it for debugging
176183
let solution =
177184
if is_chalk_debug() { chalk::tls::set_current_program(db, solve) } else { solve() };
178185

186+
log::debug!("solve({:?}) => {:?}", goal, solution);
187+
179188
solution
180189
}
181190

182191
fn is_chalk_debug() -> bool {
183192
std::env::var("CHALK_DEBUG").is_ok()
184193
}
185194

195+
fn is_chalk_print() -> bool {
196+
std::env::var("CHALK_PRINT").is_ok()
197+
}
198+
186199
fn solution_from_chalk(
187200
db: &dyn HirDatabase,
188201
solution: chalk_solve::Solution<Interner>,

crates/hir_ty/src/traits/chalk.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,23 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
240240
Substs::empty().to_chalk(self.db)
241241
}
242242

243-
fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String {
244-
unimplemented!()
243+
fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String {
244+
let id = from_chalk(self.db, trait_id);
245+
self.db.trait_data(id).name.to_string()
245246
}
246-
fn adt_name(&self, _struct_id: chalk_ir::AdtId<Interner>) -> String {
247-
unimplemented!()
247+
// FIXME: lookup names
248+
fn adt_name(&self, struct_id: chalk_ir::AdtId<Interner>) -> String {
249+
let datum = self.db.struct_datum(self.krate, struct_id);
250+
format!("{:?}", datum.name(&Interner))
248251
}
249-
fn assoc_type_name(&self, _assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
250-
unimplemented!()
252+
fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
253+
format!("Assoc_{}", assoc_ty_id.0)
251254
}
252-
fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
253-
unimplemented!()
255+
fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
256+
format!("Opaque_{}", opaque_ty_id.0)
254257
}
255-
fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId<Interner>) -> String {
256-
unimplemented!()
258+
fn fn_def_name(&self, fn_def_id: chalk_ir::FnDefId<Interner>) -> String {
259+
format!("fn_{}", fn_def_id.0)
257260
}
258261
}
259262

0 commit comments

Comments
 (0)