Skip to content

internal: Catch panics in inference in analysis-stats #19809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/rust-analyzer/src/cli/analysis_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::{
env, fmt,
ops::AddAssign,
panic::{AssertUnwindSafe, catch_unwind},
time::{SystemTime, UNIX_EPOCH},
};

Expand Down Expand Up @@ -721,6 +722,7 @@ impl flags::AnalysisStats {
let mut num_pats_unknown = 0;
let mut num_pats_partially_unknown = 0;
let mut num_pat_type_mismatches = 0;
let mut panics = 0;
for &body_id in bodies {
let name = body_id.name(db).unwrap_or_else(Name::missing);
let module = body_id.module(db);
Expand Down Expand Up @@ -774,7 +776,20 @@ impl flags::AnalysisStats {
}
bar.set_message(msg);
let body = db.body(body_id.into());
let inference_result = db.infer(body_id.into());
let inference_result = catch_unwind(AssertUnwindSafe(|| db.infer(body_id.into())));
let inference_result = match inference_result {
Ok(inference_result) => inference_result,
Err(p) => {
if let Some(s) = p.downcast_ref::<&str>() {
eprintln!("infer panicked for {}: {}", full_name(), s);
} else if let Some(s) = p.downcast_ref::<String>() {
eprintln!("infer panicked for {}: {}", full_name(), s);
}
panics += 1;
bar.inc(1);
continue;
}
};
// This query is LRU'd, so actually calling it will skew the timing results.
let sm = || db.body_with_source_map(body_id.into()).1;

Expand Down Expand Up @@ -1008,6 +1023,7 @@ impl flags::AnalysisStats {
percentage(num_pats_partially_unknown, num_pats),
num_pat_type_mismatches
);
eprintln!(" panics: {}", panics);
eprintln!("{:<20} {}", "Inference:", inference_time);
report_metric("unknown type", num_exprs_unknown, "#");
report_metric("type mismatches", num_expr_type_mismatches, "#");
Expand Down