Rust: avoid panics

If:
* the text for a file_id is not found (likely non-utf data in file)
* path does not appear in Vfs, in which case we fall back on loading the file from disk with no "semantics" available
This commit is contained in:
Arthur Baars
2024-10-28 18:33:15 +01:00
parent c87f2c4eb1
commit 0e511d640b
2 changed files with 23 additions and 16 deletions

View File

@@ -41,7 +41,7 @@ fn extract(
label,
line_index,
file_id,
rust_analyzer.semantics(),
file_id.and(rust_analyzer.semantics()),
);
for err in errors {

View File

@@ -78,22 +78,29 @@ impl<'a> RustAnalyzer<'a> {
.map(VfsPath::from)
.and_then(|x| vfs.file_id(&x))
{
let input: Arc<str> = semantics.db.file_text(file_id);
let file_id = EditionedFileId::current_edition(file_id);
let source_file = semantics.parse(file_id);
let errors = semantics
.db
.parse_errors(file_id)
.into_iter()
.flat_map(|x| x.to_vec())
.collect();
if let Ok(input) = std::panic::catch_unwind(|| semantics.db.file_text(file_id)) {
let file_id = EditionedFileId::current_edition(file_id);
let source_file = semantics.parse(file_id);
let errors = semantics
.db
.parse_errors(file_id)
.into_iter()
.flat_map(|x| x.to_vec())
.collect();
return ParseResult {
ast: source_file,
text: input,
errors,
file_id: Some(file_id),
};
return ParseResult {
ast: source_file,
text: input,
errors,
file_id: Some(file_id),
};
} else {
log::debug!(
"No text available for file_id '{:?}', falling back to loading file '{}' from disk.",
file_id,
path.to_string_lossy()
)
}
}
}
let mut errors = Vec::new();