Skip to content

Fix incorrect mask in BufferChunks::next - #57544

Merged
Veykril merged 2 commits into
zed-industries:mainfrom
galenelias:fixBufferChunksMask
Jun 15, 2026
Merged

Fix incorrect mask in BufferChunks::next#57544
Veykril merged 2 commits into
zed-industries:mainfrom
galenelias:fixBufferChunksMask

Conversation

@galenelias

@galenelias galenelias commented May 22, 2026

Copy link
Copy Markdown
Contributor

I noticed while working on a personal project which derives some logic from the core Zed stack that I was getting very unexpected results out of BufferChunks::next when walking through formatted chunks.

The code calculates the mask for the tabs, chars, and newlines masks incorrectly, making the mask far too large when we are extracting chunks when chunk_start != 0.

I believe the reason that this isn't a problem in Zed is that the InlayMap ends up re-masks all the bitmasks before passing things up the stack, so covers up the problem, preventing it from causing any damage. It appears this isn't caught in tests because this only happens when extracting chunks with formatting, during which we offset our retrieval by next_capture_start, and the current chunks tests don't use formatting.

I think the fix is sound. The test is maybe not super ideal, but I wanted to demonstrate the issue in the pull request.

If we process the following text by chunks:

use std::cmp::Eq;

The first chunk is 'use ' with the chars mask being 0b1111. The second chunk is 'std' with the chars mask being 0b1111111 when it should just be 0b111.

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Release Notes:

  • N/A

@cla-bot

cla-bot Bot commented May 22, 2026

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Galen Elias.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

I noticed while working on a personal project which derives some logic
from the core Zed stack that I was getting very unexpected results out
of BufferChunks::next when walking through formatted chunks.

The code calculates the mask for the `tabs`, `chars`, and `newlines`
masks incorrectly, making the mask far too large when we are extracting
chunks when chunk_start != 0.

I believe the reason that this isn't a problem in Zed is that the
InlayMap ends up re-masks all the bitmasks before passing things up the
stack, so covers up the problem, preventing it from causing any damage.
It appears this isn't caught in tests because this only happens when
extracting chunks with formatting, during which we offset our retrieval
by next_capture_start, and the current chunks tests don't use
formatting.

I think the fix is sound.  The test is maybe not super ideal, but I
wanted to demonstrate the issue in the pull request.

If we process the following text by chunks:
```rust
use std::cmp::Eq;
```

The first chunk is 'use ' with the chars mask being 0b1111.
The second chunk is 'std' with the chars mask being 0b1111111 when it
should just be 0b111.
@galenelias
galenelias force-pushed the fixBufferChunksMask branch from 8ef93b9 to ce10a1f Compare May 22, 2026 23:55
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 22, 2026
@smitbarmase smitbarmase added the area:editor Feedback for code editing, formatting, editor iterations, etc label May 25, 2026

@Veykril Veykril 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.

Nice catch, thanks!

@Veykril Veykril self-assigned this Jun 15, 2026
@Veykril
Veykril enabled auto-merge June 15, 2026 08:08
@zed-industries-bot

zed-industries-bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor
Warnings
⚠️

This PR is missing release notes.

Please add a "Release Notes" section that describes the change:

Release Notes:

- Added/Fixed/Improved ...

If your change is not user-facing, you can use "N/A" for the entry:

Release Notes:

- N/A

Generated by 🚫 dangerJS against 54b0350

@Veykril
Veykril added this pull request to the merge queue Jun 15, 2026
Merged via the queue into zed-industries:main with commit 18c98b0 Jun 15, 2026
32 checks passed
This was referenced Jun 18, 2026
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
I noticed while working on a personal project which derives some logic
from the core Zed stack that I was getting very unexpected results out
of BufferChunks::next when walking through formatted chunks.

The code calculates the mask for the `tabs`, `chars`, and `newlines`
masks incorrectly, making the mask far too large when we are extracting
chunks when chunk_start != 0.

I believe the reason that this isn't a problem in Zed is that the
InlayMap ends up re-masks all the bitmasks before passing things up the
stack, so covers up the problem, preventing it from causing any damage.
It appears this isn't caught in tests because this only happens when
extracting chunks with formatting, during which we offset our retrieval
by next_capture_start, and the current chunks tests don't use
formatting.

I think the fix is sound. The test is maybe not super ideal, but I
wanted to demonstrate the issue in the pull request.

If we process the following text by chunks:
```rust
use std::cmp::Eq;
```

The first chunk is 'use ' with the chars mask being 0b1111. The second
chunk is 'std' with the chars mask being 0b1111111 when it should just
be 0b111.

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:
 - N/A

Co-authored-by: Lukas Wirth <lukas@zed.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:editor Feedback for code editing, formatting, editor iterations, etc cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants