Skip to content

Commit ae19b2f

Browse files
committed
Rust: check that TextRanges are for the correct file
1 parent faa1689 commit ae19b2f

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

rust/extractor/src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ fn extract(
1212
traps: &trap::TrapFileProvider,
1313
file: std::path::PathBuf,
1414
) -> anyhow::Result<()> {
15-
let (ast, input, parse_errors, semi) = rust_analyzer.parse(&file);
15+
let (ast, input, parse_errors, file_id, semi) = rust_analyzer.parse(&file);
1616
let line_index = LineIndex::new(input.as_ref());
1717
let display_path = file.to_string_lossy();
1818
let mut trap = traps.create("source", &file);
1919
let label = trap.emit_file(&file);
20-
let mut translator =
21-
translate::Translator::new(trap, display_path.as_ref(), label, line_index, semi);
20+
let mut translator = translate::Translator::new(
21+
trap,
22+
display_path.as_ref(),
23+
label,
24+
line_index,
25+
file_id,
26+
semi,
27+
);
2228

2329
for err in parse_errors {
2430
translator.emit_parse_error(&err);

rust/extractor/src/rust_analyzer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl RustAnalyzer {
5959
SourceFile,
6060
Arc<str>,
6161
Vec<SyntaxError>,
62+
Option<EditionedFileId>,
6263
Option<Semantics<'_, RootDatabase>>,
6364
) {
6465
let mut p = path.as_path();
@@ -80,6 +81,7 @@ impl RustAnalyzer {
8081
db.parse_errors(file_id)
8182
.map(|x| x.to_vec())
8283
.unwrap_or_default(),
84+
Some(file_id),
8385
Some(semi),
8486
);
8587
}
@@ -100,7 +102,7 @@ impl RustAnalyzer {
100102
let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
101103
errors.extend(parse.errors());
102104
errors.extend(err);
103-
(parse.tree(), input.as_ref().into(), errors, None)
105+
(parse.tree(), input.as_ref().into(), errors, None, None)
104106
}
105107
}
106108

rust/extractor/src/translate/base.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ra_ap_hir::Semantics;
99
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
1010
use ra_ap_ide_db::RootDatabase;
1111
use ra_ap_parser::SyntaxKind;
12+
use ra_ap_span::{EditionedFileId, TextSize};
1213
use ra_ap_syntax::ast::RangeItem;
1314
use ra_ap_syntax::{
1415
ast, AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxToken, TextRange,
@@ -63,11 +64,13 @@ impl TextValue for ast::RangePat {
6364
self.op_token().map(|x| x.text().to_string())
6465
}
6566
}
67+
6668
pub struct Translator<'a> {
6769
pub trap: TrapFile,
6870
path: &'a str,
6971
label: trap::Label,
7072
line_index: LineIndex,
73+
file_id: Option<EditionedFileId>,
7174
pub semi: Option<Semantics<'a, RootDatabase>>,
7275
}
7376

@@ -77,13 +80,15 @@ impl<'a> Translator<'a> {
7780
path: &'a str,
7881
label: trap::Label,
7982
line_index: LineIndex,
83+
file_id: Option<EditionedFileId>,
8084
semi: Option<Semantics<'a, RootDatabase>>,
8185
) -> Translator<'a> {
8286
Translator {
8387
trap,
8488
path,
8589
label,
8690
line_index,
91+
file_id,
8792
semi,
8893
}
8994
}
@@ -107,18 +112,32 @@ impl<'a> Translator<'a> {
107112
(start, end)
108113
}
109114

110-
pub fn text_range_for_node(&mut self, node: &impl ast::AstNode) -> TextRange {
115+
pub fn text_range_for_node(&mut self, node: &impl ast::AstNode) -> Option<TextRange> {
111116
if let Some(semi) = self.semi.as_ref() {
112117
let file_range = semi.original_range(node.syntax());
113-
file_range.range
118+
let file_id = self.file_id?;
119+
if file_id == file_range.file_id {
120+
Some(file_range.range)
121+
} else {
122+
None
123+
}
114124
} else {
115-
node.syntax().text_range()
125+
Some(node.syntax().text_range())
116126
}
117127
}
118128
pub fn emit_location<T: TrapClass>(&mut self, label: Label<T>, node: &impl ast::AstNode) {
119-
let range = self.text_range_for_node(node);
120-
let (start, end) = self.location(range);
121-
self.trap.emit_location(self.label, label, start, end)
129+
if let Some(range) = self.text_range_for_node(node) {
130+
let (start, end) = self.location(range);
131+
self.trap.emit_location(self.label, label, start, end)
132+
} else {
133+
self.emit_diagnostic(
134+
DiagnosticSeverity::Info,
135+
"locations".to_owned(),
136+
"missing location for AstNode".to_owned(),
137+
"missing location for AstNode".to_owned(),
138+
(LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 }),
139+
);
140+
}
122141
}
123142
pub fn emit_location_token(&mut self, label: Label<generated::Token>, token: &SyntaxToken) {
124143
let (start, end) = self.location(token.text_range());
@@ -248,7 +267,7 @@ impl<'a> Translator<'a> {
248267
mcall.path().map(|p| p.to_string()).unwrap_or_default(),
249268
kind, expand_to
250269
),
251-
range,
270+
range.unwrap_or_else(|| TextRange::empty(TextSize::from(0))),
252271
));
253272
}
254273
} else {
@@ -259,7 +278,7 @@ impl<'a> Translator<'a> {
259278
"macro expansion failed: could not resolve macro '{}'",
260279
mcall.path().map(|p| p.to_string()).unwrap_or_default()
261280
),
262-
range,
281+
range.unwrap_or_else(|| TextRange::empty(TextSize::from(0))),
263282
));
264283
}
265284
}

0 commit comments

Comments
 (0)