Skip to content

Commit 4b69295

Browse files
committed
GDScript: Fix lambdas capturing non-local variables
1 parent 2149682 commit 4b69295

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4061,10 +4061,23 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
40614061
mark_lambda_use_self();
40624062
return; // No need to capture.
40634063
}
4064-
// If the identifier is local, check if it's any kind of capture by comparing their source function.
4065-
// Only capture locals and enum values. Constants are still accessible from the lambda using the script reference. If not, this method is done.
4066-
if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT) {
4067-
return;
4064+
4065+
switch (p_identifier->source) {
4066+
case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
4067+
case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
4068+
case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
4069+
case GDScriptParser::IdentifierNode::LOCAL_BIND:
4070+
break; // Need to capture.
4071+
case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE: // A global.
4072+
case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
4073+
case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
4074+
case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
4075+
case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
4076+
case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
4077+
case GDScriptParser::IdentifierNode::MEMBER_CLASS:
4078+
case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
4079+
case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
4080+
return; // No need to capture.
40684081
}
40694082

40704083
GDScriptParser::FunctionNode *function_test = current_lambda->function;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# GH-92217
2+
# TODO: Add more tests.
3+
4+
static var static_var: int:
5+
set(value):
6+
prints("set static_var", value)
7+
get:
8+
print("get static_var")
9+
return 0
10+
11+
var member_var: int:
12+
set(value):
13+
prints("set member_var", value)
14+
get:
15+
print("get member_var")
16+
return 0
17+
18+
func test():
19+
var lambda := func ():
20+
var _tmp := static_var
21+
_tmp = member_var
22+
23+
static_var = 1
24+
member_var = 1
25+
26+
lambda.call()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GDTEST_OK
2+
get static_var
3+
get member_var
4+
set static_var 1
5+
set member_var 1

0 commit comments

Comments
 (0)