5614: Use split_once polyfill r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-30 20:38:58 +00:00 committed by GitHub
commit 43d8142214
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 339 additions and 357 deletions

View file

@ -4,7 +4,7 @@
use std::str::FromStr; use std::str::FromStr;
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use stdx::split_delim; use stdx::split_once;
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug)]
pub enum CfgFlag { pub enum CfgFlag {
@ -15,7 +15,7 @@ pub enum CfgFlag {
impl FromStr for CfgFlag { impl FromStr for CfgFlag {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match split_delim(s, '=') { let res = match split_once(s, '=') {
Some((key, value)) => { Some((key, value)) => {
if !(value.starts_with('"') && value.ends_with('"')) { if !(value.starts_with('"') && value.ends_with('"')) {
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s)); return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));

View file

@ -8,7 +8,7 @@ use std::{env, fmt::Write, path::PathBuf};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use pico_args::Arguments; use pico_args::Arguments;
use ra_ssr::{SsrPattern, SsrRule}; use ra_ssr::{SsrPattern, SsrRule};
use rust_analyzer::cli::{BenchWhat, Position, Verbosity}; use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
use vfs::AbsPathBuf; use vfs::AbsPathBuf;
pub(crate) struct Args { pub(crate) struct Args {
@ -24,23 +24,8 @@ pub(crate) enum Command {
Highlight { Highlight {
rainbow: bool, rainbow: bool,
}, },
Stats { AnalysisStats(AnalysisStatsCmd),
randomize: bool, Bench(BenchCmd),
parallel: bool,
memory_usage: bool,
only: Option<String>,
with_deps: bool,
path: PathBuf,
load_output_dirs: bool,
with_proc_macro: bool,
},
Bench {
memory_usage: bool,
path: PathBuf,
what: BenchWhat,
load_output_dirs: bool,
with_proc_macro: bool,
},
Diagnostics { Diagnostics {
path: PathBuf, path: PathBuf,
load_output_dirs: bool, load_output_dirs: bool,
@ -199,7 +184,7 @@ ARGS:
trailing.pop().unwrap().into() trailing.pop().unwrap().into()
}; };
Command::Stats { Command::AnalysisStats(AnalysisStatsCmd {
randomize, randomize,
parallel, parallel,
memory_usage, memory_usage,
@ -208,7 +193,7 @@ ARGS:
path, path,
load_output_dirs, load_output_dirs,
with_proc_macro, with_proc_macro,
} })
} }
"analysis-bench" => { "analysis-bench" => {
if matches.contains(["-h", "--help"]) { if matches.contains(["-h", "--help"]) {
@ -256,7 +241,13 @@ ARGS:
let memory_usage = matches.contains("--memory-usage"); let memory_usage = matches.contains("--memory-usage");
let load_output_dirs = matches.contains("--load-output-dirs"); let load_output_dirs = matches.contains("--load-output-dirs");
let with_proc_macro = matches.contains("--with-proc-macro"); let with_proc_macro = matches.contains("--with-proc-macro");
Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro } Command::Bench(BenchCmd {
memory_usage,
path,
what,
load_output_dirs,
with_proc_macro,
})
} }
"diagnostics" => { "diagnostics" => {
if matches.contains(["-h", "--help"]) { if matches.contains(["-h", "--help"]) {

View file

@ -33,36 +33,8 @@ fn main() -> Result<()> {
args::Command::Parse { no_dump } => cli::parse(no_dump)?, args::Command::Parse { no_dump } => cli::parse(no_dump)?,
args::Command::Symbols => cli::symbols()?, args::Command::Symbols => cli::symbols()?,
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?, args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
args::Command::Stats { args::Command::AnalysisStats(cmd) => cmd.run(args.verbosity)?,
randomize, args::Command::Bench(cmd) => cmd.run(args.verbosity)?,
parallel,
memory_usage,
only,
with_deps,
path,
load_output_dirs,
with_proc_macro,
} => cli::analysis_stats(
args.verbosity,
memory_usage,
path.as_ref(),
only.as_ref().map(String::as_ref),
with_deps,
randomize,
parallel,
load_output_dirs,
with_proc_macro,
)?,
args::Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro } => {
cli::analysis_bench(
args.verbosity,
path.as_ref(),
what,
memory_usage,
load_output_dirs,
with_proc_macro,
)?
}
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => { args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)? cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
} }

View file

@ -14,8 +14,8 @@ use ra_ide::Analysis;
use ra_prof::profile; use ra_prof::profile;
use ra_syntax::{AstNode, SourceFile}; use ra_syntax::{AstNode, SourceFile};
pub use analysis_bench::{analysis_bench, BenchWhat, Position}; pub use analysis_bench::{BenchCmd, BenchWhat, Position};
pub use analysis_stats::analysis_stats; pub use analysis_stats::AnalysisStatsCmd;
pub use diagnostics::diagnostics; pub use diagnostics::diagnostics;
pub use load_cargo::load_cargo; pub use load_cargo::load_cargo;
pub use ssr::{apply_ssr_rules, search_for_patterns}; pub use ssr::{apply_ssr_rules, search_for_patterns};

View file

@ -1,8 +1,8 @@
//! Benchmark operations like highlighting or goto definition. //! Benchmark operations like highlighting or goto definition.
use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant}; use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
use anyhow::{format_err, Result}; use anyhow::{bail, format_err, Result};
use ra_db::{ use ra_db::{
salsa::{Database, Durability}, salsa::{Database, Durability},
FileId, FileId,
@ -15,6 +15,14 @@ use crate::{
print_memory_usage, print_memory_usage,
}; };
pub struct BenchCmd {
pub path: PathBuf,
pub what: BenchWhat,
pub memory_usage: bool,
pub load_output_dirs: bool,
pub with_proc_macro: bool,
}
pub enum BenchWhat { pub enum BenchWhat {
Highlight { path: AbsPathBuf }, Highlight { path: AbsPathBuf },
Complete(Position), Complete(Position),
@ -30,85 +38,80 @@ pub struct Position {
impl FromStr for Position { impl FromStr for Position {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
let (path_line, column) = rsplit_at_char(s, ':')?; let mut split = s.rsplitn(3, ':');
let (path, line) = rsplit_at_char(path_line, ':')?; match (split.next(), split.next(), split.next()) {
let path = env::current_dir().unwrap().join(path); (Some(column), Some(line), Some(path)) => {
let path = AbsPathBuf::assert(path); let path = env::current_dir().unwrap().join(path);
Ok(Position { path, line: line.parse()?, column: column.parse()? }) let path = AbsPathBuf::assert(path);
Ok(Position { path, line: line.parse()?, column: column.parse()? })
}
_ => bail!("position should be in file:line:column format: {:?}", s),
}
} }
} }
fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { impl BenchCmd {
let idx = s.rfind(c).ok_or_else(|| format_err!("no `{}` in {}", c, s))?; pub fn run(self, verbosity: Verbosity) -> Result<()> {
Ok((&s[..idx], &s[idx + 1..])) ra_prof::init();
}
pub fn analysis_bench( let start = Instant::now();
verbosity: Verbosity, eprint!("loading: ");
path: &Path, let (mut host, vfs) = load_cargo(&self.path, self.load_output_dirs, self.with_proc_macro)?;
what: BenchWhat, eprintln!("{:?}\n", start.elapsed());
memory_usage: bool,
load_output_dirs: bool,
with_proc_macro: bool,
) -> Result<()> {
ra_prof::init();
let start = Instant::now(); let file_id = {
eprint!("loading: "); let path = match &self.what {
let (mut host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?; BenchWhat::Highlight { path } => path,
eprintln!("{:?}\n", start.elapsed()); BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
};
let file_id = { let path = path.clone().into();
let path = match &what { vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
BenchWhat::Highlight { path } => path,
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
}; };
let path = path.clone().into();
vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
};
match &what { match &self.what {
BenchWhat::Highlight { .. } => { BenchWhat::Highlight { .. } => {
let res = do_work(&mut host, file_id, |analysis| {
analysis.diagnostics(file_id, true).unwrap();
analysis.highlight_as_html(file_id, false).unwrap()
});
if verbosity.is_verbose() {
println!("\n{}", res);
}
}
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
let is_completion = matches!(what, BenchWhat::Complete(..));
let offset = host
.analysis()
.file_line_index(file_id)?
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
let file_position = FilePosition { file_id, offset };
if is_completion {
let options = CompletionConfig::default();
let res = do_work(&mut host, file_id, |analysis| { let res = do_work(&mut host, file_id, |analysis| {
analysis.completions(&options, file_position) analysis.diagnostics(file_id, true).unwrap();
analysis.highlight_as_html(file_id, false).unwrap()
}); });
if verbosity.is_verbose() { if verbosity.is_verbose() {
println!("\n{:#?}", res); println!("\n{}", res);
} }
} else { }
let res = BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_position)); let is_completion = matches!(self.what, BenchWhat::Complete(..));
if verbosity.is_verbose() {
println!("\n{:#?}", res); let offset = host
.analysis()
.file_line_index(file_id)?
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
let file_position = FilePosition { file_id, offset };
if is_completion {
let options = CompletionConfig::default();
let res = do_work(&mut host, file_id, |analysis| {
analysis.completions(&options, file_position)
});
if verbosity.is_verbose() {
println!("\n{:#?}", res);
}
} else {
let res = do_work(&mut host, file_id, |analysis| {
analysis.goto_definition(file_position)
});
if verbosity.is_verbose() {
println!("\n{:#?}", res);
}
} }
} }
} }
}
if memory_usage { if self.memory_usage {
print_memory_usage(host, vfs); print_memory_usage(host, vfs);
} }
Ok(()) Ok(())
}
} }
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T { fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {

View file

@ -2,7 +2,7 @@
//! errors. //! errors.
use std::{ use std::{
path::Path, path::PathBuf,
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
@ -39,273 +39,280 @@ impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
} }
} }
pub fn analysis_stats( pub struct AnalysisStatsCmd {
verbosity: Verbosity, pub randomize: bool,
memory_usage: bool, pub parallel: bool,
path: &Path, pub memory_usage: bool,
only: Option<&str>, pub only: Option<String>,
with_deps: bool, pub with_deps: bool,
randomize: bool, pub path: PathBuf,
parallel: bool, pub load_output_dirs: bool,
load_output_dirs: bool, pub with_proc_macro: bool,
with_proc_macro: bool, }
) -> Result<()> {
let mut rng = {
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
Rand32::new(seed)
};
let mut db_load_sw = StopWatch::start().memory(memory_usage); impl AnalysisStatsCmd {
let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?; pub fn run(self, verbosity: Verbosity) -> Result<()> {
let db = host.raw_database(); let mut rng = {
eprintln!("Database loaded {}", db_load_sw.elapsed()); let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
Rand32::new(seed)
};
let mut analysis_sw = StopWatch::start().memory(memory_usage); let mut db_load_sw = self.stop_watch();
let mut num_crates = 0; let (host, vfs) = load_cargo(&self.path, self.load_output_dirs, self.with_proc_macro)?;
let mut visited_modules = FxHashSet::default(); let db = host.raw_database();
let mut visit_queue = Vec::new(); eprintln!("Database loaded {}", db_load_sw.elapsed());
let mut krates = Crate::all(db); let mut analysis_sw = self.stop_watch();
if randomize { let mut num_crates = 0;
shuffle(&mut rng, &mut krates); let mut visited_modules = FxHashSet::default();
} let mut visit_queue = Vec::new();
for krate in krates {
let module = krate.root_module(db).expect("crate without root module"); let mut krates = Crate::all(db);
let file_id = module.definition_source(db).file_id; if self.randomize {
let file_id = file_id.original_file(db); shuffle(&mut rng, &mut krates);
let source_root = db.file_source_root(file_id);
let source_root = db.source_root(source_root);
if !source_root.is_library || with_deps {
num_crates += 1;
visit_queue.push(module);
} }
} for krate in krates {
let module = krate.root_module(db).expect("crate without root module");
if randomize { let file_id = module.definition_source(db).file_id;
shuffle(&mut rng, &mut visit_queue); let file_id = file_id.original_file(db);
} let source_root = db.file_source_root(file_id);
let source_root = db.source_root(source_root);
eprintln!("Crates in this dir: {}", num_crates); if !source_root.is_library || self.with_deps {
let mut num_decls = 0; num_crates += 1;
let mut funcs = Vec::new(); visit_queue.push(module);
while let Some(module) = visit_queue.pop() {
if visited_modules.insert(module) {
visit_queue.extend(module.children(db));
for decl in module.declarations(db) {
num_decls += 1;
if let ModuleDef::Function(f) = decl {
funcs.push(f);
}
} }
}
for impl_def in module.impl_defs(db) { if self.randomize {
for item in impl_def.items(db) { shuffle(&mut rng, &mut visit_queue);
}
eprintln!("Crates in this dir: {}", num_crates);
let mut num_decls = 0;
let mut funcs = Vec::new();
while let Some(module) = visit_queue.pop() {
if visited_modules.insert(module) {
visit_queue.extend(module.children(db));
for decl in module.declarations(db) {
num_decls += 1; num_decls += 1;
if let AssocItem::Function(f) = item { if let ModuleDef::Function(f) = decl {
funcs.push(f); funcs.push(f);
} }
} }
}
}
}
eprintln!("Total modules found: {}", visited_modules.len());
eprintln!("Total declarations: {}", num_decls);
eprintln!("Total functions: {}", funcs.len());
eprintln!("Item Collection: {}", analysis_sw.elapsed());
if randomize { for impl_def in module.impl_defs(db) {
shuffle(&mut rng, &mut funcs); for item in impl_def.items(db) {
} num_decls += 1;
if let AssocItem::Function(f) = item {
let mut bar = match verbosity { funcs.push(f);
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(), }
_ if parallel => ProgressReport::hidden(),
_ => ProgressReport::new(funcs.len() as u64),
};
if parallel {
let mut inference_sw = StopWatch::start().memory(memory_usage);
let snap = Snap(db.snapshot());
funcs
.par_iter()
.map_with(snap, |snap, &f| {
let f_id = FunctionId::from(f);
snap.0.body(f_id.into());
snap.0.infer(f_id.into());
})
.count();
eprintln!("Parallel Inference: {}", inference_sw.elapsed());
}
let mut inference_sw = StopWatch::start().memory(memory_usage);
bar.tick();
let mut num_exprs = 0;
let mut num_exprs_unknown = 0;
let mut num_exprs_partially_unknown = 0;
let mut num_type_mismatches = 0;
for f in funcs {
let name = f.name(db);
let full_name = f
.module(db)
.path_to_root(db)
.into_iter()
.rev()
.filter_map(|it| it.name(db))
.chain(Some(f.name(db)))
.join("::");
if let Some(only_name) = only {
if name.to_string() != only_name && full_name != only_name {
continue;
}
}
let mut msg = format!("processing: {}", full_name);
if verbosity.is_verbose() {
let src = f.source(db);
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.syntax().text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
}
if verbosity.is_spammy() {
bar.println(msg.to_string());
}
bar.set_message(&msg);
let f_id = FunctionId::from(f);
let body = db.body(f_id.into());
let inference_result = db.infer(f_id.into());
let (previous_exprs, previous_unknown, previous_partially_unknown) =
(num_exprs, num_exprs_unknown, num_exprs_partially_unknown);
for (expr_id, _) in body.exprs.iter() {
let ty = &inference_result[expr_id];
num_exprs += 1;
if let Ty::Unknown = ty {
num_exprs_unknown += 1;
} else {
let mut is_partially_unknown = false;
ty.walk(&mut |ty| {
if let Ty::Unknown = ty {
is_partially_unknown = true;
} }
});
if is_partially_unknown {
num_exprs_partially_unknown += 1;
} }
} }
if only.is_some() && verbosity.is_spammy() { }
// in super-verbose mode for just one function, we print every single expression eprintln!("Total modules found: {}", visited_modules.len());
let (_, sm) = db.body_with_source_map(f_id.into()); eprintln!("Total declarations: {}", num_decls);
let src = sm.expr_syntax(expr_id); eprintln!("Total functions: {}", funcs.len());
if let Ok(src) = src { eprintln!("Item Collection: {}", analysis_sw.elapsed());
let node = {
let root = db.parse_or_expand(src.file_id).unwrap(); if self.randomize {
src.value.to_node(&root) shuffle(&mut rng, &mut funcs);
}; }
let original_file = src.file_id.original_file(db);
let line_index = host.analysis().file_line_index(original_file).unwrap(); let mut bar = match verbosity {
let text_range = node.syntax().text_range(); Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
let (start, end) = ( _ if self.parallel => ProgressReport::hidden(),
line_index.line_col(text_range.start()), _ => ProgressReport::new(funcs.len() as u64),
line_index.line_col(text_range.end()), };
);
bar.println(format!( if self.parallel {
"{}:{}-{}:{}: {}", let mut inference_sw = self.stop_watch();
start.line + 1, let snap = Snap(db.snapshot());
start.col_utf16, funcs
end.line + 1, .par_iter()
end.col_utf16, .map_with(snap, |snap, &f| {
ty.display(db) let f_id = FunctionId::from(f);
)); snap.0.body(f_id.into());
snap.0.infer(f_id.into());
})
.count();
eprintln!("Parallel Inference: {}", inference_sw.elapsed());
}
let mut inference_sw = self.stop_watch();
bar.tick();
let mut num_exprs = 0;
let mut num_exprs_unknown = 0;
let mut num_exprs_partially_unknown = 0;
let mut num_type_mismatches = 0;
for f in funcs {
let name = f.name(db);
let full_name = f
.module(db)
.path_to_root(db)
.into_iter()
.rev()
.filter_map(|it| it.name(db))
.chain(Some(f.name(db)))
.join("::");
if let Some(only_name) = self.only.as_deref() {
if name.to_string() != only_name && full_name != only_name {
continue;
}
}
let mut msg = format!("processing: {}", full_name);
if verbosity.is_verbose() {
let src = f.source(db);
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.syntax().text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
}
if verbosity.is_spammy() {
bar.println(msg.to_string());
}
bar.set_message(&msg);
let f_id = FunctionId::from(f);
let body = db.body(f_id.into());
let inference_result = db.infer(f_id.into());
let (previous_exprs, previous_unknown, previous_partially_unknown) =
(num_exprs, num_exprs_unknown, num_exprs_partially_unknown);
for (expr_id, _) in body.exprs.iter() {
let ty = &inference_result[expr_id];
num_exprs += 1;
if let Ty::Unknown = ty {
num_exprs_unknown += 1;
} else { } else {
bar.println(format!("unknown location: {}", ty.display(db))); let mut is_partially_unknown = false;
ty.walk(&mut |ty| {
if let Ty::Unknown = ty {
is_partially_unknown = true;
}
});
if is_partially_unknown {
num_exprs_partially_unknown += 1;
}
} }
} if self.only.is_some() && verbosity.is_spammy() {
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) { // in super-verbose mode for just one function, we print every single expression
num_type_mismatches += 1;
if verbosity.is_verbose() {
let (_, sm) = db.body_with_source_map(f_id.into()); let (_, sm) = db.body_with_source_map(f_id.into());
let src = sm.expr_syntax(expr_id); let src = sm.expr_syntax(expr_id);
if let Ok(src) = src { if let Ok(src) = src {
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly let node = {
// But also, we should just turn the type mismatches into diagnostics and provide these let root = db.parse_or_expand(src.file_id).unwrap();
let root = db.parse_or_expand(src.file_id).unwrap(); src.value.to_node(&root)
let node = src.map(|e| e.to_node(&root).syntax().clone()); };
let original_range = original_range(db, node.as_ref()); let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_range.file_id); let line_index = host.analysis().file_line_index(original_file).unwrap();
let line_index = let text_range = node.syntax().text_range();
host.analysis().file_line_index(original_range.file_id).unwrap();
let text_range = original_range.range;
let (start, end) = ( let (start, end) = (
line_index.line_col(text_range.start()), line_index.line_col(text_range.start()),
line_index.line_col(text_range.end()), line_index.line_col(text_range.end()),
); );
bar.println(format!( bar.println(format!(
"{} {}:{}-{}:{}: Expected {}, got {}", "{}:{}-{}:{}: {}",
path,
start.line + 1, start.line + 1,
start.col_utf16, start.col_utf16,
end.line + 1, end.line + 1,
end.col_utf16, end.col_utf16,
mismatch.expected.display(db), ty.display(db)
mismatch.actual.display(db)
)); ));
} else { } else {
bar.println(format!( bar.println(format!("unknown location: {}", ty.display(db)));
"{}: Expected {}, got {}", }
name, }
mismatch.expected.display(db), if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
mismatch.actual.display(db) num_type_mismatches += 1;
)); if verbosity.is_verbose() {
let (_, sm) = db.body_with_source_map(f_id.into());
let src = sm.expr_syntax(expr_id);
if let Ok(src) = src {
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
// But also, we should just turn the type mismatches into diagnostics and provide these
let root = db.parse_or_expand(src.file_id).unwrap();
let node = src.map(|e| e.to_node(&root).syntax().clone());
let original_range = original_range(db, node.as_ref());
let path = vfs.file_path(original_range.file_id);
let line_index =
host.analysis().file_line_index(original_range.file_id).unwrap();
let text_range = original_range.range;
let (start, end) = (
line_index.line_col(text_range.start()),
line_index.line_col(text_range.end()),
);
bar.println(format!(
"{} {}:{}-{}:{}: Expected {}, got {}",
path,
start.line + 1,
start.col_utf16,
end.line + 1,
end.col_utf16,
mismatch.expected.display(db),
mismatch.actual.display(db)
));
} else {
bar.println(format!(
"{}: Expected {}, got {}",
name,
mismatch.expected.display(db),
mismatch.actual.display(db)
));
}
} }
} }
} }
if verbosity.is_spammy() {
bar.println(format!(
"In {}: {} exprs, {} unknown, {} partial",
full_name,
num_exprs - previous_exprs,
num_exprs_unknown - previous_unknown,
num_exprs_partially_unknown - previous_partially_unknown
));
}
bar.inc(1);
} }
if verbosity.is_spammy() { bar.finish_and_clear();
bar.println(format!( eprintln!("Total expressions: {}", num_exprs);
"In {}: {} exprs, {} unknown, {} partial", eprintln!(
full_name, "Expressions of unknown type: {} ({}%)",
num_exprs - previous_exprs, num_exprs_unknown,
num_exprs_unknown - previous_unknown, if num_exprs > 0 { num_exprs_unknown * 100 / num_exprs } else { 100 }
num_exprs_partially_unknown - previous_partially_unknown );
)); report_metric("unknown type", num_exprs_unknown, "#");
eprintln!(
"Expressions of partially unknown type: {} ({}%)",
num_exprs_partially_unknown,
if num_exprs > 0 { num_exprs_partially_unknown * 100 / num_exprs } else { 100 }
);
eprintln!("Type mismatches: {}", num_type_mismatches);
report_metric("type mismatches", num_type_mismatches, "#");
eprintln!("Inference: {}", inference_sw.elapsed());
let total_span = analysis_sw.elapsed();
eprintln!("Total: {}", total_span);
report_metric("total time", total_span.time.as_millis() as u64, "ms");
if let Some(instructions) = total_span.instructions {
report_metric("total instructions", instructions, "#instr");
}
if let Some(memory) = total_span.memory {
report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
} }
bar.inc(1);
}
bar.finish_and_clear();
eprintln!("Total expressions: {}", num_exprs);
eprintln!(
"Expressions of unknown type: {} ({}%)",
num_exprs_unknown,
if num_exprs > 0 { num_exprs_unknown * 100 / num_exprs } else { 100 }
);
report_metric("unknown type", num_exprs_unknown, "#");
eprintln!( if self.memory_usage {
"Expressions of partially unknown type: {} ({}%)", print_memory_usage(host, vfs);
num_exprs_partially_unknown, }
if num_exprs > 0 { num_exprs_partially_unknown * 100 / num_exprs } else { 100 }
);
eprintln!("Type mismatches: {}", num_type_mismatches); Ok(())
report_metric("type mismatches", num_type_mismatches, "#");
eprintln!("Inference: {}", inference_sw.elapsed());
let total_span = analysis_sw.elapsed();
eprintln!("Total: {}", total_span);
report_metric("total time", total_span.time.as_millis() as u64, "ms");
if let Some(instructions) = total_span.instructions {
report_metric("total instructions", instructions, "#instr");
}
if let Some(memory) = total_span.memory {
report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
} }
if memory_usage { fn stop_watch(&self) -> StopWatch {
print_memory_usage(host, vfs); StopWatch::start().memory(self.memory_usage)
} }
Ok(())
} }
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) { fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {

View file

@ -26,7 +26,7 @@ use ra_project_model::TargetKind;
use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize}; use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::to_value; use serde_json::to_value;
use stdx::{format_to, split_delim}; use stdx::{format_to, split_once};
use crate::{ use crate::{
cargo_target_spec::CargoTargetSpec, cargo_target_spec::CargoTargetSpec,
@ -865,7 +865,7 @@ pub(crate) fn handle_resolve_code_action(
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()); .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?; let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?;
let (id_string, index) = split_delim(&params.id, ':').unwrap(); let (id_string, index) = split_once(&params.id, ':').unwrap();
let index = index.parse::<usize>().unwrap(); let index = index.parse::<usize>().unwrap();
let assist = &assists[index]; let assist = &assists[index];
assert!(assist.assist.id.0 == id_string); assert!(assist.assist.id.0 == id_string);

View file

@ -109,9 +109,18 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
*buf = buf.replace(from, to) *buf = buf.replace(from, to)
} }
pub fn split_delim(haystack: &str, delim: char) -> Option<(&str, &str)> { // https://github.com/rust-lang/rust/issues/74773
let idx = haystack.find(delim)?; pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) let mut split = haystack.splitn(2, delim);
let prefix = split.next()?;
let suffix = split.next()?;
Some((prefix, suffix))
}
pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
let mut split = haystack.rsplitn(2, delim);
let suffix = split.next()?;
let prefix = split.next()?;
Some((prefix, suffix))
} }
pub fn trim_indent(mut text: &str) -> String { pub fn trim_indent(mut text: &str) -> String {

View file

@ -2,7 +2,7 @@
//! rust-analyzer database from a single string. //! rust-analyzer database from a single string.
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use stdx::{lines_with_ends, split_delim, trim_indent}; use stdx::{lines_with_ends, split_once, trim_indent};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Fixture { pub struct Fixture {
@ -71,14 +71,14 @@ impl Fixture {
let mut cfg_key_values = Vec::new(); let mut cfg_key_values = Vec::new();
let mut env = FxHashMap::default(); let mut env = FxHashMap::default();
for component in components[1..].iter() { for component in components[1..].iter() {
let (key, value) = split_delim(component, ':').unwrap(); let (key, value) = split_once(component, ':').unwrap();
match key { match key {
"crate" => krate = Some(value.to_string()), "crate" => krate = Some(value.to_string()),
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(), "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
"edition" => edition = Some(value.to_string()), "edition" => edition = Some(value.to_string()),
"cfg" => { "cfg" => {
for entry in value.split(',') { for entry in value.split(',') {
match split_delim(entry, '=') { match split_once(entry, '=') {
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())), Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
None => cfg_atoms.push(entry.to_string()), None => cfg_atoms.push(entry.to_string()),
} }
@ -86,7 +86,7 @@ impl Fixture {
} }
"env" => { "env" => {
for key in value.split(',') { for key in value.split(',') {
if let Some((k, v)) = split_delim(key, '=') { if let Some((k, v)) = split_once(key, '=') {
env.insert(k.into(), v.into()); env.insert(k.into(), v.into());
} }
} }