zig icon indicating copy to clipboard operation
zig copied to clipboard

compiler segfault using `@Type()`

Open dweiller opened this issue 2 years ago • 0 comments

Zig Version

0.10.0-dev.3986+e323cf126

Steps to Reproduce

Here is a repro case:

const std = @import("std");

fn StructArray(comptime T: type) type {
    // std.debug.assert(@alignOf(T) == 8);
    const field = std.builtin.Type.StructField{
        .name = "field",
        .field_type = T,
        .default_value = null,
        .is_comptime = false,
        // .alignment = 8,
        .alignment = @alignOf(T),
    };
    return @Type(std.builtin.Type{
        .Struct = .{
            .layout = .Auto,
            .fields = &.{field},
            .decls = &.{},
            .is_tuple = false,
        },
    });
}

const Statistics = struct {
    n_samples: usize,
};

const RunStats = StructArray(Statistics);

test "StructArray" {
    _ = RunStats;
}

Put this in a file repro.zig and run zig test repro.zig.

Expected Behavior

The test compiles and passes.

Actual Behavior

The compiler segfaults, with no helpful errors, the only output I get is Segmentation fault (core dumped).

Here are some additional notes:

  • the test compiles and passes with zig test -fstage1 repro.zig
  • the test compiles and passes when uncommenting the assertion on line 4
  • the test compiles and passes when uncommenting line 10 and commenting line 11
  • changing the type of the n_samples field to u64, or some other primitive type does not help.
  • changing the definition of Runstats to const RunStats = StructArray(usize), or some other primitive type makes the test compile and pass

dweiller avatar Sep 14 '22 02:09 dweiller