Skip to content

Commit

Permalink
Resovles more feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonRemaley committed Jan 28, 2025
1 parent 1add636 commit daab732
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
8 changes: 4 additions & 4 deletions lib/std/zon/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ const Parser = struct {

// Parse the struct
var result: T = undefined;
var field_found: [field_infos.len]bool = .{false} ** field_infos.len;
var field_found: [field_infos.len]bool = @splat(false);

// If we fail partway through, free all already initialized fields
var initialized: usize = 0;
Expand Down Expand Up @@ -1014,7 +1014,7 @@ const Parser = struct {
const value_node = array_init.ast.elements[field];
break :b self.ast.firstToken(value_node);
};
return self.failTokenFmt(token, 0, "cannot store runtime value in compile time variable", .{});
return self.failTokenFmt(token, 0, "cannot initialize comptime field", .{});
}
};

Expand Down Expand Up @@ -1374,7 +1374,7 @@ test "std.zon structs" {
const parsed = fromSlice(Vec2, gpa, ".{.x = 1.2, .y = 1.5}", &status, .{});
try std.testing.expectError(error.ParseZon, parsed);
try std.testing.expectFmt(
\\1:18: error: cannot store runtime value in compile time variable
\\1:18: error: cannot initialize comptime field
\\
, "{}", .{status});
}
Expand Down Expand Up @@ -1567,7 +1567,7 @@ test "std.zon tuples" {
const parsed = fromSlice(Vec2, gpa, ".{ 1.2, 1.5}", &status, .{});
try std.testing.expectError(error.ParseZon, parsed);
try std.testing.expectFmt(
\\1:9: error: cannot store runtime value in compile time variable
\\1:9: error: cannot initialize comptime field
\\
, "{}", .{status});
}
Expand Down
57 changes: 32 additions & 25 deletions lib/std/zon/stringify.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const std = @import("std");

/// Options for `serialize`.
pub const SerializeOptions = struct {
/// If false, all whitespace is emitted. Otherwise, whitespace is emitted in the standard Zig
/// style when possible.
/// If false, whitespace is omitted. Otherwise whitespace is emitted in standard Zig style.
whitespace: bool = true,
/// If true, unsigned integers with <= 21 bits are written as their corresponding UTF8 codepoint
/// instead of a numeric literal if one exists.
Expand Down Expand Up @@ -93,45 +92,36 @@ pub fn serializeArbitraryDepth(
});
}

const RecursiveTypeBuffer = [32]type;

fn typeIsRecursive(comptime T: type) bool {
comptime var buf: RecursiveTypeBuffer = undefined;
return comptime typeIsRecursiveImpl(T, buf[0..0]);
return comptime typeIsRecursiveImpl(T, &.{});
}

fn typeIsRecursiveImpl(comptime T: type, comptime visited_arg: []type) bool {
comptime var visited = visited_arg;

fn typeIsRecursiveImpl(comptime T: type, comptime prev_visited: []type) bool {
// Check if we've already seen this type
inline for (visited) |found| {
inline for (prev_visited) |found| {
if (T == found) {
return true;
}
}

// Add this type to the stack
if (visited.len >= @typeInfo(RecursiveTypeBuffer).array.len) {
@compileError("recursion limit");
}
visited.ptr[visited.len] = T;
visited.len += 1;
// Create a copy of visited with this type added
comptime var visited = prev_visited[0..prev_visited.len].* ++ .{T};

// Recurse
switch (@typeInfo(T)) {
.pointer => |pointer| return typeIsRecursiveImpl(pointer.child, visited),
.array => |array| return typeIsRecursiveImpl(array.child, visited),
.pointer => |pointer| return typeIsRecursiveImpl(pointer.child, &visited),
.array => |array| return typeIsRecursiveImpl(array.child, &visited),
.@"struct" => |@"struct"| inline for (@"struct".fields) |field| {
if (typeIsRecursiveImpl(field.type, visited)) {
if (typeIsRecursiveImpl(field.type, &visited)) {
return true;
}
},
.@"union" => |@"union"| inline for (@"union".fields) |field| {
if (typeIsRecursiveImpl(field.type, visited)) {
if (typeIsRecursiveImpl(field.type, &visited)) {
return true;
}
},
.optional => |optional| return typeIsRecursiveImpl(optional.child, visited),
.optional => |optional| return typeIsRecursiveImpl(optional.child, &visited),
else => {},
}
return false;
Expand Down Expand Up @@ -405,7 +395,7 @@ pub fn Serializer(Writer: type) type {
break :b .{ @"struct".fields.len, [1]bool{false} ** @"struct".fields.len };
} else b: {
var fields = @"struct".fields.len;
var skipped = [1]bool{false} ** @"struct".fields.len;
var skipped: [@"struct".fields.len]bool = @splat(false);
inline for (@"struct".fields, &skipped) |field_info, *skip| {
if (field_info.default_value_ptr) |default_field_value_opaque| {
const field_value = @field(val, field_info.name);
Expand Down Expand Up @@ -467,15 +457,16 @@ pub fn Serializer(Writer: type) type {
/// Serialize a float.
pub fn float(self: *Self, val: anytype) Writer.Error!void {
switch (@typeInfo(@TypeOf(val))) {
.float, .comptime_float => if (std.math.isNan(val)) {
.float => if (std.math.isNan(val)) {
return self.writer.writeAll("nan");
} else if (@as(f128, val) == std.math.inf(f128)) {
} else if (std.math.isPositiveInf(val)) {
return self.writer.writeAll("inf");
} else if (@as(f128, val) == -std.math.inf(f128)) {
} else if (std.math.isNegativeInf(val)) {
return self.writer.writeAll("-inf");
} else {
try std.fmt.format(self.writer, "{d}", .{val});
},
.comptime_float => try std.fmt.format(self.writer, "{d}", .{val}),
else => @compileError(@typeName(@TypeOf(val)) ++ ": expected float"),
}
}
Expand Down Expand Up @@ -2055,3 +2046,19 @@ test "std.zon stringify as tuple" {
try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items);
buf.clearRetainingCapacity();
}

test "std.zon stringify as float" {
var buf = std.ArrayList(u8).init(std.testing.allocator);
defer buf.deinit();
var sz = serializer(buf.writer(), .{});

// Comptime float
try sz.float(2.5);
try std.testing.expectEqualStrings("2.5", buf.items);
buf.clearRetainingCapacity();

// Sized float
try sz.float(@as(f32, 2.5));
try std.testing.expectEqualStrings("2.5", buf.items);
buf.clearRetainingCapacity();
}

0 comments on commit daab732

Please sign in to comment.