authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-12 23:35:11+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-13 01:13:17+03:00
log373488157751dbaa22415ce842c4f5616fc845ff
treefaa6c045484c41b141ed7dce97c6f4a0346a01e8
parent67d684d89aeb0bb3cfa86c57e8d77359d45743fa
signature Commit is signed but in an unrecognized format.

add error for unused/duplicate block labels


7 files changed, 97 insertions(+), 11 deletions(-)

lib/std/dwarf.zig+1-1
......@@ -322,7 +322,7 @@ fn parseFormValue(allocator: *mem.Allocator, in_stream: anytype, form_id: u64, e
322322 FORM_block1 => parseFormValueBlock(allocator, in_stream, endian, 1),
323323 FORM_block2 => parseFormValueBlock(allocator, in_stream, endian, 2),
324324 FORM_block4 => parseFormValueBlock(allocator, in_stream, endian, 4),
325 FORM_block => x: {
325 FORM_block => {
326326 const block_len = try nosuspend leb.readULEB128(usize, in_stream);
327327 return parseFormValueBlockLen(allocator, in_stream, block_len);
328328 },
lib/std/special/c.zig+3-3
......@@ -536,7 +536,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
536536 // normalize x and y
537537 if (ex == 0) {
538538 i = ux << exp_bits;
539 while (i >> bits_minus_1 == 0) : (b: {
539 while (i >> bits_minus_1 == 0) : ({
540540 ex -= 1;
541541 i <<= 1;
542542 }) {}
......@@ -547,7 +547,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
547547 }
548548 if (ey == 0) {
549549 i = uy << exp_bits;
550 while (i >> bits_minus_1 == 0) : (b: {
550 while (i >> bits_minus_1 == 0) : ({
551551 ey -= 1;
552552 i <<= 1;
553553 }) {}
......@@ -573,7 +573,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
573573 return 0 * x;
574574 ux = i;
575575 }
576 while (ux >> digits == 0) : (b: {
576 while (ux >> digits == 0) : ({
577577 ux <<= 1;
578578 ex -= 1;
579579 }) {}
lib/std/zig/render.zig+1-1
......@@ -2385,7 +2385,7 @@ fn renderTokenOffset(
23852385 }
23862386 }
23872387
2388 if (next_token_id != .LineComment) blk: {
2388 if (next_token_id != .LineComment) {
23892389 switch (space) {
23902390 Space.None, Space.NoNewline => return,
23912391 Space.Newline => {
src-self-hosted/translate_c.zig+6-6
......@@ -931,7 +931,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
931931 else => |e| return e,
932932 };
933933
934 const align_expr = blk: {
934 const align_expr = blk_2: {
935935 const alignment = ZigClangFieldDecl_getAlignedAttribute(field_decl, rp.c.clang_context);
936936 if (alignment != 0) {
937937 _ = try appendToken(rp.c, .Keyword_align, "align");
......@@ -940,9 +940,9 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
940940 const expr = try transCreateNodeInt(rp.c, alignment / 8);
941941 _ = try appendToken(rp.c, .RParen, ")");
942942
943 break :blk expr;
943 break :blk_2 expr;
944944 }
945 break :blk null;
945 break :blk_2 null;
946946 };
947947
948948 const field_node = try c.arena.create(ast.Node.ContainerField);
......@@ -1073,9 +1073,9 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
10731073
10741074 const field_name_tok = try appendIdentifier(c, field_name);
10751075
1076 const int_node = if (!pure_enum) blk: {
1076 const int_node = if (!pure_enum) blk_2: {
10771077 _ = try appendToken(c, .Colon, "=");
1078 break :blk try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const));
1078 break :blk_2 try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const));
10791079 } else
10801080 null;
10811081
......@@ -2388,7 +2388,7 @@ fn transZeroInitExpr(
23882388 ty: *const ZigClangType,
23892389) TransError!*ast.Node {
23902390 switch (ZigClangType_getTypeClass(ty)) {
2391 .Builtin => blk: {
2391 .Builtin => {
23922392 const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty);
23932393 switch (ZigClangBuiltinType_getKind(builtin_ty)) {
23942394 .Bool => return try transCreateNodeBoolLiteral(rp.c, false),
src/all_types.hpp+3
......@@ -2438,6 +2438,7 @@ struct ScopeBlock {
24382438 LVal lval;
24392439 bool safety_off;
24402440 bool fast_math_on;
2441 bool name_used;
24412442};
24422443
24432444// This scope is created from every defer expression.
......@@ -2488,6 +2489,8 @@ struct ScopeLoop {
24882489 ZigList<IrBasicBlockSrc *> *incoming_blocks;
24892490 ResultLocPeerParent *peer_parent;
24902491 ScopeExpr *spill_scope;
2492
2493 bool name_used;
24912494};
24922495
24932496// This scope blocks certain things from working such as comptime continue
src/ir.cpp+60
......@@ -5476,6 +5476,25 @@ static ResultLocPeer *create_peer_result(ResultLocPeerParent *peer_parent) {
54765476 return result;
54775477}
54785478
5479static bool is_duplicate_label(CodeGen *g, Scope *scope, AstNode *node, Buf *name) {
5480 if (name == nullptr) return false;
5481
5482 for (;;) {
5483 if (scope == nullptr || scope->id == ScopeIdFnDef) {
5484 break;
5485 } else if (scope->id == ScopeIdBlock || scope->id == ScopeIdLoop) {
5486 Buf *this_block_name = scope->id == ScopeIdBlock ? ((ScopeBlock *)scope)->name : ((ScopeLoop *)scope)->name;
5487 if (this_block_name != nullptr && buf_eql_buf(name, this_block_name)) {
5488 ErrorMsg *msg = add_node_error(g, node, buf_sprintf("redeclaration of label '%s'", buf_ptr(name)));
5489 add_error_note(g, msg, scope->source_node, buf_sprintf("previous declaration is here"));
5490 return true;
5491 }
5492 }
5493 scope = scope->parent;
5494 }
5495 return false;
5496}
5497
54795498static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *block_node, LVal lval,
54805499 ResultLoc *result_loc)
54815500{
......@@ -5484,6 +5503,9 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
54845503 ZigList<IrInstSrc *> incoming_values = {0};
54855504 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
54865505
5506 if (is_duplicate_label(irb->codegen, parent_scope, block_node, block_node->data.block.name))
5507 return irb->codegen->invalid_inst_src;
5508
54875509 ScopeBlock *scope_block = create_block_scope(irb->codegen, block_node, parent_scope);
54885510
54895511 Scope *outer_block_scope = &scope_block->base;
......@@ -5495,6 +5517,9 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
54955517 }
54965518
54975519 if (block_node->data.block.statements.length == 0) {
5520 if (scope_block->name != nullptr) {
5521 add_node_error(irb->codegen, block_node, buf_sprintf("unused block label"));
5522 }
54985523 // {}
54995524 return ir_lval_wrap(irb, parent_scope, ir_build_const_void(irb, child_scope, block_node), lval, result_loc);
55005525 }
......@@ -5552,6 +5577,10 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
55525577 }
55535578 }
55545579
5580 if (scope_block->name != nullptr && scope_block->name_used == false) {
5581 add_node_error(irb->codegen, block_node, buf_sprintf("unused block label"));
5582 }
5583
55555584 if (found_invalid_inst)
55565585 return irb->codegen->invalid_inst_src;
55575586
......@@ -8152,6 +8181,9 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
81528181 ZigList<IrInstSrc *> incoming_values = {0};
81538182 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
81548183
8184 if (is_duplicate_label(irb->codegen, payload_scope, node, node->data.while_expr.name))
8185 return irb->codegen->invalid_inst_src;
8186
81558187 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, payload_scope);
81568188 loop_scope->break_block = end_block;
81578189 loop_scope->continue_block = continue_block;
......@@ -8169,6 +8201,10 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
81698201 if (body_result == irb->codegen->invalid_inst_src)
81708202 return body_result;
81718203
8204 if (loop_scope->name != nullptr && loop_scope->name_used == false) {
8205 add_node_error(irb->codegen, node, buf_sprintf("unused while label"));
8206 }
8207
81728208 if (!instr_is_unreachable(body_result)) {
81738209 ir_mark_gen(ir_build_check_statement_is_void(irb, payload_scope, node->data.while_expr.body, body_result));
81748210 ir_mark_gen(ir_build_br(irb, payload_scope, node, continue_block, is_comptime));
......@@ -8263,6 +8299,9 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
82638299 ZigList<IrInstSrc *> incoming_values = {0};
82648300 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
82658301
8302 if (is_duplicate_label(irb->codegen, child_scope, node, node->data.while_expr.name))
8303 return irb->codegen->invalid_inst_src;
8304
82668305 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);
82678306 loop_scope->break_block = end_block;
82688307 loop_scope->continue_block = continue_block;
......@@ -8280,6 +8319,10 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
82808319 if (body_result == irb->codegen->invalid_inst_src)
82818320 return body_result;
82828321
8322 if (loop_scope->name != nullptr && loop_scope->name_used == false) {
8323 add_node_error(irb->codegen, node, buf_sprintf("unused while label"));
8324 }
8325
82838326 if (!instr_is_unreachable(body_result)) {
82848327 ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.while_expr.body, body_result));
82858328 ir_mark_gen(ir_build_br(irb, child_scope, node, continue_block, is_comptime));
......@@ -8353,6 +8396,9 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
83538396
83548397 Scope *subexpr_scope = create_runtime_scope(irb->codegen, node, scope, is_comptime);
83558398
8399 if (is_duplicate_label(irb->codegen, subexpr_scope, node, node->data.while_expr.name))
8400 return irb->codegen->invalid_inst_src;
8401
83568402 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, subexpr_scope);
83578403 loop_scope->break_block = end_block;
83588404 loop_scope->continue_block = continue_block;
......@@ -8369,6 +8415,10 @@ static IrInstSrc *ir_gen_while_expr(IrBuilderSrc *irb, Scope *scope, AstNode *no
83698415 if (body_result == irb->codegen->invalid_inst_src)
83708416 return body_result;
83718417
8418 if (loop_scope->name != nullptr && loop_scope->name_used == false) {
8419 add_node_error(irb->codegen, node, buf_sprintf("unused while label"));
8420 }
8421
83728422 if (!instr_is_unreachable(body_result)) {
83738423 ir_mark_gen(ir_build_check_statement_is_void(irb, scope, node->data.while_expr.body, body_result));
83748424 ir_mark_gen(ir_build_br(irb, scope, node, continue_block, is_comptime));
......@@ -8501,6 +8551,9 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
85018551 elem_ptr : ir_build_load_ptr(irb, &spill_scope->base, elem_node, elem_ptr);
85028552 build_decl_var_and_init(irb, parent_scope, elem_node, elem_var, elem_value, buf_ptr(elem_var_name), is_comptime);
85038553
8554 if (is_duplicate_label(irb->codegen, child_scope, node, node->data.for_expr.name))
8555 return irb->codegen->invalid_inst_src;
8556
85048557 ZigList<IrInstSrc *> incoming_values = {0};
85058558 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
85068559 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);
......@@ -8520,6 +8573,10 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
85208573 if (body_result == irb->codegen->invalid_inst_src)
85218574 return irb->codegen->invalid_inst_src;
85228575
8576 if (loop_scope->name != nullptr && loop_scope->name_used == false) {
8577 add_node_error(irb->codegen, node, buf_sprintf("unused for label"));
8578 }
8579
85238580 if (!instr_is_unreachable(body_result)) {
85248581 ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.for_expr.body, body_result));
85258582 ir_mark_gen(ir_build_br(irb, child_scope, node, continue_block, is_comptime));
......@@ -9464,6 +9521,7 @@ static IrInstSrc *ir_gen_break(IrBuilderSrc *irb, Scope *break_scope, AstNode *n
94649521 if (node->data.break_expr.name == nullptr ||
94659522 (this_loop_scope->name != nullptr && buf_eql_buf(node->data.break_expr.name, this_loop_scope->name)))
94669523 {
9524 this_loop_scope->name_used = true;
94679525 loop_scope = this_loop_scope;
94689526 break;
94699527 }
......@@ -9473,6 +9531,7 @@ static IrInstSrc *ir_gen_break(IrBuilderSrc *irb, Scope *break_scope, AstNode *n
94739531 (this_block_scope->name != nullptr && buf_eql_buf(node->data.break_expr.name, this_block_scope->name)))
94749532 {
94759533 assert(this_block_scope->end_block != nullptr);
9534 this_block_scope->name_used = true;
94769535 return ir_gen_return_from_block(irb, break_scope, node, this_block_scope);
94779536 }
94789537 } else if (search_scope->id == ScopeIdSuspend) {
......@@ -9540,6 +9599,7 @@ static IrInstSrc *ir_gen_continue(IrBuilderSrc *irb, Scope *continue_scope, AstN
95409599 if (node->data.continue_expr.name == nullptr ||
95419600 (this_loop_scope->name != nullptr && buf_eql_buf(node->data.continue_expr.name, this_loop_scope->name)))
95429601 {
9602 this_loop_scope->name_used = true;
95439603 loop_scope = this_loop_scope;
95449604 break;
95459605 }
test/compile_errors.zig+23
......@@ -2,6 +2,29 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("duplicate/unused labels",
6 \\comptime {
7 \\ blk: { blk: while (false) {} }
8 \\ blk: while (false) { blk: for (@as([0]void, undefined)) |_| {} }
9 \\ blk: for (@as([0]void, undefined)) |_| { blk: {} }
10 \\}
11 \\comptime {
12 \\ blk: {}
13 \\ blk: while(false) {}
14 \\ blk: for(@as([0]void, undefined)) |_| {}
15 \\}
16 , &[_][]const u8{
17 "tmp.zig:2:17: error: redeclaration of label 'blk'",
18 "tmp.zig:2:10: note: previous declaration is here",
19 "tmp.zig:3:31: error: redeclaration of label 'blk'",
20 "tmp.zig:3:10: note: previous declaration is here",
21 "tmp.zig:4:51: error: redeclaration of label 'blk'",
22 "tmp.zig:4:10: note: previous declaration is here",
23 "tmp.zig:7:10: error: unused block label",
24 "tmp.zig:8:10: error: unused while label",
25 "tmp.zig:9:10: error: unused for label",
26 });
27
528 cases.addTest("@alignCast of zero sized types",
629 \\export fn foo() void {
730 \\ const a: *void = undefined;