authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-28 17:11:03+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-29 14:55:43+03:00
log5321afcf9c633c5dc0da5148981dfdbd61606f1a
tree267b0231ddfa4cb31d0c43328c86387865593d43
parentd7314555f2bc413494d58bbafa2d607b88922afb

stage2: make switch on corrupt value panic point to switch condition

Closes #13295

2 files changed, 14 insertions(+), 3 deletions(-)

src/AstGen.zig+5
......@@ -6496,9 +6496,14 @@ fn switchExpr(
64966496 }
64976497
64986498 const operand_ri: ResultInfo = .{ .rl = if (any_payload_is_ref) .ref else .none };
6499 astgen.advanceSourceCursorToNode(operand_node);
6500 const operand_line = astgen.source_line - parent_gz.decl_line;
6501 const operand_column = astgen.source_column;
64996502 const raw_operand = try expr(parent_gz, scope, operand_ri, operand_node);
65006503 const cond_tag: Zir.Inst.Tag = if (any_payload_is_ref) .switch_cond_ref else .switch_cond;
65016504 const cond = try parent_gz.addUnNode(cond_tag, raw_operand, operand_node);
6505 // Sema expects a dbg_stmt immediately after switch_cond(_ref)
6506 try emitDbgStmt(parent_gz, operand_line, operand_column);
65026507 // We need the type of the operand to use as the result location for all the prong items.
65036508 const cond_ty_inst = try parent_gz.addUnNode(.typeof, cond, operand_node);
65046509 const item_ri: ResultInfo = .{ .rl = .{ .ty = cond_ty_inst } };
src/Sema.zig+9-3
......@@ -9663,6 +9663,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
96639663 const extra = sema.code.extraData(Zir.Inst.SwitchBlock, inst_data.payload_index);
96649664
96659665 const operand = try sema.resolveInst(extra.data.operand);
9666 // AstGen guarantees that the instruction immediately following
9667 // switch_cond(_ref) is a dbg_stmt
9668 const cond_dbg_node_index = Zir.refToIndex(extra.data.operand).? + 1;
96669669
96679670 var header_extra_index: usize = extra.end;
96689671
......@@ -10359,6 +10362,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1035910362 if (backend_supports_is_named_enum and block.wantSafety() and operand_ty.zigTypeTag() == .Enum and
1036010363 (!operand_ty.isNonexhaustiveEnum() or union_originally))
1036110364 {
10365 try sema.zirDbgStmt(block, cond_dbg_node_index);
1036210366 const ok = try block.addUnOp(.is_named_enum_value, operand);
1036310367 try sema.addSafetyCheck(block, ok, .corrupt_switch);
1036410368 }
......@@ -10828,6 +10832,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1082810832 if (backend_supports_is_named_enum and special.body.len != 0 and block.wantSafety() and
1082910833 operand_ty.zigTypeTag() == .Enum and (!operand_ty.isNonexhaustiveEnum() or union_originally))
1083010834 {
10835 try sema.zirDbgStmt(&case_block, cond_dbg_node_index);
1083110836 const ok = try case_block.addUnOp(.is_named_enum_value, operand);
1083210837 try sema.addSafetyCheck(&case_block, ok, .corrupt_switch);
1083310838 }
......@@ -10851,6 +10856,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1085110856 // We still need a terminator in this block, but we have proven
1085210857 // that it is unreachable.
1085310858 if (case_block.wantSafety()) {
10859 try sema.zirDbgStmt(&case_block, cond_dbg_node_index);
1085410860 _ = try sema.safetyPanic(&case_block, src, .corrupt_switch);
1085510861 } else {
1085610862 _ = try case_block.addNoOp(.unreach);
......@@ -23931,7 +23937,7 @@ fn coerceExtra(
2393123937 inst_ty.isPtrAtRuntime())
2393223938 anyopaque_check: {
2393323939 if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :optional;
23934 const elem_ty = inst_ty.elemType2();
23940 const elem_ty = inst_ty.elemType2();
2393523941 if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) {
2393623942 in_memory_result = .{ .double_ptr_to_anyopaque = .{
2393723943 .actual = inst_ty,
......@@ -23941,7 +23947,7 @@ fn coerceExtra(
2394123947 }
2394223948 // Let the logic below handle wrapping the optional now that
2394323949 // it has been checked to correctly coerce.
23944 if (!inst_ty.isPtrLikeOptional()) break: anyopaque_check;
23950 if (!inst_ty.isPtrLikeOptional()) break :anyopaque_check;
2394523951 return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
2394623952 }
2394723953
......@@ -24066,7 +24072,7 @@ fn coerceExtra(
2406624072 // but don't do it if the source type is a double pointer
2406724073 if (dest_info.pointee_type.tag() == .anyopaque and inst_ty.zigTypeTag() == .Pointer) {
2406824074 if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :pointer;
24069 const elem_ty = inst_ty.elemType2();
24075 const elem_ty = inst_ty.elemType2();
2407024076 if (elem_ty.zigTypeTag() == .Pointer or elem_ty.isPtrLikeOptional()) {
2407124077 in_memory_result = .{ .double_ptr_to_anyopaque = .{
2407224078 .actual = inst_ty,