authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-02 17:28:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-02 17:28:39-07:00
log1c8a86f063b65e95c2e23ceaa40843069adfdc23
tree2ff8a2b231a4e3cc5f21f5681f7b092c0ddc3bb9
parent220708e7c3f437958fbf8376975b70542b9ac199

Sema: detect comptime-known union initializations

Follow a similar pattern as we already do for validate_array_init and validate_struct_init. I threw in a bit of behavior test cleanup on top of it.

9 files changed, 340 insertions(+), 78 deletions(-)

src/Sema.zig+123-39
......@@ -2737,6 +2737,7 @@ fn zirValidateStructInit(
27372737 init_src,
27382738 instrs,
27392739 object_ptr,
2740 is_comptime,
27402741 ),
27412742 else => unreachable,
27422743 }
......@@ -2749,6 +2750,7 @@ fn validateUnionInit(
27492750 init_src: LazySrcLoc,
27502751 instrs: []const Zir.Inst.Index,
27512752 union_ptr: Air.Inst.Ref,
2753 is_comptime: bool,
27522754) CompileError!void {
27532755 if (instrs.len != 1) {
27542756 const msg = msg: {
......@@ -2771,6 +2773,11 @@ fn validateUnionInit(
27712773 return sema.failWithOwnedErrorMsg(msg);
27722774 }
27732775
2776 if (is_comptime or block.is_comptime) {
2777 // In this case, comptime machinery already did everything. No work to do here.
2778 return;
2779 }
2780
27742781 const field_ptr = instrs[0];
27752782 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;
27762783 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_ptr_data.src_node };
......@@ -2779,44 +2786,72 @@ fn validateUnionInit(
27792786 const field_index_big = union_obj.fields.getIndex(field_name) orelse
27802787 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);
27812788 const field_index = @intCast(u32, field_index_big);
2782
2783 // Handle the possibility of the union value being comptime-known.
2784 const union_ptr_inst = Air.refToIndex(union_ptr).?;
2785 switch (sema.air_instructions.items(.tag)[union_ptr_inst]) {
2786 .constant => {
2787 if (try sema.resolveDefinedValue(block, init_src, union_ptr)) |ptr_val| {
2788 if (ptr_val.isComptimeMutablePtr()) {
2789 // In this case the tag has already been set. No validation to do.
2790 return;
2791 }
2792 }
2793 },
2794 .bitcast => {
2795 // TODO here we need to go back and see if we need to convert the union
2796 // to a comptime-known value. In such case, we must delete all the instructions
2797 // added to the current block starting with the bitcast.
2798 // If the bitcast result ptr is an alloc, the alloc should be replaced with
2799 // a constant decl_ref.
2800 // Otherwise, the bitcast should be preserved and a store instruction should be
2801 // emitted to store the constant union value through the bitcast.
2802 },
2803 .alloc => {},
2804 else => |t| {
2805 if (std.debug.runtime_safety) {
2806 std.debug.panic("unexpected AIR tag for union pointer: {s}", .{@tagName(t)});
2807 } else {
2808 unreachable;
2809 }
2810 },
2789 const air_tags = sema.air_instructions.items(.tag);
2790 const air_datas = sema.air_instructions.items(.data);
2791 const field_ptr_air_ref = sema.inst_map.get(field_ptr).?;
2792 const field_ptr_air_inst = Air.refToIndex(field_ptr_air_ref).?;
2793
2794 // Our task here is to determine if the union is comptime-known. In such case,
2795 // we erase the runtime AIR instructions for initializing the union, and replace
2796 // the mapping with the comptime value. Either way, we will need to populate the tag.
2797
2798 // We expect to see something like this in the current block AIR:
2799 // %a = alloc(*const U)
2800 // %b = bitcast(*U, %a)
2801 // %c = field_ptr(..., %b)
2802 // %e!= store(%c!, %d!)
2803 // If %d is a comptime operand, the union is comptime.
2804 // If the union is comptime, we want `first_block_index`
2805 // to point at %c so that the bitcast becomes the last instruction in the block.
2806 //
2807 // In the case of a comptime-known pointer to a union, the
2808 // the field_ptr instruction is missing, so we have to pattern-match
2809 // based only on the store instructions.
2810 // `first_block_index` needs to point to the `field_ptr` if it exists;
2811 // the `store` otherwise.
2812 //
2813 // It's also possible for there to be no store instruction, in the case
2814 // of nested `coerce_result_ptr` instructions. If we see the `field_ptr`
2815 // but we have not found a `store`, treat as a runtime-known field.
2816 var first_block_index = block.instructions.items.len;
2817 var block_index = block.instructions.items.len - 1;
2818 var init_val: ?Value = null;
2819 while (block_index > 0) : (block_index -= 1) {
2820 const store_inst = block.instructions.items[block_index];
2821 if (store_inst == field_ptr_air_inst) break;
2822 if (air_tags[store_inst] != .store) continue;
2823 const bin_op = air_datas[store_inst].bin_op;
2824 if (bin_op.lhs != field_ptr_air_ref) continue;
2825 if (block_index > 0 and
2826 field_ptr_air_inst == block.instructions.items[block_index - 1])
2827 {
2828 first_block_index = @minimum(first_block_index, block_index - 1);
2829 } else {
2830 first_block_index = @minimum(first_block_index, block_index);
2831 }
2832 init_val = try sema.resolveMaybeUndefValAllowVariables(block, init_src, bin_op.rhs);
2833 break;
28112834 }
28122835
2813 // Otherwise, we set the new union tag now.
2814 const new_tag = try sema.addConstant(
2815 union_obj.tag_ty,
2816 try Value.Tag.enum_field_index.create(sema.arena, field_index),
2817 );
2836 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
2837
2838 if (init_val) |val| {
2839 // Our task is to delete all the `field_ptr` and `store` instructions, and insert
2840 // instead a single `store` to the result ptr with a comptime union value.
2841 block.instructions.shrinkRetainingCapacity(first_block_index);
2842
2843 const union_val = try Value.Tag.@"union".create(sema.arena, .{
2844 .tag = tag_val,
2845 .val = val,
2846 });
2847 const union_ty = sema.typeOf(union_ptr).childType();
2848 const union_init = try sema.addConstant(union_ty, union_val);
2849 try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store);
2850 return;
2851 }
28182852
28192853 try sema.requireRuntimeBlock(block, init_src);
2854 const new_tag = try sema.addConstant(union_obj.tag_ty, tag_val);
28202855 _ = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
28212856}
28222857
......@@ -3084,7 +3119,7 @@ fn zirValidateArrayInit(
30843119 }
30853120
30863121 var array_is_comptime = true;
3087 var first_block_index: usize = std.math.maxInt(u32);
3122 var first_block_index = block.instructions.items.len;
30883123
30893124 // Collect the comptime element values in case the array literal ends up
30903125 // being comptime-known.
......@@ -15147,7 +15182,32 @@ fn unionFieldPtr(
1514715182 });
1514815183
1514915184 if (try sema.resolveDefinedValue(block, src, union_ptr)) |union_ptr_val| {
15150 // TODO detect inactive union field and emit compile error
15185 switch (union_obj.layout) {
15186 .Auto => {
15187 // TODO emit the access of inactive union field error commented out below.
15188 // In order to do that, we need to first solve the problem that AstGen
15189 // emits field_ptr instructions in order to initialize union values.
15190 // In such case we need to know that the field_ptr instruction (which is
15191 // calling this unionFieldPtr function) is *initializing* the union,
15192 // in which case we would skip this check, and in fact we would actually
15193 // set the union tag here and the payload to undefined.
15194
15195 //const tag_and_val = union_val.castTag(.@"union").?.data;
15196 //var field_tag_buf: Value.Payload.U32 = .{
15197 // .base = .{ .tag = .enum_field_index },
15198 // .data = field_index,
15199 //};
15200 //const field_tag = Value.initPayload(&field_tag_buf.base);
15201 //const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty);
15202 //if (!tag_matches) {
15203 // // TODO enhance this saying which one was active
15204 // // and which one was accessed, and showing where the union was declared.
15205 // return sema.fail(block, src, "access of inactive union field", .{});
15206 //}
15207 // TODO add runtime safety check for the active tag
15208 },
15209 .Packed, .Extern => {},
15210 }
1515115211 return sema.addConstant(
1515215212 ptr_field_ty,
1515315213 try Value.Tag.field_ptr.create(arena, .{
......@@ -15183,9 +15243,33 @@ fn unionFieldVal(
1518315243 if (try sema.resolveMaybeUndefVal(block, src, union_byval)) |union_val| {
1518415244 if (union_val.isUndef()) return sema.addConstUndef(field.ty);
1518515245
15186 // TODO detect inactive union field and emit compile error
15187 const active_val = union_val.castTag(.@"union").?.data.val;
15188 return sema.addConstant(field.ty, active_val);
15246 const tag_and_val = union_val.castTag(.@"union").?.data;
15247 var field_tag_buf: Value.Payload.U32 = .{
15248 .base = .{ .tag = .enum_field_index },
15249 .data = field_index,
15250 };
15251 const field_tag = Value.initPayload(&field_tag_buf.base);
15252 const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty);
15253 switch (union_obj.layout) {
15254 .Auto => {
15255 if (tag_matches) {
15256 return sema.addConstant(field.ty, tag_and_val.val);
15257 } else {
15258 // TODO enhance this saying which one was active
15259 // and which one was accessed, and showing where the union was declared.
15260 return sema.fail(block, src, "access of inactive union field", .{});
15261 }
15262 },
15263 .Packed, .Extern => {
15264 if (tag_matches) {
15265 return sema.addConstant(field.ty, tag_and_val.val);
15266 } else {
15267 const old_ty = union_ty.unionFieldType(tag_and_val.tag);
15268 const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty);
15269 return sema.addConstant(field.ty, new_val);
15270 }
15271 },
15272 }
1518915273 }
1519015274
1519115275 try sema.requireRuntimeBlock(block, src);
test/behavior.zig+2-3
......@@ -51,6 +51,7 @@ test {
5151 _ = @import("behavior/defer.zig");
5252 _ = @import("behavior/enum.zig");
5353 _ = @import("behavior/error.zig");
54 _ = @import("behavior/floatop.zig");
5455 _ = @import("behavior/fn.zig");
5556 _ = @import("behavior/fn_delegation.zig");
5657 _ = @import("behavior/fn_in_struct_in_comptime.zig");
......@@ -79,6 +80,7 @@ test {
7980 _ = @import("behavior/slice_sentinel_comptime.zig");
8081 _ = @import("behavior/src.zig");
8182 _ = @import("behavior/struct.zig");
83 _ = @import("behavior/switch.zig");
8284 _ = @import("behavior/this.zig");
8385 _ = @import("behavior/truncate.zig");
8486 _ = @import("behavior/try.zig");
......@@ -93,7 +95,6 @@ test {
9395 _ = @import("behavior/void.zig");
9496 _ = @import("behavior/while.zig");
9597
96 // tests that don't pass for stage1
9798 if (builtin.zig_backend != .stage1) {
9899 _ = @import("behavior/decltest.zig");
99100 }
......@@ -116,13 +117,11 @@ test {
116117 if (builtin.zig_backend != .stage2_c) {
117118 // Tests that pass for stage1 and the llvm backend.
118119 _ = @import("behavior/atomics.zig");
119 _ = @import("behavior/floatop.zig");
120120 _ = @import("behavior/math.zig");
121121 _ = @import("behavior/maximum_minimum.zig");
122122 _ = @import("behavior/popcount.zig");
123123 _ = @import("behavior/saturating_arithmetic.zig");
124124 _ = @import("behavior/sizeof_and_typeof.zig");
125 _ = @import("behavior/switch.zig");
126125 _ = @import("behavior/widening.zig");
127126 _ = @import("behavior/bugs/421.zig");
128127 _ = @import("behavior/bugs/726.zig");
test/behavior/array.zig+7-3
......@@ -108,15 +108,19 @@ test "array len field" {
108108}
109109
110110test "array with sentinels" {
111 if (builtin.zig_backend == .stage1) {
112 // Stage1 test coverage disabled at runtime because of
113 // https://github.com/ziglang/zig/issues/4372
114 return error.SkipZigTest;
115 }
116
111117 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
112118 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
113119
114120 const S = struct {
115121 fn doTheTest(is_ct: bool) !void {
116 if (is_ct or builtin.zig_backend != .stage1) {
122 {
117123 var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
118 // Stage1 test coverage disabled at runtime because of
119 // https://github.com/ziglang/zig/issues/4372
120124 try expect(zero_sized[0] == 0xde);
121125 var reinterpreted = @ptrCast(*[1]u8, &zero_sized);
122126 try expect(reinterpreted[0] == 0xde);
test/behavior/cast.zig+6-4
......@@ -1050,9 +1050,9 @@ fn incrementVoidPtrArray(array: ?*anyopaque, len: usize) void {
10501050}
10511051
10521052test "compile time int to ptr of function" {
1053 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
10541053 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1055 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
1054
1055 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10561056 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10571057 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10581058 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
......@@ -1060,11 +1060,13 @@ test "compile time int to ptr of function" {
10601060 try foobar(FUNCTION_CONSTANT);
10611061}
10621062
1063pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
1063// On some architectures function pointers must be aligned.
1064const hardcoded_fn_addr = maxInt(usize) & ~@as(usize, 0xf);
1065pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, hardcoded_fn_addr);
10641066pub const PFN_void = *const fn (*anyopaque) callconv(.C) void;
10651067
10661068fn foobar(func: PFN_void) !void {
1067 try std.testing.expect(@ptrToInt(func) == maxInt(usize));
1069 try std.testing.expect(@ptrToInt(func) == hardcoded_fn_addr);
10681070}
10691071
10701072test "implicit ptr to *anyopaque" {
test/behavior/floatop.zig+94-10
......@@ -21,6 +21,12 @@ fn epsForType(comptime T: type) T {
2121}
2222
2323test "floating point comparisons" {
24 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
26 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
27 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
28 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
29
2430 try testFloatComparisons();
2531 comptime try testFloatComparisons();
2632}
......@@ -51,6 +57,12 @@ fn testFloatComparisons() !void {
5157}
5258
5359test "different sized float comparisons" {
60 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
61 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
62 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
63 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
64 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
65
5466 try testDifferentSizedFloatComparisons();
5567 comptime try testDifferentSizedFloatComparisons();
5668}
......@@ -81,12 +93,24 @@ fn testDifferentSizedFloatComparisons() !void {
8193//}
8294
8395test "negative f128 floatToInt at compile-time" {
96 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
97 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
98 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
99 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
100 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
101
84102 const a: f128 = -2;
85103 var b = @floatToInt(i64, a);
86104 try expect(@as(i64, -2) == b);
87105}
88106
89107test "@sqrt" {
108 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
109 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
110 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
111 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
112 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
113
90114 comptime try testSqrt();
91115 try testSqrt();
92116}
......@@ -129,6 +153,12 @@ fn testSqrt() !void {
129153}
130154
131155test "more @sqrt f16 tests" {
156 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
157 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
158 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
159 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
160 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
161
132162 // TODO these are not all passing at comptime
133163 try expect(@sqrt(@as(f16, 0.0)) == 0.0);
134164 try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
......@@ -149,14 +179,21 @@ test "more @sqrt f16 tests" {
149179}
150180
151181test "@sin" {
182 if (builtin.zig_backend == .stage1) {
183 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
184 return error.SkipZigTest;
185 }
186 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
187 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
188 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
189 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
190 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
191
152192 comptime try testSin();
153193 try testSin();
154194}
155195
156196fn testSin() !void {
157 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
158 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
159
160197 inline for ([_]type{ f16, f32, f64 }) |ty| {
161198 const eps = epsForType(ty);
162199 try expect(@sin(@as(ty, 0)) == 0);
......@@ -176,14 +213,21 @@ fn testSin() !void {
176213}
177214
178215test "@cos" {
216 if (builtin.zig_backend == .stage1) {
217 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
218 return error.SkipZigTest;
219 }
220 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
221 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
222 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
223 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
224 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
225
179226 comptime try testCos();
180227 try testCos();
181228}
182229
183230fn testCos() !void {
184 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
185 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
186
187231 inline for ([_]type{ f16, f32, f64 }) |ty| {
188232 const eps = epsForType(ty);
189233 try expect(@cos(@as(ty, 0)) == 1);
......@@ -203,6 +247,12 @@ fn testCos() !void {
203247}
204248
205249test "@exp" {
250 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
251 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
252 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
253 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
254 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
255
206256 comptime try testExp();
207257 try testExp();
208258}
......@@ -226,6 +276,12 @@ fn testExp() !void {
226276}
227277
228278test "@exp2" {
279 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
280 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
281 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
282 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
283 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
284
229285 comptime try testExp2();
230286 try testExp2();
231287}
......@@ -249,7 +305,7 @@ fn testExp2() !void {
249305}
250306
251307test "@log" {
252 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
308 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
253309
254310 comptime try testLog();
255311 try testLog();
......@@ -285,7 +341,7 @@ fn testLog() !void {
285341}
286342
287343test "@log2" {
288 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
344 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
289345
290346 comptime try testLog2();
291347 try testLog2();
......@@ -310,7 +366,7 @@ fn testLog2() !void {
310366}
311367
312368test "@log10" {
313 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
369 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
314370
315371 comptime try testLog10();
316372 try testLog10();
......@@ -335,6 +391,12 @@ fn testLog10() !void {
335391}
336392
337393test "@fabs" {
394 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
395 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
396 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
397 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
398 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
399
338400 comptime try testFabs();
339401 try testFabs();
340402}
......@@ -367,6 +429,12 @@ fn testFabs() !void {
367429}
368430
369431test "@floor" {
432 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
433 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
434 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
435 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
436 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
437
370438 comptime try testFloor();
371439 try testFloor();
372440}
......@@ -394,6 +462,12 @@ fn testFloor() !void {
394462}
395463
396464test "@ceil" {
465 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
466 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
467 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
468 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
469 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
470
397471 comptime try testCeil();
398472 try testCeil();
399473}
......@@ -421,6 +495,12 @@ fn testCeil() !void {
421495}
422496
423497test "@trunc" {
498 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
499 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
500 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
501 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
502 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
503
424504 comptime try testTrunc();
425505 try testTrunc();
426506}
......@@ -449,7 +529,11 @@ fn testTrunc() !void {
449529
450530test "negation" {
451531 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
452 if (builtin.os.tag == .freebsd) return error.SkipZigTest;
532
533 if (builtin.os.tag == .freebsd) {
534 // TODO file issue to track this failure
535 return error.SkipZigTest;
536 }
453537
454538 const S = struct {
455539 fn doTheTest() !void {
test/behavior/inttoptr.zig+1-2
......@@ -2,7 +2,6 @@ const builtin = @import("builtin");
22
33test "casting integer address to function pointer" {
44 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
5 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .aarch64) return error.SkipZigTest; // TODO
65 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
76 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
87
......@@ -11,7 +10,7 @@ test "casting integer address to function pointer" {
1110}
1211
1312fn addressToFunction() void {
14 var addr: usize = 0xdeadbeef;
13 var addr: usize = 0xdeadbee0;
1514 _ = @intToPtr(*const fn () void, addr);
1615}
1716
test/behavior/struct.zig+20-4
......@@ -1175,7 +1175,11 @@ test "for loop over pointers to struct, getting field from struct pointer" {
11751175
11761176test "anon init through error unions and optionals" {
11771177 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1178 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
1178 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1179 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1180 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1181 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1182 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11791183
11801184 const S = struct {
11811185 a: u32,
......@@ -1200,7 +1204,11 @@ test "anon init through error unions and optionals" {
12001204
12011205test "anon init through optional" {
12021206 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1203 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
1207 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1208 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1209 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1210 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1211 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12041212
12051213 const S = struct {
12061214 a: u32,
......@@ -1218,7 +1226,11 @@ test "anon init through optional" {
12181226
12191227test "anon init through error union" {
12201228 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1221 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
1229 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1230 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1231 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1232 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1233 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12221234
12231235 const S = struct {
12241236 a: u32,
......@@ -1236,7 +1248,11 @@ test "anon init through error union" {
12361248
12371249test "typed init through error unions and optionals" {
12381250 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
1239 if (builtin.zig_backend != .stage2_llvm) return error.SkipZigTest; // TODO
1251 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1252 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1253 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1254 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1255 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12401256
12411257 const S = struct {
12421258 a: u32,
test/behavior/switch.zig+80-6
......@@ -1,9 +1,13 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const expect = std.testing.expect;
34const expectError = std.testing.expectError;
45const expectEqual = std.testing.expectEqual;
56
67test "switch with numbers" {
8 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10
711 try testSwitchWithNumbers(13);
812}
913
......@@ -17,6 +21,9 @@ fn testSwitchWithNumbers(x: u32) !void {
1721}
1822
1923test "switch with all ranges" {
24 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
25 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
26
2027 try expect(testSwitchWithAllRanges(50, 3) == 1);
2128 try expect(testSwitchWithAllRanges(101, 0) == 2);
2229 try expect(testSwitchWithAllRanges(300, 5) == 3);
......@@ -48,6 +55,9 @@ test "implicit comptime switch" {
4855}
4956
5057test "switch on enum" {
58 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
59 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
60
5161 const fruit = Fruit.Orange;
5262 nonConstSwitchOnEnum(fruit);
5363}
......@@ -65,6 +75,9 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
6575}
6676
6777test "switch statement" {
78 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
79 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
80
6881 try nonConstSwitch(SwitchStatementFoo.C);
6982}
7083fn nonConstSwitch(foo: SwitchStatementFoo) !void {
......@@ -79,6 +92,10 @@ fn nonConstSwitch(foo: SwitchStatementFoo) !void {
7992const SwitchStatementFoo = enum { A, B, C, D };
8093
8194test "switch with multiple expressions" {
95 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
96 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
97 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
98
8299 const x = switch (returnsFive()) {
83100 1, 2, 3 => 1,
84101 4, 5, 6 => 2,
......@@ -91,6 +108,9 @@ fn returnsFive() i32 {
91108}
92109
93110test "switch on type" {
111 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
112 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
113
94114 try expect(trueIfBoolFalseOtherwise(bool));
95115 try expect(!trueIfBoolFalseOtherwise(i32));
96116}
......@@ -103,6 +123,10 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
103123}
104124
105125test "switching on booleans" {
126 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
127 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
128 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
129
106130 try testSwitchOnBools();
107131 comptime try testSwitchOnBools();
108132}
......@@ -154,6 +178,9 @@ test "undefined.u0" {
154178}
155179
156180test "switch with disjoint range" {
181 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
182 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
183
157184 var q: u8 = 0;
158185 switch (q) {
159186 0...125 => {},
......@@ -163,6 +190,9 @@ test "switch with disjoint range" {
163190}
164191
165192test "switch variable for range and multiple prongs" {
193 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
194 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
195
166196 const S = struct {
167197 fn doTheTest() !void {
168198 var u: u8 = 16;
......@@ -196,6 +226,9 @@ fn poll() void {
196226}
197227
198228test "switch on global mutable var isn't constant-folded" {
229 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
230 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
231
199232 while (state < 2) {
200233 poll();
201234 }
......@@ -208,6 +241,10 @@ const SwitchProngWithVarEnum = union(enum) {
208241};
209242
210243test "switch prong with variable" {
244 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
245 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
246 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
247
211248 try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
212249 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
213250 try switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
......@@ -228,6 +265,9 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) !void {
228265}
229266
230267test "switch on enum using pointer capture" {
268 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
269 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
270
231271 try testSwitchEnumPtrCapture();
232272 comptime try testSwitchEnumPtrCapture();
233273}
......@@ -245,6 +285,10 @@ fn testSwitchEnumPtrCapture() !void {
245285}
246286
247287test "switch handles all cases of number" {
288 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
289 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
290 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
291
248292 try testSwitchHandleAllCases();
249293 comptime try testSwitchHandleAllCases();
250294}
......@@ -282,6 +326,9 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
282326}
283327
284328test "switch on union with some prongs capturing" {
329 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
330 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
331
285332 const X = union(enum) {
286333 a,
287334 b: i32,
......@@ -311,10 +358,16 @@ fn returnsFalse() bool {
311358 }
312359}
313360test "switch on const enum with var" {
361 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
362 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
363
314364 try expect(!returnsFalse());
315365}
316366
317367test "anon enum literal used in switch on union enum" {
368 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
369 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
370
318371 const Foo = union(enum) {
319372 a: i32,
320373 };
......@@ -328,6 +381,9 @@ test "anon enum literal used in switch on union enum" {
328381}
329382
330383test "switch all prongs unreachable" {
384 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
385 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
386
331387 try testAllProngsUnreachable();
332388 comptime try testAllProngsUnreachable();
333389}
......@@ -349,6 +405,9 @@ fn switchWithUnreachable(x: i32) i32 {
349405}
350406
351407test "capture value of switch with all unreachable prongs" {
408 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
409 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
410
352411 const x = return_a_number() catch |err| switch (err) {
353412 else => unreachable,
354413 };
......@@ -360,6 +419,10 @@ fn return_a_number() anyerror!i32 {
360419}
361420
362421test "switch on integer with else capturing expr" {
422 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
423 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
424 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
425
363426 const S = struct {
364427 fn doTheTest() !void {
365428 var x: i32 = 5;
......@@ -375,7 +438,7 @@ test "switch on integer with else capturing expr" {
375438}
376439
377440test "else prong of switch on error set excludes other cases" {
378 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
441 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
379442
380443 const S = struct {
381444 fn doTheTest() !void {
......@@ -407,7 +470,7 @@ test "else prong of switch on error set excludes other cases" {
407470}
408471
409472test "switch prongs with error set cases make a new error set type for capture value" {
410 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
473 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
411474
412475 const S = struct {
413476 fn doTheTest() !void {
......@@ -441,6 +504,9 @@ test "switch prongs with error set cases make a new error set type for capture v
441504}
442505
443506test "return result loc and then switch with range implicit casted to error union" {
507 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
508 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
509
444510 const S = struct {
445511 fn doTheTest() !void {
446512 try expect((func(0xb) catch unreachable) == 0xb);
......@@ -457,6 +523,10 @@ test "return result loc and then switch with range implicit casted to error unio
457523}
458524
459525test "switch with null and T peer types and inferred result location type" {
526 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
527 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
528 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
529
460530 const S = struct {
461531 fn doTheTest(c: u8) !void {
462532 if (switch (c) {
......@@ -473,7 +543,7 @@ test "switch with null and T peer types and inferred result location type" {
473543}
474544
475545test "switch prongs with cases with identical payload types" {
476 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
546 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
477547
478548 const Union = union(enum) {
479549 A: usize,
......@@ -515,7 +585,11 @@ test "switch prongs with cases with identical payload types" {
515585}
516586
517587test "switch on pointer type" {
518 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
588 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
589 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
590 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
591 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
592 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
519593
520594 const S = struct {
521595 const X = struct {
......@@ -544,7 +618,7 @@ test "switch on pointer type" {
544618}
545619
546620test "switch on error set with single else" {
547 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
621 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
548622
549623 const S = struct {
550624 fn doTheTest() !void {
......@@ -563,7 +637,7 @@ test "switch on error set with single else" {
563637}
564638
565639test "switch capture copies its payload" {
566 if (@import("builtin").zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
640 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
567641
568642 const S = struct {
569643 fn doTheTest() !void {
test/behavior/union.zig+7-7
......@@ -878,8 +878,6 @@ test "union with comptime_int tag" {
878878}
879879
880880test "extern union doesn't trigger field check at comptime" {
881 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
882
883881 const U = extern union {
884882 x: u32,
885883 y: u8,
......@@ -890,7 +888,8 @@ test "extern union doesn't trigger field check at comptime" {
890888}
891889
892890test "anonymous union literal syntax" {
893 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
891 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
892 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
894893
895894 const S = struct {
896895 const Number = union {
......@@ -914,7 +913,8 @@ test "anonymous union literal syntax" {
914913}
915914
916915test "function call result coerces from tagged union to the tag" {
917 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
916 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
917 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
918918
919919 const S = struct {
920920 const Arch = union(enum) {
......@@ -1104,9 +1104,9 @@ test "union enum type gets a separate scope" {
11041104test "global variable struct contains union initialized to non-most-aligned field" {
11051105 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
11061106 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1107 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
1108 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
1109 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1107 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1108 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1109 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
11101110
11111111 const T = struct {
11121112 const U = union(enum) {