From b18af37c578b118a245c911508d73ed23c303ee0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 7 Sep 2018 15:17:24 -0400 Subject: [PATCH] fix crash when var init has compile error and then the var is referenced closes #1483 --- src/ir.cpp | 3 +-- test/compile_errors.zig | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 0269c29b1a..30f40b3a1e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13107,8 +13107,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, assert(ira->codegen->errors.length != 0); return ira->codegen->invalid_instruction; } - assert(var->value->type); - if (type_is_invalid(var->value->type)) + if (var->value->type == nullptr || type_is_invalid(var->value->type)) return ira->codegen->invalid_instruction; bool comptime_var_mem = ir_get_var_is_comptime(var); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index faf17d2c05..185ca62973 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,24 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "variable initialization compile error then referenced", + \\fn Undeclared() type { + \\ return T; + \\} + \\fn Gen() type { + \\ const X = Undeclared(); + \\ return struct { + \\ x: X, + \\ }; + \\} + \\export fn entry() void { + \\ const S = Gen(); + \\} + , + ".tmp_source.zig:2:12: error: use of undeclared identifier 'T'", + ); + cases.add( "refer to the type of a generic function", \\export fn entry() void {