Sitelet https://github.com/starkware-libs/cairo/commit/8a7a46a8d15dd7069b475fc935ec66c9c44c2f1f
Skip to content

Commit 8a7a46a

Browse files
committed
bugfix(parser): Skipped-token diagnostic spans the token, not its leading comment.
skip_until computed the "Skipped tokens" span from self.offset, which after take_raw() points at the start of the skipped token's leading trivia. A skipped token preceded by a `//` line comment therefore had its diagnostic underline the comment instead of the token. Advance past the leading trivia (text_start = offset + leading_trivia_width) for both the span start and end, mirroring append_skipped_token_to_pending_trivia. Adds regression tests to diagnostics/skipped_tokens.
1 parent 5051f92 commit 8a7a46a

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎crates/cairo-lang-parser/src/parser.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,14 +3659,14 @@ impl<'a, 'mt> Parser<'a, 'mt> {
36593659
let mut diag_start = None;
36603660
let mut diag_end = None;
36613661
while !should_stop(self.peek().kind) {
3662-
let terminal = self.take_raw();
3663-
diag_start.get_or_insert(self.offset);
3664-
diag_end =
3665-
Some(self.offset.add_width(TextWidth::from_str(terminal.text.long(self.db))));
3666-
3667-
self.pending_trivia.extend(terminal.leading_trivia);
3668-
self.pending_trivia.push(TokenSkipped::new_green(self.db, terminal.text).into());
3669-
self.pending_trivia.extend(terminal.trailing_trivia);
3662+
let LexerTerminal { leading_trivia, text, trailing_trivia, .. } = self.take_raw();
3663+
let text_start = self.offset.add_width(trivia_total_width(self.db, &leading_trivia));
3664+
diag_start.get_or_insert(text_start);
3665+
diag_end = Some(text_start.add_width(TextWidth::from_str(text.long(self.db))));
3666+
3667+
self.pending_trivia.extend(leading_trivia);
3668+
self.pending_trivia.push(TokenSkipped::new_green(self.db, text).into());
3669+
self.pending_trivia.extend(trailing_trivia);
36703670
}
36713671
if let (Some(diag_start), Some(diag_end)) = (diag_start, diag_end) {
36723672
Err(SkippedError(TextSpan::new(diag_start, diag_end)))

‎crates/cairo-lang-parser/src/parser_test_data/diagnostics/skipped_tokens‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,43 @@ error[E1000]: Skipped tokens. Expected: Const/Enum/ExternFunction/ExternType/Fun
116116
--> dummy_file.cairo:3:9
117117
tokens fn foo() {}
118118
^
119+
120+
//! > ==========================================================================
121+
122+
//! > Skipped token whose leading trivia is a full-line comment: span points at the token, not the comment.
123+
124+
//! > test_runner_name
125+
get_diagnostics
126+
127+
//! > cairo_code
128+
fn foo()
129+
// a leading comment
130+
)
131+
{
132+
}
133+
134+
//! > expected_diagnostics
135+
error[E1000]: Skipped tokens. Expected: '{'.
136+
--> dummy_file.cairo:3:5
137+
)
138+
^
139+
140+
//! > ==========================================================================
141+
142+
//! > Test skipped tokens with a leading comment span the token, not the comment (skip_until).
143+
144+
//! > test_runner_name
145+
get_diagnostics
146+
147+
//! > cairo_code
148+
fn foo()
149+
// explanatory comment
150+
)
151+
{
152+
}
153+
154+
//! > expected_diagnostics
155+
error[E1000]: Skipped tokens. Expected: '{'.
156+
--> dummy_file.cairo:3:1
157+
)
158+
^

0 commit comments

Comments
 (0)