Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crash on @typeInfo call inside usingnamespace function(@This()) #14322

Closed
Flaminator opened this issue Jan 14, 2023 · 1 comment
Closed
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@Flaminator
Copy link

Zig Version

0.11.0-dev.1300+7cb2f9222

Steps to Reproduce and Observed Behavior

Trying to compile the following code snippet triggers a compiler crash

// main.zig
pub fn Def(comptime T: type) type
{
	_ = @typeInfo(T);
	return struct{};
}

pub fn main() !void {	
}

usingnamespace Def(@This());

On windows the compiler crashes and just returns an error 253 without message.
Using a debug build of the compiler it returns the following error log:

PS D:\Programming\Zig\Projects\bug> ..\..\zig\stage3\bin\zig.exe build run
thread 7876 panic: reached unreachable code
Analyzing src\main.zig: main.zig:Def
      %5 = dbg_block_begin()
      %6 = dbg_stmt(3, 2)
    > %7 = type_info(%3)
      %8 = ensure_result_non_error(%7)
      %9 = dbg_stmt(4, 2)
      %10 = ret_type()
      %11 = extended(struct_decl(func, Auto, {}, {}, {})
      %12 = as_node(%10, %11)
      %13 = restore_err_ret_index(@Zir.Inst.Ref.none, @Zir.Inst.Ref.none)
      %14 = dbg_stmt(4, 2)
      %15 = ret_node(%12)
      %16 = dbg_block_end()
      %17 = restore_err_ret_index(%4, @Zir.Inst.Ref.none)
      %18 = break(%4, @Zir.Inst.Ref.void_value)
    For full context, use the command
      zig ast-check -t src\main.zig

  in src\main.zig: main.zig:Def
    > %4 = block({%5..%18})
  in src\main.zig: main.zig:usingnamespace_0
    > %34 = call(.compile_time, %32, [
        {%35, %36},
      ])

D:\Programming\Zig\zig\src\Module.zig:4000:25: 0x7ff7aa76aa8e in ensureDeclAnalyzed (zig.exe.obj)
        .in_progress => unreachable,
                        ^
D:\Programming\Zig\zig\src\Sema.zig:16261:44: 0x7ff7ab4bc234 in typeInfoNamespaceDecls (zig.exe.obj)
            try sema.mod.ensureDeclAnalyzed(decl_index);
                                           ^
D:\Programming\Zig\zig\src\Sema.zig:16227:69: 0x7ff7ab18601a in typeInfoDecls (zig.exe.obj)
        try sema.typeInfoNamespaceDecls(block, decls_anon_decl.arena(), some, &decl_vals, &seen_namespaces);
                                                                    ^
D:\Programming\Zig\zig\src\Sema.zig:16131:102: 0x7ff7aad5124c in zirTypeInfo (zig.exe.obj)
            const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, struct_ty.getNamespace());
                                                                                                     ^
D:\Programming\Zig\zig\src\Sema.zig:984:66: 0x7ff7aaad0ffe in analyzeBodyInner (zig.exe.obj)
            .type_info                    => try sema.zirTypeInfo(block, inst),
                                                                 ^
D:\Programming\Zig\zig\src\Sema.zig:810:45: 0x7ff7aa902300 in analyzeBodyBreak (zig.exe.obj)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
D:\Programming\Zig\zig\src\Sema.zig:1461:62: 0x7ff7aaadd5cd in analyzeBodyInner (zig.exe.obj)
                const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
                                                             ^
D:\Programming\Zig\zig\src\Sema.zig:793:30: 0x7ff7aacb485e in analyzeBody (zig.exe.obj)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
D:\Programming\Zig\zig\src\Sema.zig:6690:55: 0x7ff7ab142b22 in analyzeCall (zig.exe.obj)
                sema.analyzeBody(&child_block, fn_info.body) catch |err| switch (err) {
                                                      ^
D:\Programming\Zig\zig\src\Sema.zig:6212:32: 0x7ff7aad0fffe in zirCall (zig.exe.obj)
        return sema.analyzeCall(block, func, func_src, call_src, modifier, ensure_result_used, resolved_args, bound_arg_src);
                               ^
D:\Programming\Zig\zig\src\Sema.zig:911:62: 0x7ff7aaaccb81 in analyzeBodyInner (zig.exe.obj)
            .call                         => try sema.zirCall(block, inst),
                                                             ^
D:\Programming\Zig\zig\src\Sema.zig:810:45: 0x7ff7aa902300 in analyzeBodyBreak (zig.exe.obj)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
D:\Programming\Zig\zig\src\Module.zig:4435:50: 0x7ff7aa8fe5a5 in semaDecl (zig.exe.obj)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
                                                 ^
D:\Programming\Zig\zig\src\Module.zig:4047:38: 0x7ff7aa76ae1e in ensureDeclAnalyzed (zig.exe.obj)
    const type_changed = mod.semaDecl(decl_index) catch |err| switch (err) {
                                     ^
D:\Programming\Zig\zig\src\Compilation.zig:3213:38: 0x7ff7aa8dea96 in processOneJob (zig.exe.obj)
            module.ensureDeclAnalyzed(decl_index) catch |err| switch (err) {
                                     ^
D:\Programming\Zig\zig\src\Compilation.zig:3083:30: 0x7ff7aa7a9ec3 in performAllTheWork (zig.exe.obj)
            try processOneJob(comp, work_item);
                             ^
D:\Programming\Zig\zig\src\Compilation.zig:2385:31: 0x7ff7aa7a5c45 in update (zig.exe.obj)
    try comp.performAllTheWork(main_progress_node);
                              ^
D:\Programming\Zig\zig\src\main.zig:3505:20: 0x7ff7aa7ccf5b in updateModule (zig.exe.obj)
    try comp.update();
                   ^
D:\Programming\Zig\zig\src\main.zig:3168:17: 0x7ff7aa6ad3e8 in buildOutputType (zig.exe.obj)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
D:\Programming\Zig\zig\src\main.zig:263:31: 0x7ff7aa687595 in mainArgs (zig.exe.obj)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
D:\Programming\Zig\zig\src\main.zig:207:20: 0x7ff7aa6871ce in main (zig.exe.obj)
    return mainArgs(gpa, arena, args);
                   ^
D:\Programming\Zig\zig\lib\std\start.zig:515:80: 0x7ff7aa688fcb in main (zig.exe.obj)
    return @call(.always_inline, callMainWithArgs, .{ @intCast(usize, c_argc), @ptrCast([*][*:0]u8, c_argv), envp });
                                                                               ^
Unable to dump stack trace: InvalidDebugInfo
error: bug...

The example is a reduction of a piece of code I have in my code base that used to work fine. It seems the change introduced in commit d8128c2 makes the compiler crash on this.

Expected Behavior

I would expect this piece of code to compile like it did before and not to crash the compiler.
I made sure to test with commit 1658e48 which is the commit before d8128c2 and with an older compiler version 0.11.0-dev.220+e7424d5d2 I had downloaded on my pc and with these version it works correctly.

@Flaminator Flaminator added the bug Observed behavior contradicts documented or intended behavior label Jan 14, 2023
@Vexu Vexu added this to the 0.11.0 milestone Jan 15, 2023
@michaelbartnett
Copy link
Contributor

michaelbartnett commented Jan 16, 2023

I also hit this. Flaminator's repro is tighter, but here's my variation in case it's helpful for testing:

const std = @import("std");

fn countDecls(comptime T: type) usize {
    var n: usize = 0;

    inline for (@typeInfo(T).Struct.decls) |decl| {
        _ = decl;
        n += 1;
    }

    return n;
}


fn Mixin(comptime T: type) type {
    return struct {
        const num_decls = countDecls(T);
        pub fn numIntDecls() usize {
            return num_decls;
        }
    };
}

const MyType = struct {
    const n: u16 = 24;

    pub usingnamespace Mixin(@This());
};

pub fn main() void {
    std.log.info("Num decls in MyType: {}", .{MyType.numIntDecls()});
}

Vexu added a commit to Vexu/zig that referenced this issue Jan 16, 2023
Vexu added a commit to Vexu/zig that referenced this issue Jan 16, 2023
Vexu added a commit to Vexu/zig that referenced this issue Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants