Skip to content

Add sub-ranges for diagnostics and hints - #8254

Merged
laurmaedje merged 5 commits into
typst:mainfrom
isuffix:sub-ranges
May 28, 2026
Merged

laurmaedje merged 5 commits into
typst:mainfrom
isuffix:sub-ranges

Conversation

@isuffix

@isuffix isuffix commented May 12, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds sub-ranges for diagnostics, although does not add any uses of them yet. A following PR will add warnings for raw syntax that will make use of sub-ranges and sub-ranges on hints.

This PR is dependent on #8188. The first new commit is "Don't use raw range spans for imported files in definition.rs"

Why are sub-ranges necessary?
When we assign spans to a concrete syntax tree, we assign targetable numbers that correspond to a range in the source. However, spans only exist on full nodes in the tree, and do not allow targeting parts of a node, or ranges of text across multiple nodes. Adding sub-ranges gives us much more flexibility to create good diagnostic messages that target exactly the desired text.

Why not use raw-range spans instead?
Sub-ranges are relative to the overall range for a span so they aren't invalidated when we insert new text into the syntax tree when parsing incrementally. Absolute raw-ranges would need to be searched for and globally updated on every reparse.

What else changed?

  • The new interface in synthesize_with evolves a lot over all of the commits, although I am open to changing it. However, leaving InnerNode::synthesize_with as a separate function caused too many non-local changes, so I just inlined it to synthesize_with.
  • I updated the RangeMapper quite a bit and notably removed the Option from the range return by using partition_point and returning a result on construction if any invariants weren't followed.
  • I added the SpanNumber type that solely interacts with the Source::range since sub-ranges add a new way to produce ranges that would have made Source::range taking a whole Span easy to use incorrectly.
  • SpanNumber also made it easier to distinguish the kinds of LinkedNode::find operation, and I made it work for raw-range spans.
  • Of course, there needed to be a way to get a SpanNumber, and I settled on a .get() method returning the new unpacked SpanKind enum that can be explicitly matched against.
  • Sub-ranges get their own type as SubRange and use a NonZeroU32 for the end index for null-optimization and to ensure they are non-empty ranges. Most uses take an Option<SubRange> which is conviently produced by SubRange::new requiring a non-empty range.
  • However, sub-ranges are not meant to be stored in the document content, so I added DiagSpan and DiagSpanKind to distinguish diagnostic spans with sub-ranges from normal spans.
  • DiagSpan containing an extra 64 bits also allows us to store larger ranges than just 23 bits for externally loaded files by storing a 46-bit start index within the Span and another 46-bit end index in the extra space.
  • And this comes with no compromise for raw-range span sizes except fixing a bug where the start index could be 2^23 instead of saturating at 2^23-1 like the end index.
  • In the syntax tree, only warnings get sub-ranges since they wrap other nodes instead of encompassing a node entirely like errors.
  • However in the final commit, both warnings and errors are allowed sub-ranges on hints.


/// The possible kinds of span.
#[derive(Debug)]
pub enum SpanKind {

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.

The SpanKind is a nice touch. The previous way of checking for the different span types felt a little un-rusty in some places :)

@isuffix isuffix added diagnostics Improvements to compiler errors internals About internal changes to the codebase. syntax About syntax, parsing, etc. labels May 17, 2026
@isuffix
isuffix force-pushed the sub-ranges branch 3 times, most recently from 6bbcda6 to fb661c6 Compare May 23, 2026 15:44
@isuffix isuffix mentioned this pull request May 23, 2026
@isuffix

isuffix commented May 26, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased off main

Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
Comment thread crates/typst-syntax/src/span.rs Resolved
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label May 26, 2026
@isuffix

isuffix commented May 28, 2026

Copy link
Copy Markdown
Collaborator Author

With the new commit, this should be ready.

@laurmaedje

Copy link
Copy Markdown
Member

For fast-forward merging, it's a bit unfortunate if the last two commits go back and forth on the hints representation. I would suggest squashing the last two.

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label May 28, 2026
@laurmaedje
laurmaedje merged commit 2016791 into typst:main May 28, 2026
9 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Thank you, nice work! :)

@isuffix
isuffix deleted the sub-ranges branch May 28, 2026 16:54
ParaN3xus added a commit to ParaN3xus/typst.ts 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 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 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
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 10, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 10, 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 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 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 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 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