zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b79929b2eaa634b756fe374372d59718f4f8479a (tree)
parent d1d24b426dd8f12e6d643f45fcb6bb11dddaa8ef
Author: Veikka Tuominen <git@vexu.eu>
Date:   Tue,  2 Aug 2022 21:01:20 +0300

AstGen: better source location for if/while condition unwrapping

Diffstat:
Msrc/AstGen.zig | 21+++++++++++----------
Atest/cases/compile_errors/packed_union_given_enum_tag_type.zig | 20++++++++++++++++++++
Atest/cases/compile_errors/packed_union_with_automatic_layout_field.zig | 20++++++++++++++++++++
Atest/cases/compile_errors/specify_non-integer_enum_tag_type.zig | 16++++++++++++++++
Dtest/cases/compile_errors/stage1/obj/packed_union_given_enum_tag_type.zig | 20--------------------
Dtest/cases/compile_errors/stage1/obj/packed_union_with_automatic_layout_field.zig | 18------------------
Dtest/cases/compile_errors/stage1/obj/specify_non-integer_enum_tag_type.zig | 16----------------
Dtest/cases/compile_errors/stage1/obj/unused_variable_error_on_errdefer.zig | 13-------------
Dtest/cases/compile_errors/stage1/obj/vector_index_out_of_bounds.zig | 10----------
Dtest/cases/compile_errors/stage1/obj/while_expected_error_union_got_bool.zig | 10----------
Dtest/cases/compile_errors/stage1/obj/while_expected_error_union_got_optional.zig | 10----------
Dtest/cases/compile_errors/stage1/obj/while_expected_optional_got_bool.zig | 10----------
Dtest/cases/compile_errors/stage1/obj/while_expected_optional_got_error_union.zig | 10----------
Atest/cases/compile_errors/unused_variable_error_on_errdefer.zig | 13+++++++++++++
Atest/cases/compile_errors/vector_index_out_of_bounds.zig | 10++++++++++
Atest/cases/compile_errors/while_expected_error_union_got_bool.zig | 10++++++++++
Atest/cases/compile_errors/while_expected_error_union_got_optional.zig | 10++++++++++
Atest/cases/compile_errors/while_expected_optional_got_bool.zig | 10++++++++++
Atest/cases/compile_errors/while_expected_optional_got_error_union.zig | 10++++++++++
19 files changed, 130 insertions(+), 127 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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'