Skip to content

Commit d976772

Browse files
Merge #3157
3157: Extend analysis-stats a bit r=matklad a=flodiebold This adds some tools helpful when debugging nondeterminism in analysis-stats: - a `--randomize` option that analyses everything in random order - a `-vv` option that prints even more detail Also add a debug log if Chalk fuel is exhausted (which would be a source of nondeterminism, but didn't happen in my tests). I found one source of nondeterminism (rust-lang/chalk#331), but there are still other cases remaining. Co-authored-by: Florian Diebold <[email protected]>
2 parents ff7110f + 3484d72 commit d976772

File tree

6 files changed

+103
-14
lines changed

6 files changed

+103
-14
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ opt-level = 0
3131

3232
[patch.'crates-io']
3333
# rowan = { path = "../rowan" }
34+
35+
[patch.'https://github.com/rust-lang/chalk.git']
36+
# chalk-solve = { path = "../chalk/chalk-solve" }
37+
# chalk-rust-ir = { path = "../chalk/chalk-rust-ir" }
38+
# chalk-ir = { path = "../chalk/chalk-ir" }

crates/ra_cli/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ authors = ["rust-analyzer developers"]
66
publish = false
77

88
[dependencies]
9+
itertools = "0.8.0"
910
pico-args = "0.3.0"
1011
env_logger = { version = "0.7.1", default-features = false }
12+
rand = { version = "0.7.0", features = ["small_rng"] }
1113

1214
ra_syntax = { path = "../ra_syntax" }
1315
ra_ide = { path = "../ra_ide" }

crates/ra_cli/src/analysis_stats.rs

+74-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
44

5+
use itertools::Itertools;
6+
use rand::{seq::SliceRandom, thread_rng};
7+
58
use hir::{
69
db::{DefDatabase, HirDatabase},
710
AssocItem, Crate, HasSource, HirDisplay, ModuleDef,
@@ -19,6 +22,7 @@ pub fn run(
1922
path: &Path,
2023
only: Option<&str>,
2124
with_deps: bool,
25+
randomize: bool,
2226
) -> Result<()> {
2327
let db_load_time = Instant::now();
2428
let (mut host, roots) = ra_batch::load_cargo(path)?;
@@ -41,7 +45,11 @@ pub fn run(
4145
})
4246
.collect::<HashSet<_>>();
4347

44-
for krate in Crate::all(db) {
48+
let mut krates = Crate::all(db);
49+
if randomize {
50+
krates.shuffle(&mut thread_rng());
51+
}
52+
for krate in krates {
4553
let module = krate.root_module(db).expect("crate without root module");
4654
let file_id = module.definition_source(db).file_id;
4755
if members.contains(&db.file_source_root(file_id.original_file(db))) {
@@ -50,6 +58,10 @@ pub fn run(
5058
}
5159
}
5260

61+
if randomize {
62+
visit_queue.shuffle(&mut thread_rng());
63+
}
64+
5365
println!("Crates in this dir: {}", num_crates);
5466
let mut num_decls = 0;
5567
let mut funcs = Vec::new();
@@ -79,10 +91,14 @@ pub fn run(
7991
println!("Total functions: {}", funcs.len());
8092
println!("Item Collection: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage());
8193

94+
if randomize {
95+
funcs.shuffle(&mut thread_rng());
96+
}
97+
8298
let inference_time = Instant::now();
8399
let mut bar = match verbosity {
84-
Verbosity::Verbose | Verbosity::Normal => ProgressReport::new(funcs.len() as u64),
85-
Verbosity::Quiet => ProgressReport::hidden(),
100+
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
101+
_ => ProgressReport::new(funcs.len() as u64),
86102
};
87103

88104
bar.tick();
@@ -92,23 +108,36 @@ pub fn run(
92108
let mut num_type_mismatches = 0;
93109
for f in funcs {
94110
let name = f.name(db);
95-
let mut msg = format!("processing: {}", name);
111+
let full_name = f
112+
.module(db)
113+
.path_to_root(db)
114+
.into_iter()
115+
.rev()
116+
.filter_map(|it| it.name(db))
117+
.chain(Some(f.name(db)))
118+
.join("::");
119+
if let Some(only_name) = only {
120+
if name.to_string() != only_name && full_name != only_name {
121+
continue;
122+
}
123+
}
124+
let mut msg = format!("processing: {}", full_name);
96125
if verbosity.is_verbose() {
97126
let src = f.source(db);
98127
let original_file = src.file_id.original_file(db);
99128
let path = db.file_relative_path(original_file);
100129
let syntax_range = src.value.syntax().text_range();
101130
write!(msg, " ({:?} {})", path, syntax_range).unwrap();
102131
}
103-
bar.set_message(&msg);
104-
if let Some(only_name) = only {
105-
if name.to_string() != only_name {
106-
continue;
107-
}
132+
if verbosity.is_spammy() {
133+
bar.println(format!("{}", msg));
108134
}
135+
bar.set_message(&msg);
109136
let f_id = FunctionId::from(f);
110137
let body = db.body(f_id.into());
111138
let inference_result = db.infer(f_id.into());
139+
let (previous_exprs, previous_unknown, previous_partially_unknown) =
140+
(num_exprs, num_exprs_unknown, num_exprs_partially_unknown);
112141
for (expr_id, _) in body.exprs.iter() {
113142
let ty = &inference_result[expr_id];
114143
num_exprs += 1;
@@ -125,6 +154,33 @@ pub fn run(
125154
num_exprs_partially_unknown += 1;
126155
}
127156
}
157+
if only.is_some() && verbosity.is_spammy() {
158+
// in super-verbose mode for just one function, we print every single expression
159+
let (_, sm) = db.body_with_source_map(f_id.into());
160+
let src = sm.expr_syntax(expr_id);
161+
if let Some(src) = src {
162+
let original_file = src.file_id.original_file(db);
163+
let line_index = host.analysis().file_line_index(original_file).unwrap();
164+
let text_range = src.value.either(
165+
|it| it.syntax_node_ptr().range(),
166+
|it| it.syntax_node_ptr().range(),
167+
);
168+
let (start, end) = (
169+
line_index.line_col(text_range.start()),
170+
line_index.line_col(text_range.end()),
171+
);
172+
bar.println(format!(
173+
"{}:{}-{}:{}: {}",
174+
start.line + 1,
175+
start.col_utf16,
176+
end.line + 1,
177+
end.col_utf16,
178+
ty.display(db)
179+
));
180+
} else {
181+
bar.println(format!("unknown location: {}", ty.display(db)));
182+
}
183+
}
128184
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
129185
num_type_mismatches += 1;
130186
if verbosity.is_verbose() {
@@ -164,6 +220,15 @@ pub fn run(
164220
}
165221
}
166222
}
223+
if verbosity.is_spammy() {
224+
bar.println(format!(
225+
"In {}: {} exprs, {} unknown, {} partial",
226+
full_name,
227+
num_exprs - previous_exprs,
228+
num_exprs_unknown - previous_unknown,
229+
num_exprs_partially_unknown - previous_partially_unknown
230+
));
231+
}
167232
bar.inc(1);
168233
}
169234
bar.finish_and_clear();

crates/ra_cli/src/main.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
1616

1717
#[derive(Clone, Copy)]
1818
pub enum Verbosity {
19+
Spammy,
1920
Verbose,
2021
Normal,
2122
Quiet,
@@ -24,7 +25,13 @@ pub enum Verbosity {
2425
impl Verbosity {
2526
fn is_verbose(self) -> bool {
2627
match self {
27-
Verbosity::Verbose => true,
28+
Verbosity::Verbose | Verbosity::Spammy => true,
29+
_ => false,
30+
}
31+
}
32+
fn is_spammy(self) -> bool {
33+
match self {
34+
Verbosity::Spammy => true,
2835
_ => false,
2936
}
3037
}
@@ -86,14 +93,18 @@ fn main() -> Result<()> {
8693
return Ok(());
8794
}
8895
let verbosity = match (
96+
matches.contains(["-vv", "--spammy"]),
8997
matches.contains(["-v", "--verbose"]),
9098
matches.contains(["-q", "--quiet"]),
9199
) {
92-
(false, false) => Verbosity::Normal,
93-
(false, true) => Verbosity::Quiet,
94-
(true, false) => Verbosity::Verbose,
95-
(true, true) => Err("Invalid flags: -q conflicts with -v")?,
100+
(true, _, true) => Err("Invalid flags: -q conflicts with -vv")?,
101+
(true, _, false) => Verbosity::Spammy,
102+
(false, false, false) => Verbosity::Normal,
103+
(false, false, true) => Verbosity::Quiet,
104+
(false, true, false) => Verbosity::Verbose,
105+
(false, true, true) => Err("Invalid flags: -q conflicts with -v")?,
96106
};
107+
let randomize = matches.contains("--randomize");
97108
let memory_usage = matches.contains("--memory-usage");
98109
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
99110
let with_deps: bool = matches.contains("--with-deps");
@@ -111,6 +122,7 @@ fn main() -> Result<()> {
111122
path.as_ref(),
112123
only.as_ref().map(String::as_ref),
113124
with_deps,
125+
randomize,
114126
)?;
115127
}
116128
"analysis-bench" => {

crates/ra_hir_ty/src/traits.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ impl TraitSolver {
6060
context.0.db.check_canceled();
6161
let remaining = fuel.get();
6262
fuel.set(remaining - 1);
63+
if remaining == 0 {
64+
log::debug!("fuel exhausted");
65+
}
6366
remaining > 0
6467
})
6568
}

0 commit comments

Comments
 (0)