diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-05-24 08:14:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-24 08:14:48 +0200 |
commit | db5a86339b9a5aba1a99d133ecfa3673f1ca1a97 (patch) | |
tree | f2f55f3bda386c9f7647695be110379b1ef6b3a5 | |
parent | 3555b970bfc233be5d050cc3eb178886e10b406c (diff) | |
parent | 1b76a9d70515cc98d9772c72daadbf2634aa788f (diff) |
Merge pull request #61345 from vnen/gdscript-lambda-issues
GDScript: A few fixes for lambda issues
7 files changed, 31 insertions, 1 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 96d1f68f60..b93fff3914 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1624,6 +1624,10 @@ GDScriptParser::Node *GDScriptParser::parse_statement() { case Node::AWAIT: // Fine. break; + case Node::LAMBDA: + // Standalone lambdas can't be used, so make this an error. + push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression); + break; default: push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION); } @@ -2099,7 +2103,7 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_precedence(Precedence p_pr ExpressionNode *previous_operand = (this->*prefix_rule)(nullptr, p_can_assign); while (p_precedence <= get_rule(current.type)->precedence) { - if (previous_operand == nullptr || (p_stop_on_assign && current.type == GDScriptTokenizer::Token::EQUAL)) { + if (previous_operand == nullptr || (p_stop_on_assign && current.type == GDScriptTokenizer::Token::EQUAL) || (previous_operand->type == Node::LAMBDA && lambda_ended)) { return previous_operand; } // Also switch multiline mode on here for infix operators. @@ -2922,6 +2926,9 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_p current_function = function; SuiteNode *body = alloc_node<SuiteNode>(); + body->parent_function = current_function; + body->parent_block = current_suite; + SuiteNode *previous_suite = current_suite; current_suite = body; diff --git a/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.gd b/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.gd new file mode 100644 index 0000000000..fa0a43094e --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.gd @@ -0,0 +1,3 @@ +func test(): + func standalone(): + print("can't be accessed") diff --git a/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.out b/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.out new file mode 100644 index 0000000000..c6830c8258 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/lambda_standalone.out @@ -0,0 +1,2 @@ +GDTEST_PARSER_ERROR +Standalone lambdas cannot be accessed. Consider assigning it to a variable. diff --git a/modules/gdscript/tests/scripts/parser/features/if_after_lambda.gd b/modules/gdscript/tests/scripts/parser/features/if_after_lambda.gd new file mode 100644 index 0000000000..f5e26ab1ab --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/if_after_lambda.gd @@ -0,0 +1,7 @@ +# https://github.com/godotengine/godot/issues/61231 + +func test(): + var my_lambda = func(): + print("hello") + if 0 == 0: + my_lambda.call() diff --git a/modules/gdscript/tests/scripts/parser/features/if_after_lambda.out b/modules/gdscript/tests/scripts/parser/features/if_after_lambda.out new file mode 100644 index 0000000000..58774d2d3f --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/if_after_lambda.out @@ -0,0 +1,2 @@ +GDTEST_OK +hello diff --git a/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.gd b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.gd new file mode 100644 index 0000000000..2140b6923e --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.gd @@ -0,0 +1,7 @@ +# https://github.com/godotengine/godot/issues/56751 + +func test(): + var x = "local" + var lambda = func(param = x): + print(param) + lambda.call() diff --git a/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.out b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.out new file mode 100644 index 0000000000..ce3241b94d --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.out @@ -0,0 +1,2 @@ +GDTEST_OK +local |