Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplitIterator panics when doing SplitIterator.first() twice #22338

Open
sfiedler0 opened this issue Dec 28, 2024 · 1 comment · May be fixed by #22374
Open

SplitIterator panics when doing SplitIterator.first() twice #22338

sfiedler0 opened this issue Dec 28, 2024 · 1 comment · May be fixed by #22374
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@sfiedler0
Copy link
Contributor

Zig Version

0.14.0-dev.2569+30169d1d2

Steps to Reproduce and Observed Behavior

  1. Write some code that uses the SplitIterator (std.mem.splitAny)
  2. Call twice your split_iterator.first()
  3. Observe a panic: reached unreachable code after calling the second split_iterator.first()

Example code:

const std = @import("std");

test "this will crash" {
    var split_iterator = std.mem.splitAny(u8, "test|test|test", "|");
    std.debug.print("{s}\n", .{ split_iterator.first() });
    std.debug.print("{s}\n", .{ split_iterator.first() });
}

Expected Behavior

I expected that function to either succeed or to explicitly say it can be called only once.
The documentation says "this function always succeeds" and "call this only to get the first field".

I know I should probably have saved the value of the first call to split_iterator.first() (and reused the value), but the documentation does not say something like "Caller guarantees that this function is called only once".

@sfiedler0 sfiedler0 added the bug Observed behavior contradicts documented or intended behavior label Dec 28, 2024
@Vexu
Copy link
Member

Vexu commented Dec 28, 2024

It was added with the intention of it only being valid once which the documentation should state more clearly. It could be made to always work by having it call reset but I think that could be more unexpected.

This also applies to all other firsts in the std.mem iterators.

@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Dec 28, 2024
@Vexu Vexu added this to the 0.15.0 milestone Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants