| author | |
| committer | |
| log | 490cafe2c5a62585fd80245a0f6fa5cff4f52dba |
| tree | cfeaee15bc2cb5b37e9c9f83d1068c28f8c5e280 |
| parent | f4bb8be9fc8766fec93618f89552c28d3f0a201b |
Closes #68023 files changed, 22 insertions(+), 2 deletions(-)
doc/langref.html.in+1-1| ... | @@ -8675,7 +8675,7 @@ test "safety check" { | ... | @@ -8675,7 +8675,7 @@ test "safety check" { |
| 8675 | {#code_end#} | 8675 | {#code_end#} |
| 8676 | {#header_open|Reaching Unreachable Code#} | 8676 | {#header_open|Reaching Unreachable Code#} |
| 8677 | <p>At compile-time:</p> | 8677 | <p>At compile-time:</p> |
| 8678 | {#code_begin|test_err|unable to evaluate constant expression#} | 8678 | {#code_begin|test_err|reached unreachable code#} |
| 8679 | comptime { | 8679 | comptime { |
| 8680 | assert(false); | 8680 | assert(false); |
| 8681 | } | 8681 | } |
src/stage1/ir.cpp+5| ... | @@ -21592,6 +21592,11 @@ static IrInstGen *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstSrcCondBr | ... | @@ -21592,6 +21592,11 @@ static IrInstGen *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstSrcCondBr |
| 21592 | static IrInstGen *ir_analyze_instruction_unreachable(IrAnalyze *ira, | 21592 | static IrInstGen *ir_analyze_instruction_unreachable(IrAnalyze *ira, |
| 21593 | IrInstSrcUnreachable *unreachable_instruction) | 21593 | IrInstSrcUnreachable *unreachable_instruction) |
| 21594 | { | 21594 | { |
| 21595 | if (ir_should_inline(ira->old_irb.exec, unreachable_instruction->base.base.scope)) { | ||
| 21596 | ir_add_error(ira, &unreachable_instruction->base.base, buf_sprintf("reached unreachable code")); | ||
| 21597 | return ir_unreach_error(ira); | ||
| 21598 | } | ||
| 21599 | |||
| 21595 | IrInstGen *result = ir_build_unreachable_gen(ira, &unreachable_instruction->base.base); | 21600 | IrInstGen *result = ir_build_unreachable_gen(ira, &unreachable_instruction->base.base); |
| 21596 | return ir_finish_anal(ira, result); | 21601 | return ir_finish_anal(ira, result); |
| 21597 | } | 21602 | } |
test/compile_errors.zig+16-1| ... | @@ -2,6 +2,21 @@ const tests = @import("tests.zig"); | ... | @@ -2,6 +2,21 @@ const tests = @import("tests.zig"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 5 | cases.add("unreachable executed at comptime", | ||
| 6 | \\fn foo(comptime x: i32) i32 { | ||
| 7 | \\ comptime { | ||
| 8 | \\ if (x >= 0) return -x; | ||
| 9 | \\ unreachable; | ||
| 10 | \\ } | ||
| 11 | \\} | ||
| 12 | \\export fn entry() void { | ||
| 13 | \\ _ = foo(-42); | ||
| 14 | \\} | ||
| 15 | , &[_][]const u8{ | ||
| 16 | "tmp.zig:4:9: error: reached unreachable code", | ||
| 17 | "tmp.zig:8:12: note: called from here", | ||
| 18 | }); | ||
| 19 | |||
| 5 | cases.add("indexing a undefined slice at comptime", | 20 | cases.add("indexing a undefined slice at comptime", |
| 6 | \\comptime { | 21 | \\comptime { |
| 7 | \\ var slice: []u8 = undefined; | 22 | \\ var slice: []u8 = undefined; |
| ... | @@ -6208,7 +6223,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { | ... | @@ -6208,7 +6223,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 6208 | \\ if (!ok) unreachable; | 6223 | \\ if (!ok) unreachable; |
| 6209 | \\} | 6224 | \\} |
| 6210 | , &[_][]const u8{ | 6225 | , &[_][]const u8{ |
| 6211 | "tmp.zig:10:14: error: unable to evaluate constant expression", | 6226 | "tmp.zig:10:14: error: reached unreachable code", |
| 6212 | "tmp.zig:6:20: note: referenced here", | 6227 | "tmp.zig:6:20: note: referenced here", |
| 6213 | }); | 6228 | }); |
| 6214 | 6229 |