From b79929b2eaa634b756fe374372d59718f4f8479a Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Tue, 2 Aug 2022 21:01:20 +0300 Subject: [PATCH] AstGen: better source location for if/while condition unwrapping --- src/AstGen.zig | 21 ++++++++++--------- .../packed_union_given_enum_tag_type.zig | 4 ++-- ...cked_union_with_automatic_layout_field.zig | 6 ++++-- .../specify_non-integer_enum_tag_type.zig | 4 ++-- .../unused_variable_error_on_errdefer.zig | 4 ++-- .../obj => }/vector_index_out_of_bounds.zig | 4 ++-- .../while_expected_error_union_got_bool.zig | 4 ++-- ...hile_expected_error_union_got_optional.zig | 4 ++-- .../while_expected_optional_got_bool.zig | 4 ++-- ...hile_expected_optional_got_error_union.zig | 4 ++-- 10 files changed, 31 insertions(+), 28 deletions(-) rename test/cases/compile_errors/{stage1/obj => }/packed_union_given_enum_tag_type.zig (72%) rename test/cases/compile_errors/{stage1/obj => }/packed_union_with_automatic_layout_field.zig (51%) rename test/cases/compile_errors/{stage1/obj => }/specify_non-integer_enum_tag_type.zig (67%) rename test/cases/compile_errors/{stage1/obj => }/unused_variable_error_on_errdefer.zig (71%) rename test/cases/compile_errors/{stage1/obj => }/vector_index_out_of_bounds.zig (64%) rename test/cases/compile_errors/{stage1/obj => }/while_expected_error_union_got_bool.zig (62%) rename test/cases/compile_errors/{stage1/obj => }/while_expected_error_union_got_optional.zig (62%) rename test/cases/compile_errors/{stage1/obj => }/while_expected_optional_got_bool.zig (59%) rename test/cases/compile_errors/{stage1/obj => }/while_expected_optional_got_error_union.zig (58%) 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/stage1/obj/packed_union_given_enum_tag_type.zig b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig similarity index 72% rename from test/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig rename to test/cases/compile_errors/packed_union_given_enum_tag_type.zig index fceb7af65c7c643273d0b490ef1884042296505e..03aaef0d8cd560a461cc0673594eec990717c73d 100644 --- a/test/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig +++ b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig @@ -14,7 +14,7 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:6:30: error: packed union does not support enum tag type +// :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/packed_union_with_automatic_layout_field.zig similarity index 51% rename from test/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig rename to test/cases/compile_errors/packed_union_with_automatic_layout_field.zig index 99ad6ca306aaeb666ef9b62cc4fb31c2f045486b..97771e9b784347306f253ddf688a7f516e4afd0f 100644 --- a/test/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig +++ b/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig @@ -12,7 +12,9 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:6:5: error: non-packed, non-extern struct 'Foo' not allowed in packed union; no guaranteed in-memory representation +// :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/stage1/obj/specify_non-integer_enum_tag_type.zig b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig similarity index 67% rename from test/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig rename to test/cases/compile_errors/specify_non-integer_enum_tag_type.zig index 333647e1e392afb22f7596385ceace2d6a01a48c..f2ff3e2cd142254ec001de6e54bc88277b9c6e7c 100644 --- a/test/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig +++ b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig @@ -10,7 +10,7 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:1:21: error: expected integer, found 'f32' +// :1:21: error: expected integer tag type, found 'f32' diff --git a/test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig b/test/cases/compile_errors/unused_variable_error_on_errdefer.zig similarity index 71% rename from test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig rename to test/cases/compile_errors/unused_variable_error_on_errdefer.zig index b85d5729dcf8f62c23cdee35a1a3fa873ff42768..4c37af04ebc58858d1cdc4d66b00d39362be1b0a 100644 --- a/test/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig +++ b/test/cases/compile_errors/unused_variable_error_on_errdefer.zig @@ -7,7 +7,7 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:15: error: unused variable: 'a' +// :2:15: error: unused capture diff --git a/test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig b/test/cases/compile_errors/vector_index_out_of_bounds.zig similarity index 64% rename from test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig rename to test/cases/compile_errors/vector_index_out_of_bounds.zig index fdffd8b45567777d9dc61263f24a724d019a9431..ed1a25a32150006332f3b8c165b8ebd65b77f866 100644 --- a/test/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig +++ b/test/cases/compile_errors/vector_index_out_of_bounds.zig @@ -4,7 +4,7 @@ export fn entry() void { } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:62: error: index 3 outside vector of size 3 +// :2:49: error: expected 3 vector elements; found 4 diff --git a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig b/test/cases/compile_errors/while_expected_error_union_got_bool.zig similarity index 62% rename from test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig rename to test/cases/compile_errors/while_expected_error_union_got_bool.zig index b8a72e97937e454aa404e0c744fef8549d33d4ce..f7960437ec55619ecc0fd137fa99f5c6f109d08f 100644 --- a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig +++ b/test/cases/compile_errors/while_expected_error_union_got_bool.zig @@ -4,7 +4,7 @@ export fn foo() void { fn bar() bool { return true; } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:15: error: expected error union type, found 'bool' +// :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/while_expected_error_union_got_optional.zig similarity index 62% rename from test/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig rename to test/cases/compile_errors/while_expected_error_union_got_optional.zig index c933dc95092aa2bc4637f9dd9498cfe8771496a1..5cabd76fce3fdca65a4f32eab0f45f8a0d91e9f4 100644 --- a/test/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig +++ b/test/cases/compile_errors/while_expected_error_union_got_optional.zig @@ -4,7 +4,7 @@ export fn foo() void { fn bar() ?i32 { return 1; } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:15: error: expected error union type, found '?i32' +// :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/while_expected_optional_got_bool.zig similarity index 59% rename from test/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig rename to test/cases/compile_errors/while_expected_optional_got_bool.zig index 0458d1ba0179951341bfbbf6e2d78df4ab315a02..22b8c1e58c732c4ad7125772fa47562655e80adc 100644 --- a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig +++ b/test/cases/compile_errors/while_expected_optional_got_bool.zig @@ -4,7 +4,7 @@ export fn foo() void { fn bar() bool { return true; } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:15: error: expected optional type, found 'bool' +// :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/while_expected_optional_got_error_union.zig similarity index 58% rename from test/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig rename to test/cases/compile_errors/while_expected_optional_got_error_union.zig index 7cdbd2cccf69ad800659f21f13eabaa6177f6757..38a8a0dd20387ea7b1ec00426ed1073b970db83c 100644 --- a/test/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig +++ b/test/cases/compile_errors/while_expected_optional_got_error_union.zig @@ -4,7 +4,7 @@ export fn foo() void { fn bar() anyerror!i32 { return 1; } // error -// backend=stage1 +// backend=stage2 // target=native // -// tmp.zig:2:15: error: expected optional type, found 'anyerror!i32' +// :2:15: error: expected optional type, found 'anyerror!i32' -- 2.54.0