Skip to content

Commit 61592a3

Browse files
committed
Rust: fix locations
In QL locations are 1-based inclusive ranges. The locations is rust are 0-based and the end position is exclusive. To patch things up, subtract 1 from the end offset and add 1 to all line and column numbers.
1 parent 86215b4 commit 61592a3

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

rust/extractor/src/translate.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ra_ap_hir_def::hir::{CaptureBy, ExprId, LabelId, MatchArm, PatId, Statement}
1111
use ra_ap_ide_db::line_index::LineIndex;
1212
use ra_ap_ide_db::{label, LineIndexDatabase, RootDatabase};
1313
use ra_ap_syntax::ast::RangeOp;
14-
use ra_ap_syntax::{AstNode, Edition};
14+
use ra_ap_syntax::{AstNode, Edition, TextRange, TextSize};
1515
use ra_ap_vfs::{FileId, Vfs};
1616
use std::collections::HashMap;
1717
use std::fs;
@@ -81,11 +81,9 @@ impl CrateTranslator<'_> {
8181
.file_id()
8282
.map(|f| (f.file_id(), source))
8383
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
84-
.and_then(|(data, source)| {
84+
.map(|(data, source)| {
8585
let range = source.value.text_range();
86-
let start = data.line_index.line_col(range.start());
87-
let end = data.line_index.line_col(range.end());
88-
Some(self.trap.emit_location(data.label, start, end))
86+
self.emit_location_textrange(data, range)
8987
})
9088
}
9189

@@ -151,12 +149,19 @@ impl CrateTranslator<'_> {
151149
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
152150
.map(|(data, source)| {
153151
let range = source.value.syntax().text_range();
154-
let start = data.line_index.line_col(range.start());
155-
let end = data.line_index.line_col(range.end());
156-
self.trap.emit_location(data.label, start, end)
152+
self.emit_location_textrange(data, range)
157153
})
158154
}
159-
155+
fn emit_location_textrange(&mut self, data: FileData, range: TextRange) -> trap::Label {
156+
let start = data.line_index.line_col(range.start());
157+
let end = data.line_index.line_col(
158+
range
159+
.end()
160+
.checked_sub(TextSize::new(1))
161+
.unwrap_or(range.end()),
162+
);
163+
self.trap.emit_location(data.label, start, end)
164+
}
160165
fn emit_label(
161166
&mut self,
162167
label_id: LabelId,

rust/extractor/src/trap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::config::Compression;
22
use crate::generated;
33
use crate::{config, path};
44
use codeql_extractor::trap;
5-
use log::{debug};
5+
use log::debug;
66
use ra_ap_ide_db::line_index::LineCol;
77
use std::ffi::OsString;
8-
use std::fmt::{Debug};
8+
use std::fmt::Debug;
99
use std::path::{Path, PathBuf};
1010

1111
//TODO: typed labels
@@ -85,10 +85,10 @@ impl TrapFile {
8585
start: LineCol,
8686
end: LineCol,
8787
) -> trap::Label {
88-
let start_line = start.line as usize;
89-
let start_column = start.col as usize;
90-
let end_line = end.line as usize;
91-
let end_column = end.col as usize;
88+
let start_line = 1 + start.line as usize;
89+
let start_column = 1 + start.col as usize;
90+
let end_line = 1 + end.line as usize;
91+
let end_column = 1 + end.col as usize;
9292
let (ret, _) = self.writer.location_label(trap::Location {
9393
start_line,
9494
start_column,

0 commit comments

Comments
 (0)