Skip to content

Compiler segfaults / starts printing corrupt characters to terminal when compiling infinitely recursive code. #24248

Open
@ItsMeSamey

Description

@ItsMeSamey

Zig Version

0.14.1 and 0.15.0-dev.847+850655f06

Steps to Reproduce and Observed Behavior

when compiling the following code with zig test 1.zig

1.zig

const std = @import("std");

const Options = struct {
  T: type,
  U: type = void,
  depth: comptime_int = 0,
};

fn GetSkeleton(options: Options) type {
  comptime var T = options.T;
  while (switch (@typeInfo(T)) {
    .pointer, .optional, .array, .vector => true,
    else => false,
  }) T = std.meta.Child(T);
  
  const info = @typeInfo(T).@"struct";
  return opaque {
    pub const Sub = blk: {
      var sub: [info.fields.len]type = undefined;
      for (info.fields, 0..) |f, i| sub[i] = GetSkeleton(.{ .T = f.type, .U = @This(), .depth = options.depth + 1 });
      break :blk sub;
    };
  };
}

const Recursive = struct {
  self: *@This(),
};

fn printRecursive(T: type) void {
  std.debug.print("Self: {any}\n", .{T});
  inline for (T.Sub) |s| printRecursive(s);
}

test printRecursive {
  printRecursive(GetSkeleton(.{ .T = Recursive, .depth = 0, }));
}

the compiler starts printing illegal / garbage characters to terminal (for 1.14.1).

for example

zig test 1.zig
[18481] Semantic Analysis
tq a+æ�4æaBC��HæaSæ�^�k�W�j�n�k�uæa|æ
mq lV
^C2620] Semantic Analysis
 zig test 1.zig
[21313] Semantic Analysis
[15724] Code Generation
mq CଚCsD@
[29380] Semantic Analysis
[76020] Semantic Analysis
^C1312] Code Generation

and here is a part of capture from script -q -c "zig test 1.zig" file.txt.
I cleaned up some formatting characters but some are still presnt.

[345701] Semantic Analysis
�(0mq�(B 
[214782] Code Generation

[345876] Semantic Analysis
�(0mq�(B ITSETI_B
[214891] Code Generation

[346298] Semantic Analysis
�(0mq�(B E_INDEX
[215153] Code Generation

[346599] Semantic Analysis
�(0mq�(B £<
[215343] Code Generation

Although i know nothing about it but these seem like the contents of the compiler's memory.

Sometimes (i only observed this once with 0.15.0-dev.847+850655f06), compiler segfaults

/tmp/tmp ❯❯ zignew test 1.zig
Segmentation fault (core dumped)
/tmp/tmp ❯❯ ^C__anon_60698

Expected Behavior

The compiler should not print invalid characters to the terminal or segfault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions