authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-26 17:41:26-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-26 17:41:26-07:00
logc8c798685f3a7d6454bc06d5f47531ad6f615eb5
treea656fc1e402c6b8b4bb6d3b1c6ac9914ed627f71
parente2d4709779d23c83edf26db2d92f4a326b790825
parentd6e3988fe846864a33adf9f5f04d800718b31392
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12244 from Vexu/stage2

Minor stage2 fixes

22 files changed, 149 insertions(+), 91 deletions(-)

src/Sema.zig+49-15
......@@ -2869,7 +2869,16 @@ fn ensureResultUsed(
28692869 };
28702870 return sema.failWithOwnedErrorMsg(block, msg);
28712871 },
2872 else => return sema.fail(block, src, "expression value is ignored", .{}),
2872 else => {
2873 const msg = msg: {
2874 const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{operand_ty.fmt(sema.mod)});
2875 errdefer msg.destroy(sema.gpa);
2876 try sema.errNote(block, src, msg, "all non-void values must be used", .{});
2877 try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
2878 break :msg msg;
2879 };
2880 return sema.failWithOwnedErrorMsg(block, msg);
2881 },
28732882 }
28742883}
28752884
......@@ -4508,6 +4517,7 @@ fn zirCompileLog(
45084517 const arg = try sema.resolveInst(arg_ref);
45094518 const arg_ty = sema.typeOf(arg);
45104519 if (try sema.resolveMaybeUndefVal(block, src, arg)) |val| {
4520 try sema.resolveLazyValue(block, src, val);
45114521 try writer.print("@as({}, {})", .{
45124522 arg_ty.fmt(sema.mod), val.fmtValue(arg_ty, sema.mod),
45134523 });
......@@ -5498,7 +5508,7 @@ fn analyzeCall(
54985508 // TODO add error note: declared here
54995509 return sema.fail(
55005510 block,
5501 func_src,
5511 call_src,
55025512 "expected {d} argument(s), found {d}",
55035513 .{ fn_params_len, uncasted_args.len },
55045514 );
......@@ -8918,6 +8928,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
89188928
89198929 if (special_prong == .@"else" and seen_errors.count() == operand_ty.errorSetNames().len) {
89208930
8931 // TODO re-enable if defer implementation is improved
8932 // https://github.com/ziglang/zig/issues/11798
8933 if (true) break :else_validation;
8934
89218935 // In order to enable common patterns for generic code allow simple else bodies
89228936 // else => unreachable,
89238937 // else => return,
......@@ -20592,6 +20606,14 @@ fn tupleFieldPtr(
2059220606 .@"addrspace" = tuple_ptr_ty.ptrAddressSpace(),
2059320607 });
2059420608
20609 if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
20610 const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
20611 .field_ty = field_ty,
20612 .field_val = default_val,
20613 });
20614 return sema.addConstant(ptr_field_ty, val);
20615 }
20616
2059520617 if (try sema.resolveMaybeUndefVal(block, tuple_ptr_src, tuple_ptr)) |tuple_ptr_val| {
2059620618 return sema.addConstant(
2059720619 ptr_field_ty,
......@@ -20603,14 +20625,6 @@ fn tupleFieldPtr(
2060320625 );
2060420626 }
2060520627
20606 if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
20607 const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
20608 .field_ty = field_ty,
20609 .field_val = default_val,
20610 });
20611 return sema.addConstant(ptr_field_ty, val);
20612 }
20613
2061420628 if (!init) {
2061520629 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
2061620630 }
......@@ -21354,7 +21368,7 @@ fn coerceExtra(
2135421368 else => {},
2135521369 },
2135621370 .ErrorUnion => switch (inst_ty.zigTypeTag()) {
21357 .ErrorUnion => {
21371 .ErrorUnion => eu: {
2135821372 if (maybe_inst_val) |inst_val| {
2135921373 switch (inst_val.tag()) {
2136021374 .undef => return sema.addConstUndef(dest_ty),
......@@ -21363,7 +21377,10 @@ fn coerceExtra(
2136321377 inst_ty.errorUnionPayload(),
2136421378 inst_val.castTag(.eu_payload).?.data,
2136521379 );
21366 return sema.wrapErrorUnionPayload(block, dest_ty, payload, inst_src);
21380 return sema.wrapErrorUnionPayload(block, dest_ty, payload, inst_src) catch |err| switch (err) {
21381 error.NotCoercible => break :eu,
21382 else => |e| return e,
21383 };
2136721384 },
2136821385 else => {
2136921386 const error_set = try sema.addConstant(
......@@ -21382,9 +21399,12 @@ fn coerceExtra(
2138221399 .Undefined => {
2138321400 return sema.addConstUndef(dest_ty);
2138421401 },
21385 else => {
21402 else => eu: {
2138621403 // T to E!T
21387 return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src);
21404 return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src) catch |err| switch (err) {
21405 error.NotCoercible => break :eu,
21406 else => |e| return e,
21407 };
2138821408 },
2138921409 },
2139021410 .Union => switch (inst_ty.zigTypeTag()) {
......@@ -23334,6 +23354,16 @@ fn beginComptimePtrLoad(
2333423354 break :blk deref;
2333523355 },
2333623356
23357 .comptime_field_ptr => blk: {
23358 const comptime_field_ptr = ptr_val.castTag(.comptime_field_ptr).?.data;
23359 break :blk ComptimePtrLoadKit{
23360 .parent = null,
23361 .pointee = .{ .ty = comptime_field_ptr.field_ty, .val = comptime_field_ptr.field_val },
23362 .is_mutable = false,
23363 .ty_without_well_defined_layout = comptime_field_ptr.field_ty,
23364 };
23365 },
23366
2333723367 .opt_payload_ptr,
2333823368 .eu_payload_ptr,
2333923369 => blk: {
......@@ -24864,7 +24894,7 @@ fn wrapErrorUnionPayload(
2486424894 inst_src: LazySrcLoc,
2486524895) !Air.Inst.Ref {
2486624896 const dest_payload_ty = dest_ty.errorUnionPayload();
24867 const coerced = try sema.coerce(block, dest_payload_ty, inst, inst_src);
24897 const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, false, false);
2486824898 if (try sema.resolveMaybeUndefVal(block, inst_src, coerced)) |val| {
2486924899 return sema.addConstant(dest_ty, try Value.Tag.eu_payload.create(sema.arena, val));
2487024900 }
......@@ -25523,6 +25553,10 @@ fn resolveLazyValue(
2552325553 const ty = val.castTag(.lazy_align).?.data;
2552425554 return sema.resolveTypeLayout(block, src, ty);
2552525555 },
25556 .lazy_size => {
25557 const ty = val.castTag(.lazy_size).?.data;
25558 return sema.resolveTypeLayout(block, src, ty);
25559 },
2552625560 else => return,
2552725561 }
2552825562}
test/cases/compile_errors/compile_log.zig+5
......@@ -6,9 +6,14 @@ fn bar(a: i32, b: []const u8) void {
66 @compileLog("a", a, "b", b);
77 @compileLog("end",);
88}
9export fn baz() void {
10 const S = struct { a: u32 };
11 @compileLog(@sizeOf(S));
12}
913
1014// error
1115// backend=llvm
1216// target=native
1317//
1418// :5:5: error: found compile log statement
19// :11:5: note: also here
test/cases/compile_errors/ignored_assert-err-ok_return_value.zig+3-1
......@@ -7,4 +7,6 @@ fn bar() anyerror!i32 { return 0; }
77// backend=stage2
88// target=native
99//
10// :2:11: error: expression value is ignored
10// :2:11: error: value of type 'i32' ignored
11// :2:11: note: all non-void values must be used
12// :2:11: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/ignored_comptime_statement_value.zig+3-1
......@@ -6,4 +6,6 @@ export fn foo() void {
66// backend=stage2
77// target=native
88//
9// :2:15: error: expression value is ignored
9// :2:15: error: value of type 'comptime_int' ignored
10// :2:15: note: all non-void values must be used
11// :2:15: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/ignored_comptime_value.zig+3-1
......@@ -6,4 +6,6 @@ export fn foo() void {
66// backend=stage2
77// target=native
88//
9// :2:5: error: expression value is ignored
9// :2:5: error: value of type 'comptime_int' ignored
10// :2:5: note: all non-void values must be used
11// :2:5: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/ignored_deferred_statement_value.zig+3-1
......@@ -6,4 +6,6 @@ export fn foo() void {
66// backend=stage2
77// target=native
88//
9// :2:12: error: expression value is ignored
9// :2:12: error: value of type 'comptime_int' ignored
10// :2:12: note: all non-void values must be used
11// :2:12: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/ignored_return_value.zig+3-1
......@@ -7,4 +7,6 @@ fn bar() i32 { return 0; }
77// backend=stage2
88// target=native
99//
10// :2:8: error: expression value is ignored
10// :2:8: error: value of type 'i32' ignored
11// :2:8: note: all non-void values must be used
12// :2:8: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/ignored_statement_value.zig+3-1
......@@ -6,4 +6,6 @@ export fn foo() void {
66// backend=stage2
77// target=native
88//
9// :2:5: error: expression value is ignored
9// :2:5: error: value of type 'comptime_int' ignored
10// :2:5: note: all non-void values must be used
11// :2:5: note: this error can be suppressed by assigning the value to '_'
test/cases/compile_errors/invalid_store_to_comptime_field.zig+5
......@@ -40,6 +40,10 @@ pub export fn entry4() void {
4040 };
4141 _ = U.foo(.{ .foo = 2, .bar = 2 });
4242}
43pub export fn entry5() void {
44 comptime var y = .{ 1, 2};
45 y = .{ 3, 4 };
46}
4347// pub export fn entry5() void {
4448// var x: u32 = 15;
4549// const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
......@@ -60,3 +64,4 @@ pub export fn entry4() void {
6064// :31:19: error: value stored in comptime field does not match the default value of the field
6165// :25:29: note: default value set here
6266// :41:16: error: value stored in comptime field does not match the default value of the field
67// :45:12: error: value stored in comptime field does not match the default value of the field
test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig created+11
......@@ -0,0 +1,11 @@
1export fn entry() void {
2 var foo: u32 = @This(){};
3 _ = foo;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :2:27: error: expected type 'u32', found 'tmp.tmp'
11// :1:1: note: struct declared here
test/cases/compile_errors/nested_error_set_mismatch.zig created+19
......@@ -0,0 +1,19 @@
1const NextError = error{NextError};
2const OtherError = error{OutOfMemory};
3
4export fn entry() void {
5 const a: ?NextError!i32 = foo();
6 _ = a;
7}
8
9fn foo() ?OtherError!i32 {
10 return null;
11}
12
13// error
14// backend=llvm
15// target=native
16//
17// :4:1: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
18// :4:1: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
19// :4:1: note: 'error.OutOfMemory' not a member of destination error set
test/cases/compile_errors/return_invalid_type_from_test.zig+1-1
......@@ -5,4 +5,4 @@ test "example" { return 1; }
55// target=native
66// is_test=1
77//
8// :1:25: error: expected type 'void', found 'comptime_int'
8// :1:25: error: expected type '@typeInfo(@typeInfo(@TypeOf(tmp.test.example)).Fn.return_type.?).ErrorUnion.error_set!void', found 'comptime_int'
\ No newline at end of file
test/cases/compile_errors/stage1/obj/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn entry() void {
2 var foo: u32 = @This(){};
3 _ = foo;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:27: error: type 'u32' does not support array initialization
test/cases/compile_errors/stage1/obj/nested_error_set_mismatch.zig deleted-20
......@@ -1,20 +0,0 @@
1const NextError = error{NextError};
2const OtherError = error{OutOfMemory};
3
4export fn entry() void {
5 const a: ?NextError!i32 = foo();
6 _ = a;
7}
8
9fn foo() ?OtherError!i32 {
10 return null;
11}
12
13// error
14// backend=stage1
15// target=native
16//
17// tmp.zig:5:34: error: expected type '?NextError!i32', found '?OtherError!i32'
18// tmp.zig:5:34: note: optional type child 'OtherError!i32' cannot cast into optional type child 'NextError!i32'
19// tmp.zig:5:34: note: error set 'OtherError' cannot cast into error set 'NextError'
20// tmp.zig:2:26: note: 'error.OutOfMemory' not a member of destination error set
test/cases/compile_errors/stage1/obj/std.fmt_error_for_unused_arguments.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() void {
2 @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// ?:?:?: error: 10 unused arguments in '{d} {d} {d} {d} {d}'
test/cases/compile_errors/stage1/obj/wrong_number_of_arguments.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn a() void {
2 c(1);
3}
4fn c(d: i32, e: i32, f: i32) void { _ = d; _ = e; _ = f; }
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:6: error: expected 3 argument(s), found 1
test/cases/compile_errors/stage1/obj/wrong_types_given_to_atomic_order_args_in_cmpxchg.zig deleted-10
......@@ -1,10 +0,0 @@
1export fn entry() void {
2 var x: i32 = 1234;
3 while (!@cmpxchgWeak(i32, &x, 1234, 5678, @as(u32, 1234), @as(u32, 1234))) {}
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:3:47: error: expected type 'std.builtin.AtomicOrder', found 'u32'
test/cases/compile_errors/stage1/obj/wrong_types_given_to_export.zig deleted-10
......@@ -1,10 +0,0 @@
1fn entry() callconv(.C) void { }
2comptime {
3 @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:3:59: error: expected type 'std.builtin.GlobalLinkage', found 'comptime_int'
test/cases/compile_errors/std.fmt_error_for_unused_arguments.zig created+9
......@@ -0,0 +1,9 @@
1export fn entry() void {
2 @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});
3}
4
5// error
6// backend=llvm
7// target=native
8//
9// :?:?: error: 10 unused arguments in '{d} {d} {d} {d} {d}'
test/cases/compile_errors/wrong_number_of_arguments.zig created+10
......@@ -0,0 +1,10 @@
1export fn a() void {
2 c(1);
3}
4fn c(d: i32, e: i32, f: i32) void { _ = d; _ = e; _ = f; }
5
6// error
7// backend=stage2
8// target=native
9//
10// :2:6: error: expected 3 argument(s), found 1
test/cases/compile_errors/wrong_types_given_to_atomic_order_args_in_cmpxchg.zig created+11
......@@ -0,0 +1,11 @@
1export fn entry() void {
2 var x: i32 = 1234;
3 while (!@cmpxchgWeak(i32, &x, 1234, 5678, @as(u32, 1234), @as(u32, 1234))) {}
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :3:47: error: expected type 'builtin.AtomicOrder', found 'u32'
11// :?:?: note: enum declared here
test/cases/compile_errors/wrong_types_given_to_export.zig created+11
......@@ -0,0 +1,11 @@
1fn entry() callconv(.C) void { }
2comptime {
3 @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :3:50: error: expected type 'builtin.GlobalLinkage', found 'u32'
11// :?:?: note: enum declared here