Skip to content

Rust: avoid panic from line_index crate #18759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub struct Translator<'a> {
pub semantics: Option<&'a Semantics<'a, RootDatabase>>,
}

const UNKNOWN_LOCATION: (LineCol, LineCol) =
(LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 });

impl<'a> Translator<'a> {
pub fn new(
trap: TrapFile,
Expand All @@ -98,8 +101,8 @@ impl<'a> Translator<'a> {
semantics: semantic_info.map(|i| i.semantics),
}
}
fn location(&self, range: TextRange) -> (LineCol, LineCol) {
let start = self.line_index.line_col(range.start());
fn location(&self, range: TextRange) -> Option<(LineCol, LineCol)> {
let start = self.line_index.try_line_col(range.start())?;
let range_end = range.end();
// QL end positions are inclusive, while TextRange offsets are exclusive and point at the position
// right after the last character of the range. We need to shift the end offset one character to the left to
Expand All @@ -111,11 +114,11 @@ impl<'a> Translator<'a> {
.checked_sub(i.into())
.and_then(|x| self.line_index.try_line_col(x))
{
return (start, end);
return Some((start, end));
}
}
let end = self.line_index.line_col(range_end);
(start, end)
let end = self.line_index.try_line_col(range_end)?;
Some((start, end))
}

pub fn text_range_for_node(&mut self, node: &impl ast::AstNode) -> Option<TextRange> {
Expand All @@ -132,16 +135,18 @@ impl<'a> Translator<'a> {
}
}
pub fn emit_location<T: TrapClass>(&mut self, label: Label<T>, node: &impl ast::AstNode) {
if let Some(range) = self.text_range_for_node(node) {
let (start, end) = self.location(range);
if let Some((start, end)) = self
.text_range_for_node(node)
.and_then(|r| self.location(r))
{
self.trap.emit_location(self.label, label, start, end)
} else {
self.emit_diagnostic(
DiagnosticSeverity::Debug,
"locations".to_owned(),
"missing location for AstNode".to_owned(),
"missing location for AstNode".to_owned(),
(LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 }),
UNKNOWN_LOCATION,
);
}
}
Expand All @@ -156,8 +161,9 @@ impl<'a> Translator<'a> {
if let Some(clipped_range) = token_range.intersect(parent_range) {
if let Some(parent_range2) = self.text_range_for_node(parent) {
let token_range = clipped_range + parent_range2.start() - parent_range.start();
let (start, end) = self.location(token_range);
self.trap.emit_location(self.label, label, start, end)
if let Some((start, end)) = self.location(token_range) {
self.trap.emit_location(self.label, label, start, end)
}
}
}
}
Expand Down Expand Up @@ -206,7 +212,7 @@ impl<'a> Translator<'a> {
"parse_error".to_owned(),
message.clone(),
message,
location,
location.unwrap_or(UNKNOWN_LOCATION),
);
}
}
Expand Down