Skip to content

Commit

Permalink
Fix span calculations for errors at the end of a file with no final `…
Browse files Browse the repository at this point in the history
…\n` (FuelLabs#3066)
  • Loading branch information
mohammadfawaz authored Oct 18, 2022
1 parent 9f31df0 commit a2395b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
12 changes: 9 additions & 3 deletions sway-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ impl<'a, 'e> Parser<'a, 'e> {
let span = match self.token_trees {
[token_tree, ..] => token_tree.span(),
_ => {
// Create a new span that points to _just_ after the last parsed item
// Create a new span that points to _just_ after the last parsed item or 1
// character before that if the last parsed item is the last item in the full span.
let num_trailing_spaces =
self.full_span.as_str().len() - self.full_span.as_str().trim_end().len();
let trim_offset = if num_trailing_spaces == 0 {
1
} else {
num_trailing_spaces
};
Span::new(
self.full_span.src().clone(),
self.full_span.end() - num_trailing_spaces,
self.full_span.end() - num_trailing_spaces + 1,
self.full_span.end() - trim_offset,
self.full_span.end() - trim_offset + 1,
self.full_span.path().cloned(),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[package]]
name = 'no_newline'
source = 'root'
dependencies = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "no_newline"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
script;

use std::
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()use std::
# nextln: $()Expected an import name, group of imports, or `*`.

0 comments on commit a2395b4

Please sign in to comment.