Skip to content

improved handling of index of a sentinel-terminated array which is comptime-known to be len #3962

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

Open
andrewrk opened this issue Dec 21, 2019 · 0 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

Extracted from #3770.

When the index used for loading or storing to a sentinel-terminated slice or array is comptime-known to be equal to the length, we know at compile-time the value is the sentinel. So, loading should give a comptime value of the sentinel, with no runtime code emitted, and storing should be a no-op with a safety check to make sure the stored value is equal to the sentinel value.

const std = @import("std");
const expect = std.testing.expect;

test "comptime known load of sentinel-terminated slice" {
    // here we have a normal array
    var buf: [50]u8 = undefined;

    buf[0] = 'a';
    buf[1] = 'b';
    buf[2] = 'c';
    buf[3] = 0;

    // now we obtain a null terminated slice:
    const slice = buf[0..3 :0];

    comptime expect(slice[3] == 0);
}

The safety check thing:

test "trip runtime safety check" {
    // here we have a normal array
    var buf: [50]u8 = undefined;

    buf[0] = 'a';
    buf[1] = 'b';
    buf[2] = 'c';
    buf[3] = 0;

    // now we obtain a null terminated slice:
    const slice = buf[0..3 :0];
    var runtime_elem: u8 = 42;

    slice[3] = runtime_elem; // panic: mangled the sentinel value
}

Potentially could be further improved:

test "trip runtime safety check 2" {
    // here we have a normal array
    var buf: [50]u8 = undefined;

    buf[0] = 'a';
    buf[1] = 'b';
    buf[2] = 'c';
    buf[3] = 0;

    // now we obtain a null terminated slice:
    const slice = buf[0..3 :0];
    var runtime_elem: u8 = 42;
    var runtime_index: usize = 3;

    slice[runtime_index] = runtime_elem; // panic: mangled the sentinel value
}
@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 21, 2019
@andrewrk andrewrk added this to the 0.7.0 milestone Dec 21, 2019
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 9, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 20, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Aug 24, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jun 29, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Jan 25, 2025
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

No branches or pull requests

1 participant