Skip to content

impossible outOfBounds and startGreaterThanEnd safety checks emitted for slicing many-pointers #22398

@xdBronch

Description

@xdBronch

Zig Version

0.14.0-dev.2596+e6879e99e

Steps to Reproduce and Observed Behavior

export fn slice(p: [*]const u8, end: usize) void {
    _ = p[0..end];
}

pub const Panic = struct {
    pub const call = unreachable;
    pub const sentinelMismatch = unreachable;
    pub const unwrapError = unreachable;
    pub const inactiveUnionField = unreachable;
    pub const messages = unreachable;
    pub fn outOfBounds(_: usize, _: usize) noreturn {
        @compileError("outOfBounds");
    }
    pub fn startGreaterThanEnd(_: usize, _: usize) noreturn {
        @compileError("startGreaterThanEnd");
    }
};

had to make the 2 that are triggered functions otherwise analysis stopped at startGreaterThanEnd

$ zig build-obj a.zig
a.zig:12:9: error: outOfBounds
        @compileError("outOfBounds");
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.zig:15:9: error: startGreaterThanEnd
        @compileError("startGreaterThanEnd");
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if you do a slice-by-length _ = p[0..][0..end]; then only outOfBounds is referenced

Expected Behavior

no safety checks should be referenced, both of these checks are impossible to trigger

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions