fixed scoping issues with copied static ifs
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 27 Jan 2022 14:12:53 +0000 (08:12 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 27 Jan 2022 14:12:53 +0000 (08:12 -0600)
include/astnodes.h
src/parser.c
src/symres.c

index 0354c0e873b53ef3fec2e93d2cf467640af44ff8..a57e75b4f6bd4d692d94d3387c82dc33d3eed14a 100644 (file)
@@ -781,6 +781,7 @@ struct AstIfWhile {
     AstBlock *false_stmt;
 
     // Used by Static_If
+    Scope *defined_in_scope;
     bh_arr(struct Entity *) true_entities;
     bh_arr(struct Entity *) false_entities;
 };
index 5d52cf8254bdfcdd9c8d68319e0febf098f7c53e..905cc4e044662077e84e2f23370c251a03e85ea1 100644 (file)
@@ -2671,6 +2671,7 @@ static AstEnumType* parse_enum_declaration(OnyxParser* parser) {
 static AstIf* parse_static_if_stmt(OnyxParser* parser, b32 parse_block_as_statements) {
     AstIf* static_if_node = make_node(AstIf, Ast_Kind_Static_If);
     static_if_node->token = expect_token(parser, '#');
+    static_if_node->defined_in_scope = parser->current_scope;
     expect_token(parser, Token_Type_Keyword_If);
 
     static_if_node->cond = parse_expression(parser, 0);
index 7a4175b4f5528f313c7be7a4bd5a60c6bd157690..647e5322a2f2670cc9c0dc9fe1033c7899f8463c 100644 (file)
@@ -957,8 +957,26 @@ SymresStatus symres_function_header(AstFunction* func) {
 
     if (func->nodes_that_need_entities_after_clone && bh_arr_length(func->nodes_that_need_entities_after_clone) > 0) {
         bh_arr_each(AstNode *, node, func->nodes_that_need_entities_after_clone) {
+            // This makes a lot of assumptions about how these nodes are being processed,
+            // and I don't want to start using this with other nodes without considering
+            // what the ramifications of that is.
+            assert((*node)->kind == Ast_Kind_Static_If);
+
             // Need to curr_scope->parent because curr_scope is the function body scope.
-            add_entities_for_node(NULL, *node, curr_scope->parent, func->entity->package);
+            Scope *scope = curr_scope->parent;
+
+            if ((*node)->kind == Ast_Kind_Static_If) {
+                AstIf *static_if = (AstIf *) *node;
+                assert(static_if->defined_in_scope);
+                scope = static_if->defined_in_scope;
+
+                if (func->poly_scope) {
+                    scope = scope_create(context.ast_alloc, scope, static_if->token->pos);
+                    scope_include(scope, func->poly_scope, static_if->token->pos);
+                }
+            }
+
+            add_entities_for_node(NULL, *node, scope, func->entity->package);
         }
 
         bh_arr_set_length(func->nodes_that_need_entities_after_clone, 0);