authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-06-15 18:06:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-21 17:03:03-07:00
logd34a1ccb0ea75ba31f374b8b2d34e18326b147b1
tree7bb8d8b843840ff6ce7bb3311139d538340126e6
parent18c1007a34e92c69fa87d3f2628fa076fae7941c

stage2: fix TODO in @export to look for runtime-vars

Also rename LocalPtr.is_comptime to LocalPtr.maybe_comptime as it is a better name, as it could be runtime, but is not always runtime.

1 files changed, 33 insertions(+), 9 deletions(-)

src/AstGen.zig+33-9
......@@ -2405,7 +2405,7 @@ fn varDecl(
24052405 .name = ident_name,
24062406 .ptr = init_scope.rl_ptr,
24072407 .token_src = name_token,
2408 .is_comptime = true,
2408 .maybe_comptime = true,
24092409 };
24102410 return &sub_scope.base;
24112411 },
......@@ -2461,7 +2461,7 @@ fn varDecl(
24612461 .name = ident_name,
24622462 .ptr = var_data.alloc,
24632463 .token_src = name_token,
2464 .is_comptime = is_comptime,
2464 .maybe_comptime = is_comptime,
24652465 };
24662466 return &sub_scope.base;
24672467 },
......@@ -5405,7 +5405,7 @@ fn forExpr(
54055405 .name = index_name,
54065406 .ptr = index_ptr,
54075407 .token_src = index_token,
5408 .is_comptime = is_inline,
5408 .maybe_comptime = is_inline,
54095409 };
54105410 break :blk &index_scope.base;
54115411 };
......@@ -6188,7 +6188,7 @@ fn identifier(
61886188 if (local_ptr.name == name_str_index) {
61896189 local_ptr.used = true;
61906190 if (hit_namespace) {
6191 if (local_ptr.is_comptime)
6191 if (local_ptr.maybe_comptime)
61926192 break
61936193 else
61946194 return astgen.failNodeNotes(ident, "'{s}' not accessible from inner function", .{ident_name}, &.{
......@@ -6836,9 +6836,32 @@ fn builtinCall(
68366836 .identifier => {
68376837 const ident_token = main_tokens[params[0]];
68386838 decl_name = try astgen.identAsString(ident_token);
6839 // TODO look for local variables in scope matching `decl_name` and emit a compile
6840 // error. Only top-level declarations can be exported. Until this is done, the
6841 // compile error will end up being "use of undeclared identifier" in Sema.
6839 {
6840 var s = scope;
6841 while (true) switch (s.tag) {
6842 .local_val => {
6843 const local_val = s.cast(Scope.LocalVal).?;
6844 if (local_val.name == decl_name) {
6845 local_val.used = true;
6846 break;
6847 }
6848 s = local_val.parent;
6849 },
6850 .local_ptr => {
6851 const local_ptr = s.cast(Scope.LocalPtr).?;
6852 if (local_ptr.name == decl_name) {
6853 if (!local_ptr.maybe_comptime)
6854 return astgen.failNode(params[0], "unable to export runtime-known value", .{});
6855 local_ptr.used = true;
6856 break;
6857 }
6858 s = local_ptr.parent;
6859 },
6860 .gen_zir => s = s.cast(GenZir).?.parent,
6861 .defer_normal, .defer_error => s = s.cast(Scope.Defer).?.parent,
6862 .namespace, .top => break,
6863 };
6864 }
68426865 },
68436866 .field_access => {
68446867 const namespace_node = node_datas[params[0]].lhs;
......@@ -6848,7 +6871,7 @@ fn builtinCall(
68486871 decl_name = try astgen.identAsString(field_ident);
68496872 },
68506873 else => return astgen.failNode(
6851 params[0], "the first @export parameter must be an identifier", .{},
6874 params[0], "symbol to export must identify a declaration", .{},
68526875 ),
68536876 }
68546877 const options = try comptimeExpr(gz, scope, .{ .ty = .export_options_type }, params[1]);
......@@ -8431,7 +8454,8 @@ const Scope = struct {
84318454 token_src: ast.TokenIndex,
84328455 /// String table index.
84338456 name: u32,
8434 is_comptime: bool,
8457 /// true means we find out during Sema whether the value is comptime. false means it is already known at AstGen the value is runtime-known.
8458 maybe_comptime: bool,
84358459 /// has this variable been referenced?
84368460 used: bool = false,
84378461 };