Skip to content

Commit

Permalink
GDScript: Fix lambda resolution with cyclic references
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Aug 25, 2023
1 parent 6758a7f commit 89429b0
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 31 deletions.
93 changes: 63 additions & 30 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,9 +882,12 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class,
case GDScriptParser::ClassNode::Member::VARIABLE: {
bool previous_static_context = static_context;
static_context = member.variable->is_static;

check_class_member_name_conflict(p_class, member.variable->identifier->name, member.variable);

member.variable->set_datatype(resolving_datatype);
resolve_variable(member.variable, false);
resolve_pending_lambda_bodies();

// Apply annotations.
for (GDScriptParser::AnnotationNode *&E : member.variable->annotations) {
Expand All @@ -893,7 +896,9 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class,
E->apply(parser, member.variable);
}
}

static_context = previous_static_context;

#ifdef DEBUG_ENABLED
if (member.variable->exported && member.variable->onready) {
parser->push_warning(member.variable, GDScriptWarning::ONREADY_WITH_EXPORT);
Expand Down Expand Up @@ -1345,6 +1350,11 @@ void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, co
}
}

if (!pending_body_resolution_lambdas.is_empty()) {
ERR_PRINT("GDScript bug (please report): Not all pending lambda bodies were resolved in time.");
resolve_pending_lambda_bodies();
}

parser->current_class = previous_class;
}

Expand Down Expand Up @@ -1757,6 +1767,7 @@ void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
#endif // DEBUG_ENABLED

resolve_node(stmt);
resolve_pending_lambda_bodies();

#ifdef DEBUG_ENABLED
parser->ignored_warnings = previously_ignored_warnings;
Expand Down Expand Up @@ -3080,7 +3091,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
base_type.is_meta_type = false;
is_self = true;

if (p_call->callee == nullptr && !lambda_stack.is_empty()) {
if (p_call->callee == nullptr && current_lambda != nullptr) {
push_error("Cannot use `super()` inside a lambda.", p_call);
}
} else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
Expand Down Expand Up @@ -3753,7 +3764,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
}
}

if (!lambda_stack.is_empty()) {
if (current_lambda != nullptr) {
// If the identifier is a member variable (including the native class properties) or a signal, we consider the lambda to be using `self`, so we keep a reference to the current instance.
if (source_is_variable || source_is_signal) {
mark_lambda_use_self();
Expand All @@ -3765,7 +3776,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
return;
}

GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
GDScriptParser::FunctionNode *function_test = current_lambda->function;
// Make sure we aren't capturing variable in the same lambda.
// This also add captures for nested lambdas.
while (function_test != nullptr && function_test != p_identifier->source_function && function_test->source_lambda != nullptr && !function_test->source_lambda->captures_indices.has(p_identifier->name)) {
Expand Down Expand Up @@ -3920,34 +3931,12 @@ void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
return;
}

lambda_stack.push_back(p_lambda);
GDScriptParser::LambdaNode *previous_lambda = current_lambda;
current_lambda = p_lambda;
resolve_function_signature(p_lambda->function, p_lambda, true);
resolve_function_body(p_lambda->function, true);
lambda_stack.pop_back();
current_lambda = previous_lambda;

int captures_amount = p_lambda->captures.size();
if (captures_amount > 0) {
// Create space for lambda parameters.
// At the beginning to not mess with optional parameters.
int param_count = p_lambda->function->parameters.size();
p_lambda->function->parameters.resize(param_count + captures_amount);
for (int i = param_count - 1; i >= 0; i--) {
p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
}

// Add captures as extra parameters at the beginning.
for (int i = 0; i < p_lambda->captures.size(); i++) {
GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
capture_param->identifier = capture;
capture_param->usages = capture->usages;
capture_param->set_datatype(capture->get_datatype());

p_lambda->function->parameters.write[i] = capture_param;
p_lambda->function->parameters_indices[capture->name] = i;
}
}
pending_body_resolution_lambdas.push_back(p_lambda);
}

void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
Expand Down Expand Up @@ -5288,9 +5277,53 @@ void GDScriptAnalyzer::downgrade_node_type_source(GDScriptParser::Node *p_node)
}

