authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-07 23:31:38+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-08 03:09:41+00:00
log5667a21b1e7b8aa2dfcefed81d2b594029a116db
tree07ce6306793ab5486b92d873987030c7f4601c32
parent597a36367305cdb63ffe25af707d708ef9376a7a

fix missing check on extern variables with no type


2 files changed, 13 insertions(+), 0 deletions(-)

src/analyze.cpp+4
...@@ -4012,6 +4012,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -4012,6 +4012,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
4012 } else if (!is_extern) {4012 } else if (!is_extern) {
4013 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));4013 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));
4014 implicit_type = g->builtin_types.entry_invalid;4014 implicit_type = g->builtin_types.entry_invalid;
4015 } else if (explicit_type == nullptr) {
4016 // extern variable without explicit type
4017 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
4018 implicit_type = g->builtin_types.entry_invalid;
4015 }4019 }
40164020
4017 ZigType *type = explicit_type ? explicit_type : implicit_type;4021 ZigType *type = explicit_type ? explicit_type : implicit_type;
test/compile_errors.zig+9
...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");...@@ -2,6 +2,15 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("extern variable has no type",
6 \\extern var foo;
7 \\pub export fn entry() void {
8 \\ foo;
9 \\}
10 , &[_][]const u8{
11 "tmp.zig:1:1: error: unable to infer variable type",
12 });
13
5 cases.add("@src outside function",14 cases.add("@src outside function",
6 \\comptime {15 \\comptime {
7 \\ @src();16 \\ @src();