diff --git a/src/AstGen.zig b/src/AstGen.zig index 3850fe84a78fd841ca8b41718d957ea1118d6138..e30913ac76e4459136e5c075a278c230bf0f7a96 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2696,6 +2696,7 @@ fn genDefers( break :blk &local_val_scope.base; }; try unusedResultDeferExpr(gz, defer_scope, sub_scope, expr_node); + try checkUsed(gz, scope, sub_scope); try gz.addDbgBlockEnd(); }, .normal_only => continue, @@ -5384,7 +5385,7 @@ fn ifExpr( const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_err_ptr else .is_non_err; break :c .{ .inst = err_union, - .bool_bit = try block_scope.addUnNode(tag, err_union, node), + .bool_bit = try block_scope.addUnNode(tag, err_union, if_full.ast.cond_expr), }; } else if (if_full.payload_token) |_| { const cond_rl: ResultLoc = if (payload_is_ref) .ref else .none; @@ -5392,7 +5393,7 @@ fn ifExpr( const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_null_ptr else .is_non_null; break :c .{ .inst = optional, - .bool_bit = try block_scope.addUnNode(tag, optional, node), + .bool_bit = try block_scope.addUnNode(tag, optional, if_full.ast.cond_expr), }; } else { const cond = try expr(&block_scope, &block_scope.base, bool_rl, if_full.ast.cond_expr); @@ -5423,7 +5424,7 @@ fn ifExpr( .err_union_payload_unsafe_ptr else .err_union_payload_unsafe; - const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); + const payload_inst = try then_scope.addUnNode(tag, cond.inst, if_full.ast.then_expr); const token_name_index = payload_token + @boolToInt(payload_is_ref); const ident_name = try astgen.identAsString(token_name_index); const token_name_str = tree.tokenSlice(token_name_index); @@ -5452,7 +5453,7 @@ fn ifExpr( const ident_bytes = tree.tokenSlice(ident_token); if (mem.eql(u8, "_", ident_bytes)) break :s &then_scope.base; - const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); + const payload_inst = try then_scope.addUnNode(tag, cond.inst, if_full.ast.then_expr); const ident_name = try astgen.identAsString(ident_token); try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes); payload_val_scope = .{ @@ -5495,7 +5496,7 @@ fn ifExpr( .err_union_code_ptr else .err_union_code; - const payload_inst = try else_scope.addUnNode(tag, cond.inst, node); + const payload_inst = try else_scope.addUnNode(tag, cond.inst, if_full.ast.cond_expr); const ident_name = try astgen.identAsString(error_token); const error_token_str = tree.tokenSlice(error_token); if (mem.eql(u8, "_", error_token_str)) @@ -5709,7 +5710,7 @@ fn whileExpr( const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_err_ptr else .is_non_err; break :c .{ .inst = err_union, - .bool_bit = try continue_scope.addUnNode(tag, err_union, node), + .bool_bit = try continue_scope.addUnNode(tag, err_union, while_full.ast.then_expr), }; } else if (while_full.payload_token) |_| { const cond_rl: ResultLoc = if (payload_is_ref) .ref else .none; @@ -5717,7 +5718,7 @@ fn whileExpr( const tag: Zir.Inst.Tag = if (payload_is_ref) .is_non_null_ptr else .is_non_null; break :c .{ .inst = optional, - .bool_bit = try continue_scope.addUnNode(tag, optional, node), + .bool_bit = try continue_scope.addUnNode(tag, optional, while_full.ast.then_expr), }; } else { const cond = try expr(&continue_scope, &continue_scope.base, bool_rl, while_full.ast.cond_expr); @@ -5755,7 +5756,7 @@ fn whileExpr( else .err_union_payload_unsafe; // will add this instruction to then_scope.instructions below - payload_inst = try then_scope.makeUnNode(tag, cond.inst, node); + payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr); const ident_token = if (payload_is_ref) payload_token + 1 else payload_token; const ident_bytes = tree.tokenSlice(ident_token); if (mem.eql(u8, "_", ident_bytes)) @@ -5784,7 +5785,7 @@ fn whileExpr( else .optional_payload_unsafe; // will add this instruction to then_scope.instructions below - payload_inst = try then_scope.makeUnNode(tag, cond.inst, node); + payload_inst = try then_scope.makeUnNode(tag, cond.inst, while_full.ast.cond_expr); const ident_name = try astgen.identAsString(ident_token); const ident_bytes = tree.tokenSlice(ident_token); if (mem.eql(u8, "_", ident_bytes)) @@ -5860,7 +5861,7 @@ fn whileExpr( .err_union_code_ptr else .err_union_code; - const else_payload_inst = try else_scope.addUnNode(tag, cond.inst, node); + const else_payload_inst = try else_scope.addUnNode(tag, cond.inst, while_full.ast.cond_expr); const ident_name = try astgen.identAsString(error_token); const ident_bytes = tree.tokenSlice(error_token); if (mem.eql(u8, ident_bytes, "_")) diff --git a/test/cases/compile_errors/packed_union_given_enum_tag_type.zig b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig new file mode 100644 index 0000000000000000000000000000000000000000..03aaef0d8cd560a461cc0673594eec990717c73d --- /dev/null +++ b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig @@ -0,0 +1,20 @@ +const Letter = enum { + A, + B, + C, +}; +const Payload = packed union(Letter) { + A: i32, + B: f64, + C: bool, +}; +export fn entry() void { + var a = Payload { .A = 1234 }; + _ = a; +} + +// error +// backend=stage2 +// target=native +// +// :6:30: error: packed union does not support enum tag type diff --git a/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig b/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig new file mode 100644 index 0000000000000000000000000000000000000000..97771e9b784347306f253ddf688a7f516e4afd0f --- /dev/null +++ b/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig @@ -0,0 +1,20 @@ +const Foo = struct { + a: u32, + b: f32, +}; +const Payload = packed union { + A: Foo, + B: bool, +}; +export fn entry() void { + var a = Payload { .B = true }; + _ = a; +} + +// error +// backend=stage2 +// target=native +// +// :6:5: error: packed unions cannot contain fields of type 'tmp.Foo' +// :6:5: note: only packed structs layout are allowed in packed types +// :1:13: note: struct declared here diff --git a/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig new file mode 100644 index 0000000000000000000000000000000000000000..f2ff3e2cd142254ec001de6e54bc88277b9c6e7c --- /dev/null +++ b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig @@ -0,0 +1,16 @@ +const Small = enum (f32) { + One, + Two, + Three, +}; + +export fn entry() void { + var x = Small.One; + _ = x; +} + +// error +// backend=stage2 +// target=native +// +// :1:21: error: expected integer tag type, found 'f32' diff --git a/test/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig b/test/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig deleted file mode 100644 index fceb7af65c7c643273d0b490ef1884042296505e..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig +++ /dev/null @@ -1,20 +0,0 @@ -const Letter = enum { - A, - B, - C, -}; -const Payload = packed union(Letter) { - A: i32, - B: f64, - C: bool, -}; -export fn entry() void { - var a = Payload { .A = 1234 }; - _ = a; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:6:30: error: packed union does not support enum tag type diff --git a/test/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig b/test/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig deleted file mode 100644 index 99ad6ca306aaeb666ef9b62cc4fb31c2f045486b..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig +++ /dev/null @@ -1,18 +0,0 @@ -const Foo = struct { - a: u32, - b: f32, -}; -const Payload = packed union { - A: Foo, - B: bool, -}; -export fn entry() void { - var a = Payload { .B = true }; - _ = a; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:6:5: error: non-packed, non-extern struct 'Foo' not allowed in packed union; no guaranteed in-memory representation diff --git a/test/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig b/test/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig deleted file mode 100644 index 333647e1e392afb22f7596385ceace2d6a01a48c..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig +++ /dev/null @@ -1,16 +0,0 @@ -const Small = enum (f32) { - One, - Two, - Three, -}; - -export fn entry() void { - var x = Small.One; - _ = x; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:1:21: error: expected integer, found 'f32' diff --git a/test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig b/test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig deleted file mode 100644 index b85d5729dcf8f62c23cdee35a1a3fa873ff42768..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig +++ /dev/null @@ -1,13 +0,0 @@ -fn foo() !void { - errdefer |a| unreachable; - return error.A; -} -export fn entry() void { - foo() catch unreachable; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:15: error: unused variable: 'a' diff --git a/test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig b/test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig deleted file mode 100644 index fdffd8b45567777d9dc61263f24a724d019a9431..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig +++ /dev/null @@ -1,10 +0,0 @@ -export fn entry() void { - const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 }; - _ = x; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:62: error: index 3 outside vector of size 3 diff --git a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig b/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig deleted file mode 100644 index b8a72e97937e454aa404e0c744fef8549d33d4ce..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig +++ /dev/null @@ -1,10 +0,0 @@ -export fn foo() void { - while (bar()) |x| {_ = x;} else |err| {_ = err;} -} -fn bar() bool { return true; } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:15: error: expected error union type, found 'bool' diff --git a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig b/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig deleted file mode 100644 index c933dc95092aa2bc4637f9dd9498cfe8771496a1..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig +++ /dev/null @@ -1,10 +0,0 @@ -export fn foo() void { - while (bar()) |x| {_ = x;} else |err| {_ = err;} -} -fn bar() ?i32 { return 1; } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:15: error: expected error union type, found '?i32' diff --git a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig b/test/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig deleted file mode 100644 index 0458d1ba0179951341bfbbf6e2d78df4ab315a02..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig +++ /dev/null @@ -1,10 +0,0 @@ -export fn foo() void { - while (bar()) |x| {_ = x;} -} -fn bar() bool { return true; } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:15: error: expected optional type, found 'bool' diff --git a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig b/test/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig deleted file mode 100644 index 7cdbd2cccf69ad800659f21f13eabaa6177f6757..0000000000000000000000000000000000000000 --- a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig +++ /dev/null @@ -1,10 +0,0 @@ -export fn foo() void { - while (bar()) |x| {_ = x;} -} -fn bar() anyerror!i32 { return 1; } - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:15: error: expected optional type, found 'anyerror!i32' diff --git a/test/cases/compile_errors/unused_variable_error_on_errdefer.zig b/test/cases/compile_errors/unused_variable_error_on_errdefer.zig new file mode 100644 index 0000000000000000000000000000000000000000..4c37af04ebc58858d1cdc4d66b00d39362be1b0a --- /dev/null +++ b/test/cases/compile_errors/unused_variable_error_on_errdefer.zig @@ -0,0 +1,13 @@ +fn foo() !void { + errdefer |a| unreachable; + return error.A; +} +export fn entry() void { + foo() catch unreachable; +} + +// error +// backend=stage2 +// target=native +// +// :2:15: error: unused capture diff --git a/test/cases/compile_errors/vector_index_out_of_bounds.zig b/test/cases/compile_errors/vector_index_out_of_bounds.zig new file mode 100644 index 0000000000000000000000000000000000000000..ed1a25a32150006332f3b8c165b8ebd65b77f866 --- /dev/null +++ b/test/cases/compile_errors/vector_index_out_of_bounds.zig @@ -0,0 +1,10 @@ +export fn entry() void { + const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 }; + _ = x; +} + +// error +// backend=stage2 +// target=native +// +// :2:49: error: expected 3 vector elements; found 4 diff --git a/test/cases/compile_errors/while_expected_error_union_got_bool.zig b/test/cases/compile_errors/while_expected_error_union_got_bool.zig new file mode 100644 index 0000000000000000000000000000000000000000..f7960437ec55619ecc0fd137fa99f5c6f109d08f --- /dev/null +++ b/test/cases/compile_errors/while_expected_error_union_got_bool.zig @@ -0,0 +1,10 @@ +export fn foo() void { + while (bar()) |x| {_ = x;} else |err| {_ = err;} +} +fn bar() bool { return true; } + +// error +// backend=stage2 +// target=native +// +// :2:15: error: expected error union type, found 'bool' diff --git a/test/cases/compile_errors/while_expected_error_union_got_optional.zig b/test/cases/compile_errors/while_expected_error_union_got_optional.zig new file mode 100644 index 0000000000000000000000000000000000000000..5cabd76fce3fdca65a4f32eab0f45f8a0d91e9f4 --- /dev/null +++ b/test/cases/compile_errors/while_expected_error_union_got_optional.zig @@ -0,0 +1,10 @@ +export fn foo() void { + while (bar()) |x| {_ = x;} else |err| {_ = err;} +} +fn bar() ?i32 { return 1; } + +// error +// backend=stage2 +// target=native +// +// :2:15: error: expected error union type, found '?i32' diff --git a/test/cases/compile_errors/while_expected_optional_got_bool.zig b/test/cases/compile_errors/while_expected_optional_got_bool.zig new file mode 100644 index 0000000000000000000000000000000000000000..22b8c1e58c732c4ad7125772fa47562655e80adc --- /dev/null +++ b/test/cases/compile_errors/while_expected_optional_got_bool.zig @@ -0,0 +1,10 @@ +export fn foo() void { + while (bar()) |x| {_ = x;} +} +fn bar() bool { return true; } + +// error +// backend=stage2 +// target=native +// +// :2:15: error: expected optional type, found 'bool' diff --git a/test/cases/compile_errors/while_expected_optional_got_error_union.zig b/test/cases/compile_errors/while_expected_optional_got_error_union.zig new file mode 100644 index 0000000000000000000000000000000000000000..38a8a0dd20387ea7b1ec00426ed1073b970db83c --- /dev/null +++ b/test/cases/compile_errors/while_expected_optional_got_error_union.zig @@ -0,0 +1,10 @@ +export fn foo() void { + while (bar()) |x| {_ = x;} +} +fn bar() anyerror!i32 { return 1; } + +// error +// backend=stage2 +// target=native +// +// :2:15: error: expected optional type, found 'anyerror!i32'