Fix panic when getting the line, column of diagnostic with invalid span

Introduced in cbc0c790a3
This commit is contained in:
Olivier Goffart 2023-04-19 12:35:17 +02:00
parent 84c4f817f1
commit 4076df928d
2 changed files with 8 additions and 0 deletions

View file

@ -304,6 +304,8 @@ SCENARIO("Component Compiler")
REQUIRE(diags.size() == 1); REQUIRE(diags.size() == 1);
REQUIRE(diags[0].message.starts_with("Could not load")); REQUIRE(diags[0].message.starts_with("Could not load"));
REQUIRE(diags[0].line == 0);
REQUIRE(diags[0].column == 0);
} }
SECTION("Compile from path") SECTION("Compile from path")

View file

@ -208,6 +208,7 @@ pub struct Diagnostic {
level: DiagnosticLevel, level: DiagnosticLevel,
} }
//NOTE! Diagnostic is re-exported in the public API of the interpreter
impl Diagnostic { impl Diagnostic {
/// Return the level for this diagnostic /// Return the level for this diagnostic
pub fn level(&self) -> DiagnosticLevel { pub fn level(&self) -> DiagnosticLevel {
@ -220,7 +221,12 @@ impl Diagnostic {
} }
/// Returns a tuple with the line (starting at 1) and column number (starting at 1) /// Returns a tuple with the line (starting at 1) and column number (starting at 1)
///
/// Can also return (0, 0) if the span is invalid
pub fn line_column(&self) -> (usize, usize) { pub fn line_column(&self) -> (usize, usize) {
if !self.span.span.is_valid() {
return (0, 0);
}
let offset = self.span.span.offset; let offset = self.span.span.offset;
match &self.span.source_file { match &self.span.source_file {