Skip to content

Coercion to pointer to vector segfaults at runtime rather than catching the alignment issue at comptime #16997

Description

@hmusgrave

Zig Version

0.12.0-dev.86+197d9a9eb

Steps to Reproduce and Observed Behavior

It looks like coercion involving pointer-of-vector types doesn't have the correct alignment in its calculations. Zig normally catches potentially unsafe coercion at comptime, so it's weird that this sort of code could segfault.

test "segfaults more often than not" {
    var x: [16]f32 = undefined;
    var y: *[1]@Vector(16, f32) = &x;
    for (y) |*v|
        v.* = @splat(0);
}

test "fails at comptime, as it should" {
    var x: [16]f32 = undefined;
    // cast increases pointer alignment
    var y: *[1]@Vector(16, f32) = @ptrCast(&x);
    _ = y;
}

test "also fails at comptime since the coercion doesn't involve vectors" {
    var x: u128 = 0;
    var y: *align(64) u128 = &x;
    _ = y;
}

Expected Behavior

All three of those tests should fail to compile because there's a potentially unsafe coercion from a less aligned type to a more aligned type.

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Aug 27, 2023
  2. added
    frontendTokenization, parsing, AstGen, Sema, and Liveness.
    on Sep 7, 2023
  3. added this to the 0.13.0 milestone on Sep 7, 2023
  4. modified the milestones: 0.14.0, 0.15.0 on Feb 10, 2025
  5. Vexu commented on Nov 12, 2025

    @Vexu
    Member

    The array to vector coercion was removed in #25186 and the other two cases now require @alignCast:

    a.zig:35:35: error: expected type '*[1]@Vector(16, f32)', found '*[16]f32'
        var y: *[1]@Vector(16, f32) = &x;
                                      ^~
    a.zig:35:35: note: pointer type child '[16]f32' cannot cast into pointer type child '[1]@Vector(16, f32)'
    a.zig:35:35: note: array of length 16 cannot cast into an array of length 1
    a.zig:44:35: error: @ptrCast increases pointer alignment
        var y: *[1]@Vector(16, f32) = @ptrCast(&x);
                                      ^~~~~~~~~~~~
    a.zig:44:44: note: '*[16]f32' has alignment '4'
        var y: *[1]@Vector(16, f32) = @ptrCast(&x);
                                               ^~
    a.zig:44:35: note: '*[1]@Vector(16, f32)' has alignment '32'
    a.zig:44:35: note: use @alignCast to assert pointer alignment
    a.zig:50:30: error: expected type '*align(64) u128', found '*u128'
        var y: *align(64) u128 = &x;
                                 ^~
    a.zig:50:30: note: pointer alignment '16' cannot cast into pointer alignment '64'
    
  6. modified the milestones: urgent, 0.16.0 on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions