Skip to content

Stage2: Generated type from @Type is undefined #12521

Open

Description

@Avokadoen

Zig Version

0.10.0-dev.3659+e5e6eb983

Steps to Reproduce

Run zig test file.zig where file has the following zig code:

const std = @import("std");

fn Bomb(types: []const type) type {
    const TriggerBombType = blk: {
        const Type = std.builtin.Type;

        var struct_fields: [types.len]Type.StructField = undefined;
        inline for (types) |T, i| {
            const ArrT = std.ArrayList(T);
            var num_buf: [8]u8 = undefined;
            struct_fields[i] = .{
                .name = std.fmt.bufPrint(&num_buf, "{d}", .{i}) catch unreachable,
                .field_type = if (@sizeOf(T) > 0) ArrT else T,
                .default_value = null,
                .is_comptime = false,
                .alignment = if (@sizeOf(T) > 0) @alignOf(ArrT) else 0,
            };
        }

        const RtrTypeInfo = Type{ .Struct = .{
            .layout = .Auto,
            .fields = &struct_fields,
            .decls = &[0]Type.Declaration{},
            .is_tuple = true,
        } };

        break :blk @Type(RtrTypeInfo);
    };
    @compileLog(@sizeOf(TriggerBombType)); // boom (segmentation fault)

    return struct {};
}

test "trigger bug" {
    const A = struct {};
    const B = struct { b: i32 };

    const Explosion = Bomb(&[_]type{ A, B });
    _ = Explosion;
}

Expected Behavior

Compiler to print the size of generated type and fail because of the compileLog

Actual Behavior

$ zig test file.zig
Segmentation fault

Also in the original code where i detected this issue (ecez) I get the following error:

zig test src/archetype.zig 
LLVM Emit Object... warning: stack frame size (8589934760) exceeds limit (4294967295) in function 'archetype.test.init() produce expected arche type'
Test [1/17] test.init() produce expected arche type... Stack Overflow
error: the following test command failed with exit code 3:
src\zig-cache\o\f185d8eaf05ee1cfa06939f009e64792\test.exe C:\Program Files\Zig\zig.exe

presumably because the type generated with a similar mechanism is garbage

Activity

  1. added
    bug
    on Aug 20, 2022
  2. added
    frontend
    on Aug 21, 2022
  3. added this to the 0.10.0 milestone on Aug 21, 2022
  4. modified the milestones: 0.10.0, 0.10.1 on Oct 12, 2022
  5. Avokadoen commented on Jan 5, 2023

    @Avokadoen
    Author

    @Vexu @andrewrk I have never contributed to the compiler directly before, but if I got a few hints of what the issue might be, then I would be willing to attempt to solve this issue as it has blocked my progress for a while now.

  6. Vexu commented on Jan 5, 2023

    @Vexu
    Member

    The bug has already been fixed and the only thing remaining for this issue is to add a test.

  7. Avokadoen commented on Jan 5, 2023

    @Avokadoen
    Author

    That is great news! Cheers! 🎉

  8. modified the milestones: 0.10.1, 0.11.0 on Jan 10, 2023
  9. modified the milestones: 0.11.0, 0.11.1 on Jul 20, 2023
  10. modified the milestones: 0.11.1, 0.12.0, 0.13.0 on Jan 29, 2024
  11. modified the milestones: 0.14.0, 0.15.0 on Feb 10, 2025
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

    bugfrontend

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions