-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
By-length slicing with sentinel in first slice doesn't do a by-length slice #22371
Comments
I was aware that it would no longer emit To verify my previous statement, I modified AstGen to panic when encountering Patch to detect
|
I'm not particularly aware of real-world usecases - the place noticed it was in the behaviour tests for slicing. I would also expect that this pattern is rare in real code, the reason being that a sentinel in the first slicing operation can't affect the result/type of the expression and provides no useful information to the compiler (other than a clue that the author may have made an error). I would imagine the way this pattern would emerge in a code base is a pre-existing, open sentinel-terminated slice gets reworked to be done by-length by adding the second slice operation.
Yes - it shouldn't have semantic effects, the semantic effects should be in moving from |
Zig Version
0.14.0-dev.2571+01081cc8e
Steps to Reproduce and Observed Behavior
Put this snippet in
by-length.zig
and (with a debug build of the compiler) run
zig ast-check -t by-length.zig` | grep "slice_"
to see that the above zig code is producing
slice_sentinel
andslice_end
ZIR nodes rather than aslice_length
ZIR node.Expected Behavior
I believe the above pattern should produce a
slice_length
ZIR node. This behaviour was changed in #22344 which disabled the by-length slice optimisation when performing a slice such asa[0.. :sentinel][0..len]
. From the description of the PR it's unclear whether this was intended or not as the mentioned motivation for the change was to prevent allowing nonsense likeUnfortunately I didn't document in the original by-length slice PR the reason why a sentinel in the first slice above was previously ignored, but if I remember correctly it was because it doesn't have any bearing on the result or type of the whole slicing expression, and I suppose I didn't consider that the above snippet would have needed an extra sentinel check.
If we want the allow
To benefit from by-length slicing then we will need to rework the AstGen fix from #22344 to produce ZIR like it did previously and rather add extra sentinel checks in Sema when either when
by_length
is true inanalyzeSlice
or inzirSliceLength
. As a side note it may be worth seeing if there is way to to simplifyanalyzeSlice
as I think it's getting a bit hard to follow (especially with the mentioned sentinel checks added).If the plan is to disable by-length slicing in the above example then it may be worth mentioning this edge case in the language reference and 0.14 release notes, but I believe this would be an unnecessary edge case in the language.
cc @Techatrix
The text was updated successfully, but these errors were encountered: