Skip to content

Segmentation fault when incorrectly casting with @as #16739

@christianm20358

Description

@christianm20358

Zig Version

0.12.0-dev.1+a327d8b99

Steps to Reproduce and Observed Output

I had an issue where some tests were failing to compile with unclear results, and I believe the compiler is seg-faulting when it reaches the invalid code.

const std = @import("std");

pub fn main() !void {
    const vec: @Vector(2, f32) = .{3, 4};
    const splat_multiply = vec * @as(f32, @splat(2));

    try std.testing.expect(
        std.meta.eql(
            splat_multiply,
            @Vector(2, f32){ 6, 8}
        )
    );
}

Specifically:

@as(f32, @splat(2))

is the invalid code. It successfully compiles when replaced with:

@as(@Vector(2, f32))

Example of the output when using build.zig

zig build-exe zig-test Debug native: error: the following command terminated unexpectedly:
./zig build-exe ./zig-test/src/main.zig --cache-dir ./zig-cache --global-cache-dir ~/.cache/zig --name zig-test --listen=-
Build Summary: 0/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zig-test transitive failure
   ├─ zig build-exe zig-test Debug native failure
   └─ install transitive failure
      └─ install zig-test transitive failure
         └─ zig build-exe zig-test Debug native (reused)
error: the following build command failed with exit code 1:
./zig-cache/o/c54104b2e024a8fd48d0e197aa77b784/build ./zig-test ~/.cache/zig run

When compiling with zig run on Windows, the compilation fails silently.

Trying zig run on Debian was where I found it was a segment fault.

└─$ zig run src/main.zig
Segmentation fault

I have not been able to reproduce with any other invalid usages of @as() other than with Vectors. The code segfaults even when the std.testing.expect() function is commented out.

Expected Output

A compile error and stack trace including the location of the invalid cast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions