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

Zig 0.14-master update fixes #27

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ pub fn build(b: *std.Build) void {
// Tests

var comptime_tests = b.addOptions();
const comptime_tests_enabled = b.option(bool, "comptime-tests", "Run comptime tests") orelse true;
// TODO: Re-enable comptime tests
const comptime_tests_enabled = b.option(bool, "comptime-tests", "Run comptime tests") orelse false;
Comment on lines +84 to +85
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the TODO!

I fear most of the comptime capabilities in mustache-zig are compromised due to recent changes in how Zig treats comptime memory (only const pointers are allowed to "leak" into runtime code). But let the language stabilize a bit more before trying to make this work again.

comptime_tests.addOption(bool, "comptime_tests_enabled", comptime_tests_enabled);

{
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn ParserType(comptime options: TemplateOptions) type {

fn RenderError(comptime TRender: type) type {
switch (@typeInfo(TRender)) {
.Pointer => |pointer| {
.pointer => |pointer| {
if (pointer.size == .One) {
const Render = pointer.child;
return Render.Error;
Expand Down
6 changes: 5 additions & 1 deletion src/parsing/text_scanner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,16 @@ pub fn TextScannerType(comptime Node: type, comptime options: TemplateOptions) t
self.moveLineCounter(char);
},
.matching_close => |*close_state| {
// TODO: Remove this copy
const close_state_copy: @TypeOf(self.state).MatchingCloseState = .{ .delimiter_index = close_state.delimiter_index, .part_type = close_state.part_type };
const delimiter_char = self.delimiters.ending_delimiter[close_state.delimiter_index];
if (char == delimiter_char) {
const next_index = close_state.delimiter_index + 1;

if (self.delimiters.ending_delimiter.len == next_index) {
self.state = .{ .produce_close = close_state.part_type };
// TODO: Determine why Zig states this line is "access of inactive union field"
//self.state = .{ .produce_close = close_state.part_type };
self.state = .{ .produce_close = close_state_copy.part_type };
Comment on lines +266 to +268
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I faced this same issue ... I guess it's a miscompilation. Nice workaround!

} else {
close_state.delimiter_index = next_index;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/trimmer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const TemplateOptions = mustache.options.TemplateOptions;
const parsing = @import("parsing.zig");

pub fn TrimmerType(comptime TextScanner: type, comptime TrimmingIndex: type) type {
return if (@typeInfo(TrimmingIndex) == .Union)
return if (@typeInfo(TrimmingIndex) == .@"union")
struct {
const Trimmer = @This();

Expand Down
50 changes: 25 additions & 25 deletions src/rendering/Fields.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ pub inline fn getField(data: anytype, comptime field_name: []const u8) field_typ
const TField = FieldType(Data, field_name);

switch (@typeInfo(TField)) {
.ComptimeInt => {
.comptime_int => {
const comptime_value = @field(data, field_name);
break :field_type RuntimeInt(comptime_value);
},
.ComptimeFloat => {
.comptime_float => {
const comptime_value = @field(data, field_name);
break :field_type RuntimeFloat(comptime_value);
},
.Null => break :field_type ?u0,
.null => break :field_type ?u0,
else => break :field_type FieldRef(Data, field_name),
}
} {
Expand All @@ -47,7 +47,7 @@ pub inline fn getField(data: anytype, comptime field_name: []const u8) field_typ
const comptime_value = @field(data, field_name);
const runtime_value: RuntimeFloat(comptime_value) = comptime_value;
return runtime_value;
} else if (TField == @TypeOf(.Null)) {
} else if (TField == @TypeOf(.null)) {
const runtime_null: ?u0 = null;
return runtime_null;
}
Expand All @@ -67,7 +67,7 @@ pub inline fn getRuntimeValue(ctx: anytype) type: {
RuntimeInt(ctx)
else if (TContext == comptime_float)
RuntimeFloat(ctx)
else if (TContext == @Type(.Null))
else if (TContext == @Type(.null))
?u0
else
TContext;
Expand All @@ -82,7 +82,7 @@ pub inline fn getRuntimeValue(ctx: anytype) type: {
const comptime_value = ctx;
const runtime_value: RuntimeFloat(comptime_value) = comptime_value;
return runtime_value;
} else if (TContext == @TypeOf(.Null)) {
} else if (TContext == @TypeOf(.null)) {
const runtime_null: ?u0 = null;
return runtime_null;
} else {
Expand All @@ -101,7 +101,7 @@ pub inline fn getTupleElement(ctx: anytype, comptime index: usize) element_type:
} else if (ElementType == comptime_float) {
const comptime_value = ctx[index];
break :element_type RuntimeFloat(comptime_value);
} else if (ElementType == @Type(.Null)) {
} else if (ElementType == @Type(.null)) {
break :element_type ?u0;
} else if (byValue(ElementType)) {
break :element_type ElementType;
Expand All @@ -118,7 +118,7 @@ pub inline fn getTupleElement(ctx: anytype, comptime index: usize) element_type:
const comptime_value = ctx[index];
const runtime_value: RuntimeFloat(comptime_value) = comptime_value;
return runtime_value;
} else if (ElementType == @Type(.Null)) {
} else if (ElementType == @Type(.null)) {
const runtime_null: ?u0 = null;
return runtime_null;
} else if (comptime byValue(ElementType)) {
Expand Down Expand Up @@ -153,7 +153,7 @@ pub inline fn getElement(ctx: anytype, index: usize) element_type: {

fn Lhs(comptime T: type) type {
comptime {
if (@typeInfo(T) == .Optional) {
if (@typeInfo(T) == .optional) {
return Lhs(meta.Child(T));
} else if (needsDerref(T)) {
return Lhs(meta.Child(T));
Expand All @@ -164,7 +164,7 @@ fn Lhs(comptime T: type) type {
}

pub inline fn lhs(comptime T: type, value: T) Lhs(T) {
if (@typeInfo(T) == .Optional) {
if (@typeInfo(T) == .optional) {
return lhs(@TypeOf(value.?), value.?);
} else if (comptime needsDerref(T)) {
return lhs(@TypeOf(value.*), value.*);
Expand All @@ -179,7 +179,7 @@ pub inline fn needsDerref(comptime T: type) bool {
const Child = meta.Child(T);
return stdx.isSingleItemPtr(Child) or
stdx.isSlice(Child) or
@typeInfo(Child) == .Optional;
@typeInfo(Child) == .optional;
} else {
return false;
}
Expand All @@ -188,7 +188,7 @@ pub inline fn needsDerref(comptime T: type) bool {

pub fn byValue(comptime TField: type) bool {
comptime {
if (@typeInfo(TField) == .EnumLiteral) @compileError(
if (@typeInfo(TField) == .enum_literal) @compileError(
\\Enum literal is not supported for interpolation
\\Error: ir_resolve_lazy_recurse. This is a bug in the Zig compiler
\\Type:
Expand All @@ -209,8 +209,8 @@ pub fn byValue(comptime TField: type) bool {

const can_embed = size <= max_size and
switch (@typeInfo(TField)) {
.Enum, .EnumLiteral, .Bool, .Int, .Float => true,
.Optional => |info| byValue(info.child),
.@"enum", .enum_literal, .bool, .int, .float => true,
.optional => |info| byValue(info.child),
else => false,
};

Expand All @@ -220,27 +220,27 @@ pub fn byValue(comptime TField: type) bool {

pub inline fn isNull(comptime T: type, data: T) bool {
return switch (@typeInfo(T)) {
.Pointer => |info| switch (info.size) {
.pointer => |info| switch (info.size) {
.One => return isNull(@TypeOf(data.*), data.*),
.Slice => return false,
.Many => @compileError("[*] pointers not supported"),
.C => @compileError("[*c] pointers not supported"),
},
.Optional => return data == null,
.optional => return data == null,
else => return false,
};
}

pub inline fn lenOf(comptime T: type, data: T) ?usize {
return switch (@typeInfo(T)) {
.Pointer => |info| switch (info.size) {
.pointer => |info| switch (info.size) {
.One => return null,
.Slice => return data.len,
.Many => @compileError("[*] pointers not supported"),
.C => @compileError("[*c] pointers not supported"),
},
.Array, .Vector => return data.len,
.Optional => if (data) |value| return lenOf(@TypeOf(value), value) else null,
.array, .vector => return data.len,
.optional => if (data) |value| return lenOf(@TypeOf(value), value) else null,
else => return null,
};
}
Expand All @@ -250,17 +250,17 @@ fn FieldRef(comptime T: type, comptime field_name: []const u8) type {

assert(TField != comptime_int);
assert(TField != comptime_float);
assert(TField != @TypeOf(.Null));
assert(TField != @TypeOf(.null));

if (@typeInfo(T) == .Optional) {
if (@typeInfo(T) == .optional) {
return FieldRef(meta.Child(T), field_name);
} else if (needsDerref(T)) {
return FieldRef(meta.Child(T), field_name);
} else {
const instance: T = switch (@typeInfo(T)) {
.Struct => std.mem.zeroInit(T, .{}),
.Pointer => @ptrFromInt(@alignOf(T)),
.Void => {},
.@"struct" => std.mem.zeroInit(T, .{}),
.pointer => @ptrFromInt(@alignOf(T)),
.void => {},
else => undefined,
};

Expand All @@ -269,7 +269,7 @@ fn FieldRef(comptime T: type, comptime field_name: []const u8) type {
}

fn FieldType(comptime T: type, comptime field_name: []const u8) type {
if (@typeInfo(T) == .Optional) {
if (@typeInfo(T) == .optional) {
const Child = meta.Child(T);
return FieldType(Child, field_name);
} else if (stdx.isSingleItemPtr(T)) {
Expand Down
6 changes: 3 additions & 3 deletions src/rendering/contexts/ffi/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn ContextType(
if (path.len == 1) {
return self.callGet(&root_path, index);
} else {
@setCold(true);
@branchHint(.cold);
return self.getFromPath(&root_path, &root_path, path[1..], index);
}
} else {
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn ContextType(
if (path.len == 1) {
return self.callCapacityHint(&root_path);
} else {
@setCold(true);
@branchHint(.cold);
return self.capacityHintFromPath(&root_path, &root_path, path[1..]);
}
} else {
Expand Down Expand Up @@ -244,7 +244,7 @@ pub fn ContextType(
escape,
);
} else {
@setCold(true);
@branchHint(.cold);
return try self.interpolateFromPath(
data_render,
&root_path,
Expand Down
28 changes: 14 additions & 14 deletions src/rendering/contexts/native/invoker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn InvokerType(
comptime action_fn: anytype,
) type {
const action_type_info = @typeInfo(@TypeOf(action_fn));
if (action_type_info != .Fn) @compileError("action_fn must be a function");
if (action_type_info != .@"fn") @compileError("action_fn must be a function");

return struct {
const PathResolution = PathResolutionType(TReturn);
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn InvokerType(
) TError!PathResolution {
const Data = @TypeOf(data);
switch (@typeInfo(TValue)) {
.Struct => {
.@"struct" => {
return findFieldPath(
depth,
TValue,
Expand All @@ -107,7 +107,7 @@ pub fn InvokerType(
index,
);
},
.Pointer => |info| switch (info.size) {
.pointer => |info| switch (info.size) {
.One => return try recursiveFind(
depth,
info.child,
Expand All @@ -131,7 +131,7 @@ pub fn InvokerType(
.Many => @compileError("[*] pointers not supported"),
.C => @compileError("[*c] pointers not supported"),
},
.Optional => |info| {
.optional => |info| {
if (!Fields.isNull(Data, data)) {
return try recursiveFind(
depth,
Expand All @@ -144,7 +144,7 @@ pub fn InvokerType(
);
}
},
.Array, .Vector => {
.array, .vector => {
//Slice supports the "len" field,
if (next_path_parts.len == 0 and std.mem.eql(u8, "len", current_path_part)) {
return if (next_path_parts.len == 0)
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn InvokerType(
) TError!PathResolution {
const TData = @TypeOf(data);
const TFn = @TypeOf(bound_fn);
const params_len = @typeInfo(TFn).Fn.params.len;
const params_len = @typeInfo(TFn).@"fn".params.len;

// Lambdas cannot be used for navigation through a path
// Examples:
Expand Down Expand Up @@ -251,7 +251,7 @@ pub fn InvokerType(
) TError!PathResolution {
const Data = @TypeOf(data);
switch (@typeInfo(TValue)) {
.Struct => |info| {
.@"struct" => |info| {
if (info.is_tuple) {
const derref = comptime stdx.isSingleItemPtr(Data);
inline for (0..info.fields.len) |i| {
Expand All @@ -269,13 +269,13 @@ pub fn InvokerType(
}
},
// Booleans are evaluated on the iterator
.Bool => {
.bool => {
return if (data == true and index == 0)
PathResolution{ .field = try action_fn(action_param, data) }
else
.iterator_consumed;
},
.Pointer => |info| switch (info.size) {
.pointer => |info| switch (info.size) {
.One => {
return try iterateAt(
info.child,
Expand All @@ -300,7 +300,7 @@ pub fn InvokerType(
},
else => {},
},
.Array => |info| {
.array => |info| {
//Array of u8 is always string
if (info.child != u8) {
return if (index < data.len)
Expand All @@ -314,7 +314,7 @@ pub fn InvokerType(
.iterator_consumed;
}
},
.Vector => {
.vector => {
return if (index < data.len)
PathResolution{
.field = try action_fn(
Expand All @@ -325,7 +325,7 @@ pub fn InvokerType(
else
.iterator_consumed;
},
.Optional => |info| {
.optional => |info| {
return if (!Fields.isNull(Data, data))
try iterateAt(
info.child,
Expand Down Expand Up @@ -483,8 +483,8 @@ pub fn InvokerType(
// https://github.com/ziglang/zig/issues/2473
fn isOnErrorSet(comptime Error: type, value: anyerror) bool {
switch (@typeInfo(Error)) {
.ErrorSet => |info| if (info) |errors| {
if (@typeInfo(@TypeOf(value)) == .ErrorSet) {
.error_set => |info| if (info) |errors| {
if (@typeInfo(@TypeOf(value)) == .error_set) {
inline for (errors) |item| {
const int_value = @intFromError(@field(Error, item.name));
if (int_value == @intFromError(value)) return true;
Expand Down
Loading
Loading