Skip to content

Commit 0e511d6

Browse files
committed
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
1 parent c87f2c4 commit 0e511d6

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

rust/extractor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn extract(
4141
label,
4242
line_index,
4343
file_id,
44-
rust_analyzer.semantics(),
44+
file_id.and(rust_analyzer.semantics()),
4545
);
4646

4747
for err in errors {

rust/extractor/src/rust_analyzer.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,29 @@ impl<'a> RustAnalyzer<'a> {
7878
.map(VfsPath::from)
7979
.and_then(|x| vfs.file_id(&x))
8080
{
81-
let input: Arc<str> = semantics.db.file_text(file_id);
82-
let file_id = EditionedFileId::current_edition(file_id);
83-
let source_file = semantics.parse(file_id);
84-
let errors = semantics
85-
.db
86-
.parse_errors(file_id)
87-
.into_iter()
88-
.flat_map(|x| x.to_vec())
89-
.collect();
81+
if let Ok(input) = std::panic::catch_unwind(|| semantics.db.file_text(file_id)) {
82+
let file_id = EditionedFileId::current_edition(file_id);
83+
let source_file = semantics.parse(file_id);
84+
let errors = semantics
85+
.db
86+
.parse_errors(file_id)
87+
.into_iter()
88+
.flat_map(|x| x.to_vec())
89+
.collect();
9090

91-
return ParseResult {
92-
ast: source_file,
93-
text: input,
94-
errors,
95-
file_id: Some(file_id),
96-
};
91+
return ParseResult {
92+
ast: source_file,
93+
text: input,
94+
errors,
95+
file_id: Some(file_id),
96+
};
97+
} else {
98+
log::debug!(
99+
"No text available for file_id '{:?}', falling back to loading file '{}' from disk.",
100+
file_id,
101+
path.to_string_lossy()
102+
)
103+
}
97104
}
98105
}
99106
let mut errors = Vec::new();

0 commit comments

Comments
 (0)