From 13a1300b47711e7e31760afbcd14fd3bcbe5eca1 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 27 Jan 2022 08:12:53 -0600 Subject: [PATCH] fixed scoping issues with copied static ifs --- include/astnodes.h | 1 + src/parser.c | 1 + src/symres.c | 20 +++++++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/astnodes.h b/include/astnodes.h index 0354c0e8..a57e75b4 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -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; }; diff --git a/src/parser.c b/src/parser.c index 5d52cf82..905cc4e0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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); diff --git a/src/symres.c b/src/symres.c index 7a4175b4..647e5322 100644 --- a/src/symres.c +++ b/src/symres.c @@ -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); -- 2.25.1