Skip to content

Commit

Permalink
add ast-check flag to zig fmt, fix found bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Jun 13, 2021
1 parent b9f07b7 commit e63ff4f
Show file tree
Hide file tree
Showing 29 changed files with 803 additions and 547 deletions.
2 changes: 1 addition & 1 deletion ci/azure/linux_script
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ unset CXX
make $JOBS install

# look for formatting errors
release/bin/zig fmt --check .. || (echo "Please run 'zig fmt' to fix the non-conforming files listed above." && false)
release/bin/zig fmt --check --ast-check .. || (echo "Please run 'zig fmt' to fix the non-conforming files listed above." && false)

# Here we rebuild zig but this time using the Zig binary we just now produced to
# build zig1.o rather than relying on the one built with stage0. See
Expand Down
15 changes: 14 additions & 1 deletion lib/std/fmt/parse_float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// - Only supports round-to-zero
// - Does not handle denormals

const std = @import("../std.zig");
const std = @import("std");
const ascii = std.ascii;

// The mantissa field in FloatRepr is 64bit wide and holds only 19 digits
Expand Down Expand Up @@ -231,6 +231,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult {
} else if (c == '.') {
i += 1;
state = .LeadingFractionalZeros;
} else if (c == '_') {
i += 1;
} else {
state = .MantissaIntegral;
}
Expand Down Expand Up @@ -259,6 +261,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult {
} else if (c == '.') {
i += 1;
state = .MantissaFractional;
} else if (c == '_') {
i += 1;
} else {
state = .MantissaFractional;
}
Expand All @@ -276,13 +280,17 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult {
} else if (c == 'e' or c == 'E') {
i += 1;
state = .ExponentSign;
} else if (c == '_') {
i += 1;
} else {
state = .ExponentSign;
}
},
.ExponentSign => {
if (c == '+') {
i += 1;
} else if (c == '_') {
return error.InvalidCharacter;
} else if (c == '-') {
negative_exp = true;
i += 1;
Expand All @@ -293,6 +301,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult {
.LeadingExponentZeros => {
if (c == '0') {
i += 1;
} else if (c == '_') {
i += 1;
} else {
state = .Exponent;
}
Expand All @@ -304,6 +314,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult {
exponent += @intCast(i32, c - '0');
}

i += 1;
} else if (c == '_') {
i += 1;
} else {
return error.InvalidCharacter;
Expand Down Expand Up @@ -405,6 +417,7 @@ test "fmt.parseFloat" {
try expectEqual(try parseFloat(T, "-INF"), -std.math.inf(T));

try expectEqual(try parseFloat(T, "0.4e0066999999999999999999999999999999999999999999999999999"), std.math.inf(T));
try expect(approxEqAbs(T, try parseFloat(T, "0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0"), @as(T, 123456.789000e10), epsilon));

if (T != f16) {
try expect(approxEqAbs(T, try parseFloat(T, "1e-2"), 0.01, epsilon));
Expand Down
831 changes: 523 additions & 308 deletions lib/std/special/compiler_rt.zig

Large diffs are not rendered by default.

130 changes: 85 additions & 45 deletions lib/std/special/compiler_rt/atomics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ fn atomicLoadFn(comptime T: type) fn (*T, i32) callconv(.C) T {

comptime {
if (supports_atomic_ops) {
@export(atomicLoadFn(u8), .{ .name = "__atomic_load_1", .linkage = linkage });
@export(atomicLoadFn(u16), .{ .name = "__atomic_load_2", .linkage = linkage });
@export(atomicLoadFn(u32), .{ .name = "__atomic_load_4", .linkage = linkage });
@export(atomicLoadFn(u64), .{ .name = "__atomic_load_8", .linkage = linkage });
const atomicLoad_u8 = atomicLoadFn(u8);
const atomicLoad_u16 = atomicLoadFn(u16);
const atomicLoad_u32 = atomicLoadFn(u32);
const atomicLoad_u64 = atomicLoadFn(u64);
@export(atomicLoad_u8, .{ .name = "__atomic_load_1", .linkage = linkage });
@export(atomicLoad_u16, .{ .name = "__atomic_load_2", .linkage = linkage });
@export(atomicLoad_u32, .{ .name = "__atomic_load_4", .linkage = linkage });
@export(atomicLoad_u64, .{ .name = "__atomic_load_8", .linkage = linkage });
}
}

Expand All @@ -171,10 +175,14 @@ fn atomicStoreFn(comptime T: type) fn (*T, T, i32) callconv(.C) void {

comptime {
if (supports_atomic_ops) {
@export(atomicStoreFn(u8), .{ .name = "__atomic_store_1", .linkage = linkage });
@export(atomicStoreFn(u16), .{ .name = "__atomic_store_2", .linkage = linkage });
@export(atomicStoreFn(u32), .{ .name = "__atomic_store_4", .linkage = linkage });
@export(atomicStoreFn(u64), .{ .name = "__atomic_store_8", .linkage = linkage });
const atomicStore_u8 = atomicStoreFn(u8);
const atomicStore_u16 = atomicStoreFn(u16);
const atomicStore_u32 = atomicStoreFn(u32);
const atomicStore_u64 = atomicStoreFn(u64);
@export(atomicStore_u8, .{ .name = "__atomic_store_1", .linkage = linkage });
@export(atomicStore_u16, .{ .name = "__atomic_store_2", .linkage = linkage });
@export(atomicStore_u32, .{ .name = "__atomic_store_4", .linkage = linkage });
@export(atomicStore_u64, .{ .name = "__atomic_store_8", .linkage = linkage });
}
}

Expand All @@ -196,10 +204,14 @@ fn atomicExchangeFn(comptime T: type) fn (*T, T, i32) callconv(.C) T {

comptime {
if (supports_atomic_ops) {
@export(atomicExchangeFn(u8), .{ .name = "__atomic_exchange_1", .linkage = linkage });
@export(atomicExchangeFn(u16), .{ .name = "__atomic_exchange_2", .linkage = linkage });
@export(atomicExchangeFn(u32), .{ .name = "__atomic_exchange_4", .linkage = linkage });
@export(atomicExchangeFn(u64), .{ .name = "__atomic_exchange_8", .linkage = linkage });
const atomicExchange_u8 = atomicExchangeFn(u8);
const atomicExchange_u16 = atomicExchangeFn(u16);
const atomicExchange_u32 = atomicExchangeFn(u32);
const atomicExchange_u64 = atomicExchangeFn(u64);
@export(atomicExchange_u8, .{ .name = "__atomic_exchange_1", .linkage = linkage });
@export(atomicExchange_u16, .{ .name = "__atomic_exchange_2", .linkage = linkage });
@export(atomicExchange_u32, .{ .name = "__atomic_exchange_4", .linkage = linkage });
@export(atomicExchange_u64, .{ .name = "__atomic_exchange_8", .linkage = linkage });
}
}

Expand Down Expand Up @@ -229,10 +241,14 @@ fn atomicCompareExchangeFn(comptime T: type) fn (*T, *T, T, i32, i32) callconv(.

comptime {
if (supports_atomic_ops) {
@export(atomicCompareExchangeFn(u8), .{ .name = "__atomic_compare_exchange_1", .linkage = linkage });
@export(atomicCompareExchangeFn(u16), .{ .name = "__atomic_compare_exchange_2", .linkage = linkage });
@export(atomicCompareExchangeFn(u32), .{ .name = "__atomic_compare_exchange_4", .linkage = linkage });
@export(atomicCompareExchangeFn(u64), .{ .name = "__atomic_compare_exchange_8", .linkage = linkage });
const atomicCompareExchange_u8 = atomicCompareExchangeFn(u8);
const atomicCompareExchange_u16 = atomicCompareExchangeFn(u16);
const atomicCompareExchange_u32 = atomicCompareExchangeFn(u32);
const atomicCompareExchange_u64 = atomicCompareExchangeFn(u64);
@export(atomicCompareExchange_u8, .{ .name = "__atomic_compare_exchange_1", .linkage = linkage });
@export(atomicCompareExchange_u16, .{ .name = "__atomic_compare_exchange_2", .linkage = linkage });
@export(atomicCompareExchange_u32, .{ .name = "__atomic_compare_exchange_4", .linkage = linkage });
@export(atomicCompareExchange_u64, .{ .name = "__atomic_compare_exchange_8", .linkage = linkage });
}
}

Expand Down Expand Up @@ -264,34 +280,58 @@ fn fetchFn(comptime T: type, comptime op: builtin.AtomicRmwOp) fn (*T, T, i32) c

comptime {
if (supports_atomic_ops) {
@export(fetchFn(u8, .Add), .{ .name = "__atomic_fetch_add_1", .linkage = linkage });
@export(fetchFn(u16, .Add), .{ .name = "__atomic_fetch_add_2", .linkage = linkage });
@export(fetchFn(u32, .Add), .{ .name = "__atomic_fetch_add_4", .linkage = linkage });
@export(fetchFn(u64, .Add), .{ .name = "__atomic_fetch_add_8", .linkage = linkage });

@export(fetchFn(u8, .Sub), .{ .name = "__atomic_fetch_sub_1", .linkage = linkage });
@export(fetchFn(u16, .Sub), .{ .name = "__atomic_fetch_sub_2", .linkage = linkage });
@export(fetchFn(u32, .Sub), .{ .name = "__atomic_fetch_sub_4", .linkage = linkage });
@export(fetchFn(u64, .Sub), .{ .name = "__atomic_fetch_sub_8", .linkage = linkage });

@export(fetchFn(u8, .And), .{ .name = "__atomic_fetch_and_1", .linkage = linkage });
@export(fetchFn(u16, .And), .{ .name = "__atomic_fetch_and_2", .linkage = linkage });
@export(fetchFn(u32, .And), .{ .name = "__atomic_fetch_and_4", .linkage = linkage });
@export(fetchFn(u64, .And), .{ .name = "__atomic_fetch_and_8", .linkage = linkage });

@export(fetchFn(u8, .Or), .{ .name = "__atomic_fetch_or_1", .linkage = linkage });
@export(fetchFn(u16, .Or), .{ .name = "__atomic_fetch_or_2", .linkage = linkage });
@export(fetchFn(u32, .Or), .{ .name = "__atomic_fetch_or_4", .linkage = linkage });
@export(fetchFn(u64, .Or), .{ .name = "__atomic_fetch_or_8", .linkage = linkage });

@export(fetchFn(u8, .Xor), .{ .name = "__atomic_fetch_xor_1", .linkage = linkage });
@export(fetchFn(u16, .Xor), .{ .name = "__atomic_fetch_xor_2", .linkage = linkage });
@export(fetchFn(u32, .Xor), .{ .name = "__atomic_fetch_xor_4", .linkage = linkage });
@export(fetchFn(u64, .Xor), .{ .name = "__atomic_fetch_xor_8", .linkage = linkage });

@export(fetchFn(u8, .Nand), .{ .name = "__atomic_fetch_nand_1", .linkage = linkage });
@export(fetchFn(u16, .Nand), .{ .name = "__atomic_fetch_nand_2", .linkage = linkage });
@export(fetchFn(u32, .Nand), .{ .name = "__atomic_fetch_nand_4", .linkage = linkage });
@export(fetchFn(u64, .Nand), .{ .name = "__atomic_fetch_nand_8", .linkage = linkage });
const fetch_add_u8 = fetchFn(u8, .Add);
const fetch_add_u16 = fetchFn(u16, .Add);
const fetch_add_u32 = fetchFn(u32, .Add);
const fetch_add_u64 = fetchFn(u64, .Add);
@export(fetch_add_u8, .{ .name = "__atomic_fetch_add_1", .linkage = linkage });
@export(fetch_add_u16, .{ .name = "__atomic_fetch_add_2", .linkage = linkage });
@export(fetch_add_u32, .{ .name = "__atomic_fetch_add_4", .linkage = linkage });
@export(fetch_add_u64, .{ .name = "__atomic_fetch_add_8", .linkage = linkage });

const fetch_sub_u8 = fetchFn(u8, .Sub);
const fetch_sub_u16 = fetchFn(u16, .Sub);
const fetch_sub_u32 = fetchFn(u32, .Sub);
const fetch_sub_u64 = fetchFn(u64, .Sub);
@export(fetch_sub_u8, .{ .name = "__atomic_fetch_sub_1", .linkage = linkage });
@export(fetch_sub_u16, .{ .name = "__atomic_fetch_sub_2", .linkage = linkage });
@export(fetch_sub_u32, .{ .name = "__atomic_fetch_sub_4", .linkage = linkage });
@export(fetch_sub_u64, .{ .name = "__atomic_fetch_sub_8", .linkage = linkage });

const fetch_and_u8 = fetchFn(u8, .And);
const fetch_and_u16 = fetchFn(u16, .And);
const fetch_and_u32 = fetchFn(u32, .And);
const fetch_and_u64 = fetchFn(u64, .And);
@export(fetch_and_u8, .{ .name = "__atomic_fetch_and_1", .linkage = linkage });
@export(fetch_and_u16, .{ .name = "__atomic_fetch_and_2", .linkage = linkage });
@export(fetch_and_u32, .{ .name = "__atomic_fetch_and_4", .linkage = linkage });
@export(fetch_and_u64, .{ .name = "__atomic_fetch_and_8", .linkage = linkage });

const fetch_or_u8 = fetchFn(u8, .Or);
const fetch_or_u16 = fetchFn(u16, .Or);
const fetch_or_u32 = fetchFn(u32, .Or);
const fetch_or_u64 = fetchFn(u64, .Or);
@export(fetch_or_u8, .{ .name = "__atomic_fetch_or_1", .linkage = linkage });
@export(fetch_or_u16, .{ .name = "__atomic_fetch_or_2", .linkage = linkage });
@export(fetch_or_u32, .{ .name = "__atomic_fetch_or_4", .linkage = linkage });
@export(fetch_or_u64, .{ .name = "__atomic_fetch_or_8", .linkage = linkage });

const fetch_xor_u8 = fetchFn(u8, .Xor);
const fetch_xor_u16 = fetchFn(u16, .Xor);
const fetch_xor_u32 = fetchFn(u32, .Xor);
const fetch_xor_u64 = fetchFn(u64, .Xor);
@export(fetch_xor_u8, .{ .name = "__atomic_fetch_xor_1", .linkage = linkage });
@export(fetch_xor_u16, .{ .name = "__atomic_fetch_xor_2", .linkage = linkage });
@export(fetch_xor_u32, .{ .name = "__atomic_fetch_xor_4", .linkage = linkage });
@export(fetch_xor_u64, .{ .name = "__atomic_fetch_xor_8", .linkage = linkage });

const fetch_nand_u8 = fetchFn(u8, .Nand);
const fetch_nand_u16 = fetchFn(u16, .Nand);
const fetch_nand_u32 = fetchFn(u32, .Nand);
const fetch_nand_u64 = fetchFn(u64, .Nand);
@export(fetch_nand_u8, .{ .name = "__atomic_fetch_nand_1", .linkage = linkage });
@export(fetch_nand_u16, .{ .name = "__atomic_fetch_nand_2", .linkage = linkage });
@export(fetch_nand_u32, .{ .name = "__atomic_fetch_nand_4", .linkage = linkage });
@export(fetch_nand_u64, .{ .name = "__atomic_fetch_nand_8", .linkage = linkage });
}
}
10 changes: 7 additions & 3 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn appendRefsAssumeCapacity(astgen: *AstGen, refs: []const Zir.Inst.Ref) void {
astgen.extra.appendSliceAssumeCapacity(coerced);
}

pub fn generate(gpa: *Allocator, tree: ast.Tree) InnerError!Zir {
pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();

Expand Down Expand Up @@ -662,7 +662,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerEr
const lhs = try expr(gz, scope, .ref, node_datas[node].lhs);
const extra = tree.extraData(node_datas[node].rhs, ast.Node.SliceSentinel);
const start = try expr(gz, scope, .{ .ty = .usize_type }, extra.start);
const end = try expr(gz, scope, .{ .ty = .usize_type }, extra.end);
const end = if (extra.end != 0) try expr(gz, scope, .{ .ty = .usize_type }, extra.end) else .none;
const sentinel = try expr(gz, scope, .{ .ty = .usize_type }, extra.sentinel);
const result = try gz.addPlNode(.slice_sentinel, node, Zir.Inst.SliceSentinel{
.lhs = lhs,
Expand Down Expand Up @@ -3877,6 +3877,10 @@ fn containerDecl(
const node_tags = tree.nodes.items(.tag);
const node_datas = tree.nodes.items(.data);

const prev_fn_block = astgen.fn_block;
astgen.fn_block = null;
defer astgen.fn_block = prev_fn_block;

// We must not create any types until Sema. Here the goal is only to generate
// ZIR for all the field types, alignments, and default value expressions.

Expand Down Expand Up @@ -3976,7 +3980,7 @@ fn containerDecl(
.nonexhaustive_node = nonexhaustive_node,
};
};
if (counts.total_fields == 0) {
if (counts.total_fields == 0 and counts.nonexhaustive_node == 0) {
// One can construct an enum with no tags, and it functions the same as `noreturn`. But
// this is only useful for generic code; when explicitly using `enum {}` syntax, there
// must be at least one tag.
Expand Down
Loading

0 comments on commit e63ff4f

Please sign in to comment.