From 0a28b4cd94540e1b99ccd79a70bd37170e1cb614 Mon Sep 17 00:00:00 2001 From: George Marques Date: Mon, 23 May 2022 12:25:03 -0300 Subject: [PATCH 1/3] GDScript: Do not allow standalone lambdas They cannot be accessed in this case, so an error is shown to avoid misleading the uses, especially in case of named lambdas. --- modules/gdscript/gdscript_parser.cpp | 4 ++++ .../gdscript/tests/scripts/parser/errors/lambda_standalone.gd | 3 +++ .../tests/scripts/parser/errors/lambda_standalone.out | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 modules/gdscript/tests/scripts/parser/errors/lambda_standalone.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/lambda_standalone.out diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 96d1f68f605..8327da79e21 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); } 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 00000000000..fa0a43094e3 --- /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 00000000000..c6830c8258a --- /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. From 969f1980d29cfc48c45a1bebdc98935b641dda50 Mon Sep 17 00:00:00 2001 From: George Marques Date: Mon, 23 May 2022 21:13:25 -0300 Subject: [PATCH 2/3] GDScript: Fix `if` after lambda being seen as ternary --- modules/gdscript/gdscript_parser.cpp | 2 +- .../tests/scripts/parser/features/if_after_lambda.gd | 7 +++++++ .../tests/scripts/parser/features/if_after_lambda.out | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 modules/gdscript/tests/scripts/parser/features/if_after_lambda.gd create mode 100644 modules/gdscript/tests/scripts/parser/features/if_after_lambda.out diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 8327da79e21..f23cddcbda0 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -2103,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. 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 00000000000..f5e26ab1abc --- /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 00000000000..58774d2d3fe --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/if_after_lambda.out @@ -0,0 +1,2 @@ +GDTEST_OK +hello From 1b76a9d70515cc98d9772c72daadbf2634aa788f Mon Sep 17 00:00:00 2001 From: George Marques Date: Mon, 23 May 2022 21:38:31 -0300 Subject: [PATCH 3/3] GDScript: Fix lambda captures in default argument values --- modules/gdscript/gdscript_parser.cpp | 3 +++ .../parser/features/lambda_default_parameter_capture.gd | 7 +++++++ .../parser/features/lambda_default_parameter_capture.out | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.gd create mode 100644 modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.out diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index f23cddcbda0..b93fff3914b 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -2926,6 +2926,9 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_p current_function = function; SuiteNode *body = alloc_node(); + 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/features/lambda_default_parameter_capture.gd b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.gd new file mode 100644 index 00000000000..2140b6923e1 --- /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 00000000000..ce3241b94d3 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/lambda_default_parameter_capture.out @@ -0,0 +1,2 @@ +GDTEST_OK +local