Skip to content

Commit fbb075b

Browse files
authored
Merge pull request #17 from github/aibaars/locations-2
TRAP locations: always fix-up empty ranges
2 parents ca91e15 + 3350d9d commit fbb075b

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

extractor/src/extractor.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -286,35 +286,33 @@ fn location_for<'a>(source: &Vec<u8>, fp: &String, label: Label, n: Node) -> Tra
286286
let start_col = n.start_position().column + 1;
287287
let mut end_line = n.end_position().row + 1;
288288
let mut end_col = n.end_position().column;
289-
if end_col == 0 {
290-
if start_line >= end_line {
291-
// the range is empty, clip it to sensible values
292-
end_line = start_line;
293-
end_col = start_col - 1;
294-
} else {
295-
let mut index = n.end_byte();
296-
// end_col = 0 means that we are the start of a line
297-
// unfortunately 0 is invalid as column number, therefore
298-
// we should update the end location to be the end of the
299-
// previous line
300-
if index > 0 && index <= source.len() {
289+
if start_line > end_line || start_line == end_line && start_col > end_col {
290+
// the range is empty, clip it to sensible values
291+
end_line = start_line;
292+
end_col = start_col - 1;
293+
} else if end_col == 0 {
294+
// end_col = 0 means that we are at the start of a line
295+
// unfortunately 0 is invalid as column number, therefore
296+
// we should update the end location to be the end of the
297+
// previous line
298+
let mut index = n.end_byte();
299+
if index > 0 && index <= source.len() {
300+
index -= 1;
301+
if source[index] != b'\n' {
302+
error!("expecting a line break symbol, but none found while correcting end column value");
303+
}
304+
end_line -= 1;
305+
end_col = 1;
306+
while index > 0 && source[index - 1] != b'\n' {
301307
index -= 1;
302-
if source[index] != b'\n' {
303-
error!("expecting a line break symbol, but none found while correcting end column value");
304-
}
305-
end_line -= 1;
306-
end_col = 1;
307-
while index > 0 && source[index - 1] != b'\n' {
308-
index -= 1;
309-
end_col += 1;
310-
}
311-
} else {
312-
error!(
313-
"cannot correct end column value: end_byte index {} is not in range [0,{}]",
314-
index,
315-
source.len()
316-
);
308+
end_col += 1;
317309
}
310+
} else {
311+
error!(
312+
"cannot correct end column value: end_byte index {} is not in range [1,{}]",
313+
index,
314+
source.len()
315+
);
318316
}
319317
}
320318
TrapEntry::Located(vec![

0 commit comments

Comments
 (0)