2927: Publicize debug printing of CrateDefMap r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-01-28 15:30:19 +00:00 committed by GitHub
commit 3f70236965
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 41 deletions

View file

@ -229,6 +229,46 @@ impl CrateDefMap {
self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow);
(res.resolved_def, res.segment_index)
}
// FIXME: this can use some more human-readable format (ideally, an IR
// even), as this should be a great debugging aid.
pub fn dump(&self) -> String {
let mut buf = String::new();
go(&mut buf, self, "\ncrate", self.root);
return buf.trim().to_string();
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
*buf += path;
*buf += "\n";
let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
entries.sort_by_key(|(name, _)| name.clone());
for (name, def) in entries {
*buf += &format!("{}:", name);
if def.types.is_some() {
*buf += " t";
}
if def.values.is_some() {
*buf += " v";
}
if def.macros.is_some() {
*buf += " m";
}
if def.is_none() {
*buf += " _";
}
*buf += "\n";
}
for (name, child) in map.modules[module].children.iter() {
let path = path.to_string() + &format!("::{}", name);
go(buf, map, &path, *child);
}
}
}
}
impl ModuleData {

View file

@ -10,11 +10,10 @@ use insta::assert_snapshot;
use ra_db::{fixture::WithFixture, SourceDatabase};
use test_utils::covers;
use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId};
use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
fn def_map(fixture: &str) -> String {
let dm = compute_crate_def_map(fixture);
render_crate_def_map(&dm)
compute_crate_def_map(fixture).dump()
}
fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
@ -23,44 +22,6 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
db.crate_def_map(krate)
}
fn render_crate_def_map(map: &CrateDefMap) -> String {
let mut buf = String::new();
go(&mut buf, map, "\ncrate", map.root);
return buf.trim().to_string();
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
*buf += path;
*buf += "\n";
let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
entries.sort_by_key(|(name, _)| name.clone());
for (name, def) in entries {
*buf += &format!("{}:", name);
if def.types.is_some() {
*buf += " t";
}
if def.values.is_some() {
*buf += " v";
}
if def.macros.is_some() {
*buf += " m";
}
if def.is_none() {
*buf += " _";
}
*buf += "\n";
}
for (name, child) in map.modules[module].children.iter() {
let path = path.to_string() + &format!("::{}", name);
go(buf, map, &path, *child);
}
}
}
#[test]
fn crate_def_map_smoke_test() {
let map = def_map(