zig

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

commit c84548e71d4990352c202da92d47f1398385411f (tree)
parent 3c1f9baff10be20910fc6e168c293759d9b8537e
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sat, 22 Sep 2018 10:46:22 -0400

fix @compileLog having unintended side effects

closes #1459

Diffstat:
Msrc/ir.cpp | 5++++-
Mtest/compile_errors.zig | 17++++++++++++++---
2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -9874,6 +9874,7 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) { if (!const_val) return ira->codegen->builtin_types.entry_invalid; + assert(const_val->data.x_type != nullptr); return const_val->data.x_type; } @@ -16880,7 +16881,9 @@ static ZigType *ir_analyze_instruction_compile_log(IrAnalyze *ira, IrInstruction } fprintf(stderr, "\n"); - ir_add_error(ira, &instruction->base, buf_sprintf("found compile log statement")); + // Here we bypass higher level functions such as ir_add_error because we do not want + // invalidate_exec to be called. + add_node_error(ira->codegen, instruction->base.source_node, buf_sprintf("found compile log statement")); ir_build_const_from(ira, &instruction->base); return ira->codegen->builtin_types.entry_void; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -2,6 +2,20 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( + "compile log statement inside function which must be comptime evaluated", + \\fn Foo(comptime T: type) type { + \\ @compileLog(@typeName(T)); + \\ return T; + \\} + \\export fn entry() void { + \\ _ = Foo(i32); + \\ _ = @typeName(Foo(i32)); + \\} + , + ".tmp_source.zig:2:5: error: found compile log statement", + ); + + cases.add( "comptime slice of an undefined slice", \\comptime { \\ var a: []u8 = undefined; @@ -3472,11 +3486,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , ".tmp_source.zig:5:5: error: found compile log statement", - ".tmp_source.zig:2:17: note: called from here", ".tmp_source.zig:6:5: error: found compile log statement", - ".tmp_source.zig:2:17: note: called from here", ".tmp_source.zig:7:5: error: found compile log statement", - ".tmp_source.zig:2:17: note: called from here", ); cases.add(