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

disallow slicing many-pointer with sentinel without end index #22345

Open
Techatrix opened this issue Dec 29, 2024 · 3 comments · May be fixed by #22372
Open

disallow slicing many-pointer with sentinel without end index #22345

Techatrix opened this issue Dec 29, 2024 · 3 comments · May be fixed by #22372
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@Techatrix
Copy link
Contributor

Techatrix commented Dec 29, 2024

The following code currently compiles without errors:

const std = @import("std");
const assert = std.debug.assert;

test {
    var foo: [*]const u8 = &.{ 1, 2 };
    _ = foo[0.. :0];
    comptime assert(@TypeOf(foo[0.. :0]) == [*]const u8);
}

The sentinel expression is being analyzed but has does nothing.

I can think of two possible ways to resolve this:

  • disallow ptr[start.. :sentinel] on a multi item pointer (or C pointer)
  • resolve to [*:0]const u8

The problem with the second option is that it is not possible to implement a safety check for it.

Related:

@Rexicon226
Copy link
Contributor

disallow slicing with a sentinel on a multi item pointer (or C pointer)

foo[0..10 :0] this is considered slicing with a sentinel on a multi-item pointer. There's absolutely no reason it shouldn't be allowed.
However slicing a multi-item pointer where end_is_len is true, with a sentinel, I agree doesn't make sense and shouldn't be allowed.

@Techatrix
Copy link
Contributor Author

Techatrix commented Dec 29, 2024

foo[0..10 :0] this is considered slicing with a sentinel on a multi-item pointer. There's absolutely no reason it shouldn't be allowed.

I meant to focus on slicing without an end index. I will clarify the issue description.

@Techatrix Techatrix changed the title slicing with a sentinel on a multi item pointer will ignore the sentinel open slicing with a sentinel on a multi item pointer will ignore the sentinel Dec 29, 2024
@andrewrk
Copy link
Member

Thanks for the writeup. Let's go with the conservative strategy - disallow it. This choice reflects the goal of minimizing the amount of uncheckable illegal behavior that the language allows.

@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Dec 29, 2024
@andrewrk andrewrk added this to the 0.15.0 milestone Dec 29, 2024
@mlugg mlugg changed the title open slicing with a sentinel on a multi item pointer will ignore the sentinel disallow slicing many-pointer with sentinel without end index Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants