Skip to content

Add syntax warnings - #8188

Merged
laurmaedje merged 10 commits into
typst:mainfrom
isuffix:syntax-warnings
May 26, 2026
Merged

laurmaedje merged 10 commits into
typst:mainfrom
isuffix:syntax-warnings

Conversation

@isuffix

@isuffix isuffix commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

This PR allows us to add warning messages to the syntax tree generated when parsing, and it moves the warnings for ** and __ to the parser.

Since warnings do not act like errors in stopping the evaluation of the file, I've chosen to implement them as transparent wrappers around existing syntax nodes, accessible only via the errors_and_warnings method on SyntaxNode. This method was expanded from the errors method, and required adding a new Erroneous struct to differentiate between errors and warnings instead of just having a single bool for errors. And I renamed SyntaxError to SyntaxDiagnostic and gave it a boolean to distinguish the two (no enum because it felt weird to have a pure duplicate of diag::Severity housed in typst-syntax).

Also, the debug output for warnings inspired the new debug output for errors, but is a bit weirder since warnings directly contain a child node. LMK if anyone has thoughts on improving the debug outputs, or if we should just radically alter them at a later time.

The first four commits help prepare for the addition of warnings:

  • The first commit adds a test for the Debug impl of SyntaxNode which we should've had a long time ago
  • The second updates the Debug impl for error nodes to include hints, but falls back to a shorter version if the error only contains a message (no text or hints)
  • The third renames ErrorNode variables so that we can call errors err and warnings warn since calling both node would be confusing
  • Finally, the fourth commit makes a few larger changes to remove/simplify a number of the private methods on structs in node.rs that I thought deserved it. This notably includes removing the SyntaxError::new method in favor of just having SyntaxNode::error take a message as a string to create errors (since SyntaxError is renaming to SyntaxDiagnostic and because I just prefer SyntaxNode::error taking a string)

Then the fifth commit adds warnings themselves.

Note that I am also planning on separately adding an abstraction for attaching sub-ranges to diagnostics. This would allow warnings and errors to target ranges of text that aren't necessarily contained directly by a Span in the syntax tree, greatly increasing the utility of syntax warnings (since this PR otherwise has little real benefit). However I am still working on that, but I wanted to push this set of changes now since they are ready to review.

@isuffix

isuffix commented May 3, 2026

Copy link
Copy Markdown
Collaborator Author

As discussed synchronously, I have made a few changes:

  • Moved the Debug impls back to their original locations for cohesion across the codebase
  • In typst-eval, moved warnings to always be added to the Sink instead of directly returning them in the SourceResult. This required splitting the return of errors_and_warnings into two vectors for convenience. This is maybe inefficient, but it was easy and I doubt it will really matter
  • Renamed Erroneous to Diagnosis (cute!), however I am somewhat concerned that we never actually call .diagnosis() outside of node.rs, but I guess it's fine
  • Moved warnings out of the top NodeKind enum and created a separate NodeWrapper enum.

I also added a commit that renames the NodeKind enum to Node to help avoid confusion with SyntaxKind, and because it obviously represents a kind of node because it's an enum. This also works well with the new node() and node_mut() methods for descending into NodeWrapper.

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label May 4, 2026
@isuffix
isuffix force-pushed the syntax-warnings branch 3 times, most recently from 9d9d422 to d7c18a1 Compare May 5, 2026 19:13
@isuffix

isuffix commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

Oops! I wasn't careful about cleaning up from previewing docs-in-typst and jj accidentally pushed the docs website with +109,492 lines 😳 (now fixed)

@isuffix
isuffix force-pushed the syntax-warnings branch from d7c18a1 to d04e84f Compare May 5, 2026 23:16
@laurmaedje laurmaedje added syntax About syntax, parsing, etc. diagnostics Improvements to compiler errors labels May 6, 2026
@isuffix
isuffix force-pushed the syntax-warnings branch 2 times, most recently from 546516c to c880f16 Compare May 12, 2026 02:57
@saecki
saecki self-requested a review May 13, 2026 10:13

@saecki saecki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure, if there was any more feedback from Laurenz, but apart from the few remarks this looks good to me!

Could this in combination with the sub ranges be used to better report unclosed delimiters? For example adding a hint to the opening delimiter, or the location where it was popped off the stack (#5906).

///
/// Builds the string if this is an inner node.
pub fn into_text(self) -> EcoString {
// This isn't fully efficient for warnings, but the efficient

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not fully related to this PR, but is there really a benefit that into_text takes the node by value? Cloning of EcoString should be cheap, either just a bitwise copy for the inline representation up to 15 bytes, which should be a good amount of leaf nodes, and an increment of the atomic refcount for other ones. Since the returned string never seems to be modified, increasing the refcount seems like a non issue.
Most use sites of into_text seem to clone the SyntaxNode anyway, which clones the LeafNode::text, and also needlessly increments and decrements the refcount of inner and error nodes.

Maybe the functions should have more descriptive names like leaf_text and full_text to clear up the intent?

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.

I agree. I'll open another PR that renames them after this one.

I will mention that Laurenz once commented that we should really have a summarize method that would add ellipses in a smart location instead of generating the full text, perhpas based on an expected or maximum length. I would be in favor of it, but I'm not sure how exactly I would go about implementing it. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure how I'd approach this, but it would definitely be nice to have some syntax aware summary function, that'll also handle cases like: #8339

pub(super) fn is_leaf(&self) -> bool {
matches!(self.0, NodeKind::Leaf(_))
matches!(self.node(), Node::Leaf(_))
// TODO: Should we also treat non-empty errors as leaves?

@saecki saecki May 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think a problem could be that the error nodes always return SyntaxKind::Error, which could negatively impact completion?

Comment thread crates/typst-syntax/src/node.rs Resolved
@laurmaedje

Copy link
Copy Markdown
Member

Not sure, if there was any more feedback from Laurenz, but apart from the few remarks this looks good to me!

Aside from the typst_utils::debug thing, which I also noticed, I didn't have any issues with the code. However, a benchmark run revealed a ~4% slowdown on an eval-heavy document, so that's a bit of a blocker. Ian and I discussed this yesterday and I gave him access to the benchmark infra to play around with what we could do.

@isuffix
isuffix force-pushed the syntax-warnings branch from c880f16 to a8fa1a3 Compare May 17, 2026 21:11
@isuffix isuffix added the internals About internal changes to the codebase. label May 17, 2026
@isuffix
isuffix force-pushed the syntax-warnings branch from a8fa1a3 to f0cd335 Compare May 19, 2026 00:29
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels May 21, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Oh, I just saw that this was mislabelled. Are we on the same page that we'll want to change the code to avoid the perf regression in some way (e.g. something like your top-level-spans branch)?

@isuffix

isuffix commented May 22, 2026

Copy link
Copy Markdown
Collaborator Author

Yes exactly. I've been a bit busy this week, but have been planning to bring the commits from that branch into this PR, just been doing some cleanup on it.

@isuffix

isuffix commented May 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

I've added the four commits from my top-level-spans branch which should resolve the performance regression. The first is a small change I wanted to do, the second and third were actually just part of #8254 that needed to come earlier to work well, and the fourth is the change that moves the Span and SyntaxKind to the top level of SyntaxNode.

The reason SyntaxKind has to be inside the enum variants (not a field of SyntaxNode like the Span) is so that Rust will optimize the representation to store the kind and the enum's tag next to each other using the padding bytes. If we instead store the kind outside the enum, Rust doesn't do this and we add 8 bytes to the representation. This does lead to an annoying situation where warnings wrap the Node type, but also store their own top-level SyntaxKind, which means they actually duplicate the kind. But as I explain in the comment, trying to deduplicate the syntax kinds would require a whole other enum type, which makes mutable access too painful in my opinion.

This should now be ready to review.

@laurmaedje
laurmaedje merged commit 9f65176 into typst:main May 26, 2026
9 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Thank you!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label May 26, 2026
@isuffix
isuffix deleted the syntax-warnings branch May 26, 2026 16:01
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 4, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/typst.ts that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 9, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 10, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
Part of the Typst 0.15 cleanup stack. This slice covers diagnostics/span
migration and the minimal query prerequisites needed before the later
export and text accessor slices.

## Upstream Typst Changes

- [`typst/typst#8052`](typst/typst#8052) makes
diagnostic hints carry span metadata. Tinymist renders the hint message
payload instead of formatting the whole spanned wrapper.
- [`typst/typst#8254`](typst/typst#8254) changes
diagnostic, user-action, and test-range span handling. Tinymist updates
LSP diagnostics, user-action conversion, and range-sensitive tests for
the new span shape.
- [`typst/typst#8188`](typst/typst#8188) changes
syntax diagnostics. Tinymist updates syntax matchers and diagnostics
conversion paths that inspect syntax-side errors.
- [`typst/typst#8307`](typst/typst#8307) changes
bibliography lookup. Tinymist now finds bibliography elements through
the document introspector and packs them back to `BibliographyElem`.
- Typst 0.15 deprecates rooted virtual-path access through
`as_rooted_path`; diagnostics now use the slash-form virtual path
accessor for extension checks.

## Tinymist Changes

- Updates `tinymist-world` diagnostic rendering for spanned hint values.
- Updates `tinymist-query`, `tinymist-analysis`, `tinymist-lint`, and
debug-facing syntax diagnostics for the new span and syntax diagnostic
APIs.
- Updates user-action span conversion and affected test ranges.
- Updates code-context path evaluation to construct `RangeMapper` from
an iterator, matching the lint expectations of the current toolchain.
- Carries the small bibliography lookup prerequisite in this slice so
later query-oriented slices build on the updated Typst API.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin pushed a commit to Myriad-Dreamin/typst.ts that referenced this pull request Jun 15, 2026
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
Part of the Typst 0.15 cleanup stack. This slice covers diagnostics/span
migration and the minimal query prerequisites needed before the later
export and text accessor slices.

## Upstream Typst Changes

- [`typst/typst#8052`](typst/typst#8052) makes
diagnostic hints carry span metadata. Tinymist renders the hint message
payload instead of formatting the whole spanned wrapper.
- [`typst/typst#8254`](typst/typst#8254) changes
diagnostic, user-action, and test-range span handling. Tinymist updates
LSP diagnostics, user-action conversion, and range-sensitive tests for
the new span shape.
- [`typst/typst#8188`](typst/typst#8188) changes
syntax diagnostics. Tinymist updates syntax matchers and diagnostics
conversion paths that inspect syntax-side errors.
- [`typst/typst#8307`](typst/typst#8307) changes
bibliography lookup. Tinymist now finds bibliography elements through
the document introspector and packs them back to `BibliographyElem`.
- Typst 0.15 deprecates rooted virtual-path access through
`as_rooted_path`; diagnostics now use the slash-form virtual path
accessor for extension checks.

## Tinymist Changes

- Updates `tinymist-world` diagnostic rendering for spanned hint values.
- Updates `tinymist-query`, `tinymist-analysis`, `tinymist-lint`, and
debug-facing syntax diagnostics for the new span and syntax diagnostic
APIs.
- Updates user-action span conversion and affected test ranges.
- Updates code-context path evaluation to construct `RangeMapper` from
an iterator, matching the lint expectations of the current toolchain.
- Carries the small bibliography lookup prerequisite in this slice so
later query-oriented slices build on the updated Typst API.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
Part of the Typst 0.15 cleanup stack. This slice covers diagnostics/span
migration and the minimal query prerequisites needed before the later
export and text accessor slices.

## Upstream Typst Changes

- [`typst/typst#8052`](typst/typst#8052) makes
diagnostic hints carry span metadata. Tinymist renders the hint message
payload instead of formatting the whole spanned wrapper.
- [`typst/typst#8254`](typst/typst#8254) changes
diagnostic, user-action, and test-range span handling. Tinymist updates
LSP diagnostics, user-action conversion, and range-sensitive tests for
the new span shape.
- [`typst/typst#8188`](typst/typst#8188) changes
syntax diagnostics. Tinymist updates syntax matchers and diagnostics
conversion paths that inspect syntax-side errors.
- [`typst/typst#8307`](typst/typst#8307) changes
bibliography lookup. Tinymist now finds bibliography elements through
the document introspector and packs them back to `BibliographyElem`.
- Typst 0.15 deprecates rooted virtual-path access through
`as_rooted_path`; diagnostics now use the slash-form virtual path
accessor for extension checks.

## Tinymist Changes

- Updates `tinymist-world` diagnostic rendering for spanned hint values.
- Updates `tinymist-query`, `tinymist-analysis`, `tinymist-lint`, and
debug-facing syntax diagnostics for the new span and syntax diagnostic
APIs.
- Updates user-action span conversion and affected test ranges.
- Updates code-context path evaluation to construct `RangeMapper` from
an iterator, matching the lint expectations of the current toolchain.
- Carries the small bibliography lookup prerequisite in this slice so
later query-oriented slices build on the updated Typst API.

---------

Co-authored-by: paran3xus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Improvements to compiler errors internals About internal changes to the codebase. syntax About syntax, parsing, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants