Skip to content

fix 11027: std.bitmanip.peek does not work with ranges with slicing#11029

Open
jmdavis wants to merge 1 commit into
dlang:masterfrom
jmdavis:peek_fix
Open

fix 11027: std.bitmanip.peek does not work with ranges with slicing#11029
jmdavis wants to merge 1 commit into
dlang:masterfrom
jmdavis:peek_fix

Conversation

@jmdavis

@jmdavis jmdavis commented Jun 8, 2026

Copy link
Copy Markdown
Member

Okay. The basic issue here is that peek assumes that you can slice a range of ubyte and assign it to a static array of ubyte, which can't be done. And actually, since the code that checks for slicing has never worked, we can upgrade the template constraint to require a random access range in the case where slicing is required. Forward ranges with slicing are valid per the range API, but they're nonsensical, because if you can implement slicing, you can implement indexing (they both take indices; it's just that one returns the same range type containing multiple elements whereas the other returns a single element).

So, this fixes it so that the overload of peek that doesn't take an index works properly with ranges that have slicing, and the overload which takes an index now works with ranges (it always required slicing and didn't compile with any range that could satisfy the template constraint).

I also beefed up the tests for read a bit by including some tests for them with the fix, since they can just use the same test ranges types. read was fixed for this issue previously, but the unittest block associated with its fix only tested one range type, so this is a bit more robust. Either way, I didn't touch any existing tests.

write has essentially the same issue, but I'll create a PR for that after this has been merged (since they're separate bug reports).

@jmdavis jmdavis added Severity:Bug Fix Phobos 2 Issues and PR's specific to Phobos 2. labels Jun 8, 2026
@dlang-bot

Copy link
Copy Markdown
Contributor

Thanks for your pull request, @jmdavis!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#11029"

@jmdavis jmdavis force-pushed the peek_fix branch 2 times, most recently from 2a37721 to 5809503 Compare June 8, 2026 02:30
…licing

Okay. The basic issue here is that peek assumes that you can slice a
range of ubyte and assign it to a static array of ubyte, which can't be
done. And actually, since the code that checks for slicing has never
worked, we can upgrade the template constraint to require a random
access range in the case where slicing is required. Forward ranges with
slicing _are_ valid per the range API, but they're nonsensical, because
if you can implement slicing, you can implement indexing (they both take
indices; it's just that one returns the same range type containing
multiple elements whereas the other returns a single element).

So, this fixes it so that the overload of peek that doesn't take an
index works properly with ranges that have slicing, and the overload
which takes an index now works with ranges (it always required slicing
and didn't compile with any range that could satisfy the template
constraint).

I also beefed up the tests for read a bit by including some tests for
them with the fix, since they can just use the same test ranges types.
read was fixed for this issue previously, but the unittest block
associated with its fix only tested one range type, so this is a bit
more robust. Either way, I didn't touch any existing tests.

write has essentially the same issue, but I'll create a PR for that
after this has been merged (since they're separate bug reports).
@0xEAB 0xEAB linked an issue Jun 8, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Phobos 2 Issues and PR's specific to Phobos 2. Severity:Bug Fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std.bitmanip.peek does not work with ranges with slicing

3 participants