Closed
Description
This code fails to compile with the following error:
const std = @import("std");
fn Frog(comptime T: type) type {
return struct { fish: T };
}
const Bear = struct {
grizzle: Frog([]Bear),
};
pub fn main() !void {
const my_precious = Bear{ .grizzle = undefined };
std.debug.warn("{}", .{my_precious});
}
./src/main.zig:9:14: error: struct 'Bear' depends on itself
const Bear = struct {
^
./src/main.zig:10:19: note: referenced here
grizzle: Frog([]Bear),
^
./src/main.zig:10:18: note: referenced here
grizzle: Frog([]Bear),
^
./src/main.zig:15:29: note: referenced here
const my_precious = Bear{
^
/usr/lib/zig/std/start.zig:253:40: note: referenced here
const result = root.main() catch |err| {
^
However, a small change which should produce identical code allows it to compile
const std = @import("std");
fn Frog(comptime T: type) type {
return struct { fish: []T };
}
const Bear = struct {
grizzle: Frog(Bear),
};
pub fn main() !void {
const my_precious = Bear{ .grizzle = undefined };
std.debug.warn("{}", .{my_precious});
}
In the second example Instead of passing a slice type to Frog
, the struct returned by Frog
always contains a slice. The first example which fails to compile is valid code as only a pointer to Bear
is stored, not the struct itself, so it should be allowed to compile.