Skip to content

Commit

Permalink
Fix SourceMap::start_point for empty spans
Browse files Browse the repository at this point in the history
When the span is empty, it doesn't really have a first character. Even
worse, when it's empty at the end of the file, adding a byte offset will
make it be out of bounds. So we just return the empty span in these
cases.
  • Loading branch information
Noratrieb committed Feb 22, 2023
1 parent 323c2df commit e184610
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,12 @@ impl SourceMap {
}

/// Returns a new span representing just the first character of the given span.
/// When the span is empty, the same empty span is returned.
pub fn start_point(&self, sp: Span) -> Span {
if sp.is_empty() {
return sp;
}

let width = {
let sp = sp.data();
let local_begin = self.lookup_byte_offset(sp.lo);
Expand Down

0 comments on commit e184610

Please sign in to comment.