Sitelet https://github.com/starkware-libs/cairo/pull/10103
Skip to content

fix(parser): anchor literal-validation diagnostics at the token text. - #10103

Merged
orizi merged 1 commit into
mainfrom
orizi/06-15-fix_parser_anchor_literal-validation_diagnostics_at_the_token_text
Jun 16, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-15-fix_parser_anchor_literal-validation_diagnostics_at_the_token_text

Conversation

@orizi

@orizi orizi commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes incorrect diagnostic span anchoring for string/short-string/number literal validation errors when the literal appears with leading trivia (e.g., indented on its own line). Previously, diagnostic spans were computed relative to self.offset, which includes the terminal's leading trivia width, causing the error underline to point at the wrong position in the source. Now, the span is anchored at the start of the terminal's text (after leading trivia), so the caret and underline correctly point to the literal itself.

The three take_terminal_* methods are also consolidated into a single generic take_validated_terminal helper that accepts a validation function, eliminating the repeated peek/take/diagnostic pattern.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

When a string or short-string literal appeared as the first token on a new line (i.e., with leading whitespace trivia), validation diagnostics such as "Invalid string escaping" or "Short string literals can only include ASCII characters" would point to the wrong column — offset by the width of the leading whitespace — rather than pointing directly at the literal.


What was the behavior or documentation before?

Diagnostic spans for literal validation errors were computed using self.offset and self.current_width, which included the terminal's leading trivia. This caused the error underline to be shifted left, pointing into the indentation whitespace rather than the literal text.


What is the behavior or documentation after?

Diagnostic spans are now anchored at self.offset + leading_trivia_width, so the span starts at the first character of the literal text. The underline and caret in error messages correctly point to the literal, even when it is indented or preceded by other trivia.

New test cases cover '\p' and '\u{1024}' appearing with leading trivia (literal first on its line) to confirm correct span reporting.


Related issue or discussion (if any)


Additional context

The trivia_total_width helper is used to compute the leading trivia width from the peeked token before it is consumed by self.take::<Terminal>().

spans were built from the trivia-inclusive offset/width; base them on the text (after leading trivia).
  Consolidates the three take_terminal_ validators.*
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 15, 2026 08:39
@cursor

cursor Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Parser-only diagnostic span fix and refactor with snapshot tests; no semantic or security impact.

Overview
Fixes wrong caret/underline positions for number, short-string, and string literal validation errors when the literal has leading trivia (e.g. indented on the next line after let a =).

Validation diagnostics are now anchored at the start of the terminal’s text (offset + leading_trivia_width) instead of the terminal start, so spans exclude indentation. The three take_terminal_* helpers are folded into take_validated_terminal, which takes a validation callback and applies the same span logic for Full, After, and Cursor locations.

New parser diagnostic tests cover illegal escapes and non-ASCII short strings when the literal is the first token on its line.

Reviewed by Cursor Bugbot for commit 4068f90. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eytan-starkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-parser/src/parser.rs line 3170 at r1 (raw file):

        let text = peek.text.long(self.db);
        let leading_width = trivia_total_width(self.db, &peek.leading_trivia);
        let green = self.take::<Terminal>();

Cant we get text from green? Also we only need the leading width inside the error case so we should do that inside the if

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi made 1 comment.
Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-parser/src/parser.rs line 3170 at r1 (raw file):

Previously, eytan-starkware wrote…

Cant we get text from green? Also we only need the leading width inside the error case so we should do that inside the if

unfortunately - it seems we cannot. as we know nothing about the type of the terminal at this point, since it is generic.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@eytan-starkware reviewed 1 file, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 35ac706 Jun 16, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-15-fix_parser_anchor_literal-validation_diagnostics_at_the_token_text branch June 16, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants