Skip to content

Infinite loop in Semantic Analysis step #8534

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

Closed
paulsmith opened this issue Apr 14, 2021 · 1 comment
Closed

Infinite loop in Semantic Analysis step #8534

paulsmith opened this issue Apr 14, 2021 · 1 comment
Milestone

Comments

@paulsmith
Copy link

paulsmith commented Apr 14, 2021

The following code triggers a seemingly infinite Semantic Analysis step that never completes.

const std = @import("std");

const Node = union(enum) {
    leaf: u8,
    node: ?*Node,

    pub fn format(self: Node, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
        switch (self) {
            .leaf => {
                try writer.writeAll("leaf=\"");
                try std.fmt.format(writer, "{}", .{self.leaf});
                try writer.writeAll("\"");
            },
            .node => {
                try writer.writeAll("node=[");
                if (self.node) |n| try n.format("{}", std.fmt.FormatOptions{}, writer);
                try writer.writeAll("]");
            },
        }
    }
};

pub fn main() anyerror!void {
    var leaf = Node{ .leaf = 127 };
    var node = Node{ .node = &leaf };
    var root = Node{ .node = &node };
    std.debug.print("{}\n", .{root});
}

I expect node=[node=[leaf="127"]] to be printed.

zig-infinite-poc

If you comment out either the call to std.fmt.format in the .leaf arm or the recursive call to Node.format in the .node arm, the code compiles.

@Vexu
Copy link
Member

Vexu commented Apr 15, 2021

Duplicate of #4572.

@Vexu Vexu closed this as completed Apr 15, 2021
@andrewrk andrewrk added this to the 0.8.0 milestone Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants