authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-27 15:29:03-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-27 15:29:03-05:00
logb5066fdae292f44a6e4c60db9a9db012672dd10e
tree6367bdaa9d4f19ed8905061a670aaff5e6f974f6
parentc03b733f0999418b234067cac51eac0e4412dfe1
parent9f59189c954298fd2be576b7e3dd26388d07751d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11001 from Vexu/stage2

stage2: make formatted printing work

9 files changed, 87 insertions(+), 25 deletions(-)

lib/std/os/linux.zig+8-2
......@@ -865,10 +865,16 @@ pub fn flock(fd: fd_t, operation: i32) usize {
865865 return syscall2(.flock, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, operation)));
866866}
867867
868var vdso_clock_gettime = @ptrCast(?*const anyopaque, init_vdso_clock_gettime);
868var vdso_clock_gettime = if (builtin.zig_backend == .stage1)
869 @ptrCast(?*const anyopaque, init_vdso_clock_gettime)
870else
871 @ptrCast(?*const anyopaque, &init_vdso_clock_gettime);
869872
870873// We must follow the C calling convention when we call into the VDSO
871const vdso_clock_gettime_ty = fn (i32, *timespec) callconv(.C) usize;
874const vdso_clock_gettime_ty = if (builtin.zig_backend == .stage1)
875 fn (i32, *timespec) callconv(.C) usize
876else
877 *const fn (i32, *timespec) callconv(.C) usize;
872878
873879pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
874880 if (@hasDecl(VDSO, "CGT_SYM")) {
lib/std/special/test_runner.zig+9-8
......@@ -23,10 +23,12 @@ fn processArgs() void {
2323}
2424
2525pub fn main() void {
26 if (builtin.zig_backend != .stage1) {
26 if (builtin.zig_backend != .stage1 and
27 builtin.zig_backend != .stage2_llvm)
28 {
2729 return main2() catch @panic("test failure");
2830 }
29 processArgs();
31 if (builtin.zig_backend == .stage1) processArgs();
3032 const test_fn_list = builtin.test_functions;
3133 var ok_count: usize = 0;
3234 var skip_count: usize = 0;
......@@ -44,9 +46,9 @@ pub fn main() void {
4446
4547 var leaks: usize = 0;
4648 for (test_fn_list) |test_fn, i| {
47 std.testing.allocator_instance = .{};
49 if (builtin.zig_backend != .stage2_llvm) std.testing.allocator_instance = .{};
4850 defer {
49 if (std.testing.allocator_instance.deinit()) {
51 if (builtin.zig_backend != .stage2_llvm and std.testing.allocator_instance.deinit()) {
5052 leaks += 1;
5153 }
5254 }
......@@ -90,9 +92,9 @@ pub fn main() void {
9092 fail_count += 1;
9193 progress.log("FAIL ({s})\n", .{@errorName(err)});
9294 if (!have_tty) std.debug.print("FAIL ({s})\n", .{@errorName(err)});
93 if (@errorReturnTrace()) |trace| {
95 if (builtin.zig_backend != .stage2_llvm) if (@errorReturnTrace()) |trace| {
9496 std.debug.dumpStackTrace(trace.*);
95 }
97 };
9698 test_node.end();
9799 },
98100 }
......@@ -141,8 +143,7 @@ pub fn main2() anyerror!void {
141143 }
142144 };
143145 }
144 if (builtin.zig_backend == .stage2_llvm or
145 builtin.zig_backend == .stage2_wasm or
146 if (builtin.zig_backend == .stage2_wasm or
146147 builtin.zig_backend == .stage2_x86_64)
147148 {
148149 const passed = builtin.test_functions.len - skipped - failed;
src/AstGen.zig+8-1
......@@ -244,10 +244,14 @@ pub const ResultLoc = union(enum) {
244244 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {
245245 switch (rl) {
246246 // In this branch there will not be any store_to_block_ptr instructions.
247 .discard, .none, .ty, .coerced_ty, .ref => return .{
247 .none, .ty, .coerced_ty, .ref => return .{
248248 .tag = .break_operand,
249249 .elide_store_to_block_ptr_instructions = false,
250250 },
251 .discard => return .{
252 .tag = .break_void,
253 .elide_store_to_block_ptr_instructions = false,
254 },
251255 // The pointer got passed through to the sub-expressions, so we will use
252256 // break_void here.
253257 // In this branch there will not be any store_to_block_ptr instructions.
......@@ -1766,6 +1770,9 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
17661770 // we assume the result location is written, and we break with void.
17671771 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
17681772 },
1773 .discard => {
1774 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
1775 },
17691776 else => {
17701777 _ = try parent_gz.addBreak(break_tag, block_inst, operand);
17711778 },
src/Module.zig+1-1
......@@ -3762,7 +3762,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
37623762 // Note this resolves the type of the Decl, not the value; if this Decl
37633763 // is a struct, for example, this resolves `type` (which needs no resolution),
37643764 // not the struct itself.
3765 try sema.resolveTypeLayout(&block_scope, src, decl_tv.ty);
3765 try sema.resolveTypeFully(&block_scope, src, decl_tv.ty);
37663766
37673767 const decl_arena_state = try decl_arena_allocator.create(std.heap.ArenaAllocator.State);
37683768
src/Sema.zig+23-9
......@@ -4494,6 +4494,10 @@ fn analyzeCall(
44944494
44954495 try sema.emitBackwardBranch(&child_block, call_src);
44964496
4497 // Whether this call should be memoized, set to false if the call can mutate
4498 // comptime state.
4499 var should_memoize = true;
4500
44974501 // This will have return instructions analyzed as break instructions to
44984502 // the block_inst above. Here we are performing "comptime/inline semantic analysis"
44994503 // for a function body, which means we must map the parameter ZIR instructions to
......@@ -4527,6 +4531,7 @@ fn analyzeCall(
45274531 },
45284532 else => {},
45294533 }
4534 should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
45304535 memoized_call_key.args[arg_i] = .{
45314536 .ty = param_ty,
45324537 .val = arg_val,
......@@ -4552,6 +4557,7 @@ fn analyzeCall(
45524557 },
45534558 else => {},
45544559 }
4560 should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
45554561 memoized_call_key.args[arg_i] = .{
45564562 .ty = sema.typeOf(uncasted_arg),
45574563 .val = arg_val,
......@@ -4597,7 +4603,7 @@ fn analyzeCall(
45974603 // This `res2` is here instead of directly breaking from `res` due to a stage1
45984604 // bug generating invalid LLVM IR.
45994605 const res2: Air.Inst.Ref = res2: {
4600 if (is_comptime_call) {
4606 if (should_memoize and is_comptime_call) {
46014607 if (mod.memoized_calls.get(memoized_call_key)) |result| {
46024608 const ty_inst = try sema.addType(fn_ret_ty);
46034609 try sema.air_values.append(gpa, result.val);
......@@ -4621,7 +4627,7 @@ fn analyzeCall(
46214627 break :result try sema.analyzeBlockBody(block, call_src, &child_block, merges);
46224628 };
46234629
4624 if (is_comptime_call) {
4630 if (should_memoize and is_comptime_call) {
46254631 const result_val = try sema.resolveConstMaybeUndefVal(block, call_src, result);
46264632
46274633 // TODO: check whether any external comptime memory was mutated by the
......@@ -10975,8 +10981,7 @@ fn zirCondbr(
1097510981
1097610982 if (try sema.resolveDefinedValue(parent_block, src, cond)) |cond_val| {
1097710983 const body = if (cond_val.toBool()) then_body else else_body;
10978 _ = try sema.analyzeBody(parent_block, body);
10979 return always_noreturn;
10984 return sema.analyzeBodyInner(parent_block, body);
1098010985 }
1098110986
1098210987 const gpa = sema.gpa;
......@@ -11948,19 +11953,27 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1194811953 var buffer: Value.ToTypeBuffer = undefined;
1194911954 const child_ty = child_val.toType(&buffer);
1195011955
11956 const ptr_size = size_val.toEnum(std.builtin.TypeInfo.Pointer.Size);
11957
11958 var actual_sentinel: ?Value = null;
1195111959 if (!sentinel_val.isNull()) {
11952 return sema.fail(block, src, "TODO: implement zirReify for pointer with non-null sentinel", .{});
11960 if (ptr_size == .One or ptr_size == .C) {
11961 return sema.fail(block, src, "sentinels are only allowed on slices and unknown-length pointers", .{});
11962 }
11963 const sentinel_ptr_val = sentinel_val.castTag(.opt_payload).?.data;
11964 const ptr_ty = try Type.ptr(sema.arena, .{ .@"addrspace" = .generic, .pointee_type = child_ty });
11965 actual_sentinel = (try sema.pointerDeref(block, src, sentinel_ptr_val, ptr_ty)).?;
1195311966 }
1195411967
1195511968 const ty = try Type.ptr(sema.arena, .{
11956 .size = size_val.toEnum(std.builtin.TypeInfo.Pointer.Size),
11969 .size = ptr_size,
1195711970 .mutable = !is_const_val.toBool(),
1195811971 .@"volatile" = is_volatile_val.toBool(),
1195911972 .@"align" = @intCast(u8, alignment_val.toUnsignedInt()), // TODO: Validate this value.
1196011973 .@"addrspace" = address_space_val.toEnum(std.builtin.AddressSpace),
1196111974 .pointee_type = try child_ty.copy(sema.arena),
1196211975 .@"allowzero" = is_allowzero_val.toBool(),
11963 .sentinel = null,
11976 .sentinel = actual_sentinel,
1196411977 });
1196511978 return sema.addType(ty);
1196611979 },
......@@ -12070,6 +12083,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1207012083 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1207112084 const type_res = try sema.resolveType(block, src, extra.lhs);
1207212085 try sema.checkPtrType(block, type_src, type_res);
12086 _ = try sema.resolveTypeLayout(block, src, type_res.childType());
1207312087 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
1207412088
1207512089 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
......@@ -17587,7 +17601,7 @@ fn resolvePeerTypes(
1758717601 return chosen_ty;
1758817602}
1758917603
17590pub fn resolveTypeLayout(
17604fn resolveTypeLayout(
1759117605 sema: *Sema,
1759217606 block: *Block,
1759317607 src: LazySrcLoc,
......@@ -17660,7 +17674,7 @@ fn resolveUnionLayout(
1766017674 union_obj.status = .have_layout;
1766117675}
1766217676
17663fn resolveTypeFully(
17677pub fn resolveTypeFully(
1766417678 sema: *Sema,
1766517679 block: *Block,
1766617680 src: LazySrcLoc,
src/codegen/llvm.zig+1-1
......@@ -3892,7 +3892,7 @@ pub const FuncGen = struct {
38923892 return self.builder.buildBitCast(operand, llvm_dest_ty.pointerType(0), "");
38933893 }
38943894
3895 if (operand_ty.zigTypeTag() == .Int and inst_ty.zigTypeTag() == .Pointer) {
3895 if (operand_ty.zigTypeTag() == .Int and inst_ty.isPtrAtRuntime()) {
38963896 return self.builder.buildIntToPtr(operand, llvm_dest_ty, "");
38973897 }
38983898
src/type.zig+1-1
......@@ -2602,7 +2602,7 @@ pub const Type = extern union {
26022602 const payload = self.castTag(.pointer).?.data;
26032603 return payload.@"allowzero";
26042604 },
2605 else => false,
2605 else => return self.zigTypeTag() == .Optional,
26062606 };
26072607 }
26082608
test/behavior.zig+2-2
......@@ -125,6 +125,7 @@ test {
125125 _ = @import("behavior/widening.zig");
126126 _ = @import("behavior/bugs/421.zig");
127127 _ = @import("behavior/bugs/726.zig");
128 _ = @import("behavior/bugs/828.zig");
128129 _ = @import("behavior/bugs/1421.zig");
129130 _ = @import("behavior/bugs/1442.zig");
130131 _ = @import("behavior/bugs/1607.zig");
......@@ -132,6 +133,7 @@ test {
132133 _ = @import("behavior/bugs/3384.zig");
133134 _ = @import("behavior/bugs/3742.zig");
134135 _ = @import("behavior/bugs/5398.zig");
136 _ = @import("behavior/bugs/5413.zig");
135137 _ = @import("behavior/bugs/5487.zig");
136138 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
137139 _ = @import("behavior/switch_prong_err_enum.zig");
......@@ -148,12 +150,10 @@ test {
148150 _ = @import("behavior/await_struct.zig");
149151 _ = @import("behavior/bugs/529.zig");
150152 _ = @import("behavior/bugs/718.zig");
151 _ = @import("behavior/bugs/828.zig");
152153 _ = @import("behavior/bugs/920.zig");
153154 _ = @import("behavior/bugs/1120.zig");
154155 _ = @import("behavior/bugs/1851.zig");
155156 _ = @import("behavior/bugs/3779.zig");
156 _ = @import("behavior/bugs/5413.zig");
157157 _ = @import("behavior/bugs/6456.zig");
158158 _ = @import("behavior/bugs/6781.zig");
159159 _ = @import("behavior/bugs/7003.zig");
test/behavior/basic.zig+34
......@@ -819,3 +819,37 @@ test "if expression type coercion" {
819819 const x: u16 = if (cond) 1 else 0;
820820 try expect(@as(u16, x) == 1);
821821}
822
823test "discarding the result of various expressions" {
824 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
825 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
826 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
827
828 const S = struct {
829 fn foo() !u32 {
830 return 1;
831 }
832 fn bar() ?u32 {
833 return 1;
834 }
835 };
836 _ = S.bar() orelse {
837 // do nothing
838 };
839 _ = S.foo() catch {
840 // do nothing
841 };
842 _ = switch (1) {
843 1 => 1,
844 2 => {},
845 else => return,
846 };
847 _ = try S.foo();
848 _ = if (S.bar()) |some| some else {};
849 _ = blk: {
850 if (S.bar()) |some| break :blk some;
851 break :blk;
852 };
853 _ = while (S.bar()) |some| break some else {};
854 _ = for ("foo") |char| break char else {};
855}