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](https://github.com/rust-lang/chalk/issues/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 <wilcokusee@gmail.com>
This commit is contained in:
bors[bot] 2020-08-14 17:02:55 +00:00 committed by GitHub
commit c2594daf29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 14 deletions

View file

@ -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);
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<Interner>,

View file

@ -240,20 +240,23 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
Substs::empty().to_chalk(self.db)
}
fn trait_name(&self, _trait_id: chalk_ir::TraitId<Interner>) -> String {
unimplemented!()
fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> 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<Interner>) -> String {
unimplemented!()
// FIXME: lookup names
fn adt_name(&self, struct_id: chalk_ir::AdtId<Interner>) -> 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<Interner>) -> String {
unimplemented!()
fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
format!("Assoc_{}", assoc_ty_id.0)
}
fn opaque_type_name(&self, _opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
unimplemented!()
fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
format!("Opaque_{}", opaque_ty_id.0)
}
fn fn_def_name(&self, _fn_def_id: chalk_ir::FnDefId<Interner>) -> String {
unimplemented!()
fn fn_def_name(&self, fn_def_id: chalk_ir::FnDefId<Interner>) -> String {
format!("fn_{}", fn_def_id.0)
}
}