void GDScriptAnalyzer::mark_lambda_use_self() {
for (GDScriptParser::LambdaNode *lambda : lambda_stack) {
GDScriptParser::LambdaNode *lambda = current_lambda;
while (lambda != nullptr) {
lambda->use_self = true;
lambda = lambda->parent_lambda;
}
}

void GDScriptAnalyzer::resolve_pending_lambda_bodies() {
if (pending_body_resolution_lambdas.is_empty()) {
return;
}

GDScriptParser::LambdaNode *previous_lambda = current_lambda;

List<GDScriptParser::LambdaNode *> lambdas = pending_body_resolution_lambdas;
pending_body_resolution_lambdas.clear();

for (GDScriptParser::LambdaNode *lambda : lambdas) {
current_lambda = lambda;
resolve_function_body(lambda->function, true);

int captures_amount = lambda->captures.size();
if (captures_amount > 0) {
// Create space for lambda parameters.
// At the beginning to not mess with optional parameters.
int param_count = lambda->function->parameters.size();
lambda->function->parameters.resize(param_count + captures_amount);
for (int i = param_count - 1; i >= 0; i--) {
lambda->function->parameters.write[i + captures_amount] = lambda->function->parameters[i];
lambda->function->parameters_indices[lambda->function->parameters[i]->identifier->name] = i + captures_amount;
}

// Add captures as extra parameters at the beginning.
for (int i = 0; i < lambda->captures.size(); i++) {
GDScriptParser::IdentifierNode *capture = lambda->captures[i];
GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
capture_param->identifier = capture;
capture_param->usages = capture->usages;
capture_param->set_datatype(capture->get_datatype());

lambda->function->parameters.write[i] = capture_param;
lambda->function->parameters_indices[capture->name] = i;
}
}
}

current_lambda = previous_lambda;
}

bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
Expand Down
4 changes: 3 additions & 1 deletion modules/gdscript/gdscript_analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class GDScriptAnalyzer {
HashMap<String, Ref<GDScriptParserRef>> depended_parsers;

const GDScriptParser::EnumNode *current_enum = nullptr;
List<GDScriptParser::LambdaNode *> lambda_stack;
GDScriptParser::LambdaNode *current_lambda = nullptr;
List<GDScriptParser::LambdaNode *> pending_body_resolution_lambdas;
bool static_context = false;

// Tests for detecting invalid overloading of script members
Expand Down Expand Up @@ -129,6 +130,7 @@ class GDScriptAnalyzer {
void mark_node_unsafe(const GDScriptParser::Node *p_node);
void downgrade_node_type_source(GDScriptParser::Node *p_node);
void mark_lambda_use_self();
void resolve_pending_lambda_bodies();
bool class_exists(const StringName &p_class) const;
Ref<GDScriptParserRef> get_parser_for(const String &p_path);
void reduce_identifier_from_base_set_class(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType p_identifier_datatype);
Expand Down
6 changes: 6 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,8 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_preload(ExpressionNode *p_
GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign) {
LambdaNode *lambda = alloc_node<LambdaNode>();
lambda->parent_function = current_function;
lambda->parent_lambda = current_lambda;

FunctionNode *function = alloc_node<FunctionNode>();
function->source_lambda = lambda;

Expand Down Expand Up @@ -3178,6 +3180,9 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_p
FunctionNode *previous_function = current_function;
current_function = function;

LambdaNode *previous_lambda = current_lambda;
current_lambda = lambda;

SuiteNode *body = alloc_node<SuiteNode>();
body->parent_function = current_function;
body->parent_block = current_suite;
Expand Down Expand Up @@ -3215,6 +3220,7 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_p
}

current_function = previous_function;
current_lambda = previous_lambda;
in_lambda = previous_in_lambda;
lambda->function = function;

Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ class GDScriptParser {
struct LambdaNode : public ExpressionNode {
FunctionNode *function = nullptr;
FunctionNode *parent_function = nullptr;
LambdaNode *parent_lambda = nullptr;
Vector<IdentifierNode *> captures;
HashMap<StringName, int> captures_indices;
bool use_self = false;
Expand Down Expand Up @@ -1321,6 +1322,7 @@ class GDScriptParser {

ClassNode *current_class = nullptr;
FunctionNode *current_function = nullptr;
LambdaNode *current_lambda = nullptr;
SuiteNode *current_suite = nullptr;

CompletionContext completion_context;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var f = (func (_a): return 0).call(x)
var x = f

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Could not resolve member "f": Cyclic reference.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var f = func (_a = x): return 0
var x = f

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Could not resolve member "f": Cyclic reference.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# GH-70592

var f: Callable = func ():
x = 2
return 1

var x: int = f.call()

var g: Array[Callable] = [
func ():
y += 10
return 1,
func ():
y += 20
return 2,
]

var y: int = g[0].call() + g[1].call()

func test():
print(x)
f.call()
print(x)

print(y)
g[0].call()
g[1].call()
print(y)

# This prevents memory leak in CI. TODO: Investigate it.
# Also you cannot run the `EditorScript` twice without the cleaning. Error:
# Condition "!p_keep_state && has_instances" is true. Returning: ERR_ALREADY_IN_USE
f = Callable()
g.clear()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GDTEST_OK
1
2
3
33

0 comments on commit 89429b0

Please sign in to comment.