From 7b131a7cd496da221434939c1f624629d02fd3d0 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 16 Nov 2022 19:49:58 +0200 Subject: Module: fix `fieldSrcLoc` for generated types --- src/Module.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index af29a591cc..82dd802f53 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1044,7 +1044,8 @@ pub const Struct = struct { .root => return queryFieldSrc(tree.*, query, file, tree.containerDeclRoot()), - else => unreachable, + // This struct was generated using @Type + else => return s.srcLoc(mod), } } @@ -1270,7 +1271,8 @@ pub const Union = struct { .tagged_union_enum_tag, .tagged_union_enum_tag_trailing, => return queryFieldSrc(tree.*, query, file, tree.taggedUnionEnumTag(node)), - else => unreachable, + // This union was generated using @Type + else => return u.srcLoc(mod), } } -- cgit v1.2.3 From 034507ef7cc492e105ca90c42686aa52bc2097e3 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 17 Nov 2022 23:28:19 +0200 Subject: Module: fix compile error for non-comptime-known global initializer --- src/Module.zig | 2 +- .../global_variable_stored_in_global_const.zig | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/cases/compile_errors/global_variable_stored_in_global_const.zig (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 82dd802f53..d598993c3f 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4633,7 +4633,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool { const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 }; const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 }; const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 }; - const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, undefined); + const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, "global variable initializer must be comptime-known"); // Note this resolves the type of the Decl, not the value; if this Decl // is a struct, for example, this resolves `type` (which needs no resolution), diff --git a/test/cases/compile_errors/global_variable_stored_in_global_const.zig b/test/cases/compile_errors/global_variable_stored_in_global_const.zig new file mode 100644 index 0000000000..594498f1c1 --- /dev/null +++ b/test/cases/compile_errors/global_variable_stored_in_global_const.zig @@ -0,0 +1,12 @@ +var a: u32 = 2; +const b = a; +pub export fn entry() void { + _ = b; +} + +// error +// backend=stage2 +// target=native +// +// :2:11: error: unable to resolve comptime value +// :2:11: note: global variable initializer must be comptime-known -- cgit v1.2.3