Skip to content

Commit 4d107c1

Browse files
committed
Merge pull request godotengine#109561 from aaronfranke/gdscript-error-message
Improve error messages for lambda functions without a body
2 parents 42224bb + b735972 commit 4d107c1

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,16 +1987,18 @@ void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_fun
19871987
}
19881988
p_function->resolved_body = true;
19891989

1990-
if (p_function->is_abstract) {
1991-
// Abstract functions don't have a body.
1992-
if (!p_function->body->statements.is_empty()) {
1993-
push_error(R"(Abstract function cannot have a body.)", p_function->body);
1990+
if (p_function->body->statements.is_empty()) {
1991+
// Non-abstract functions must have a body.
1992+
if (p_function->source_lambda != nullptr) {
1993+
push_error(R"(A lambda function must have a ":" followed by a body.)", p_function);
1994+
} else if (!p_function->is_abstract) {
1995+
push_error(R"(A function must either have a ":" followed by a body, or be marked as "@abstract".)", p_function);
19941996
}
19951997
return;
19961998
} else {
1997-
// Non-abstract functions must have a body.
1998-
if (p_function->body->statements.is_empty()) {
1999-
push_error(R"(A function must either have a ":" followed by a body, or be marked as "@abstract".)", p_function);
1999+
// Abstract functions must not have a body.
2000+
if (p_function->is_abstract) {
2001+
push_error(R"(An abstract function cannot have a body.)", p_function->body);
20002002
return;
20012003
}
20022004
}

modules/gdscript/tests/scripts/analyzer/errors/abstract_methods.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ class Test4 extends AbstractClass:
3737
@abstract @abstract class DuplicateAbstract:
3838
pass
3939

40+
func holding_some_invalid_lambda(invalid_default_arg = func():):
41+
var some_invalid_lambda = (func():)
42+
4043
func test():
4144
pass

modules/gdscript/tests/scripts/analyzer/errors/abstract_methods.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ GDTEST_ANALYZER_ERROR
22
>> ERROR at line 37: "@abstract" annotation can only be used once per class.
33
>> ERROR at line 28: "@abstract" annotation can only be used once per function.
44
>> ERROR at line 35: "@abstract" annotation cannot be applied to static functions.
5+
>> ERROR at line 40: A lambda function must have a ":" followed by a body.
6+
>> ERROR at line 41: A lambda function must have a ":" followed by a body.
57
>> ERROR at line 11: Class "Test1" is not abstract but contains abstract methods. Mark the class as "@abstract" or remove "@abstract" from all methods in this class.
68
>> ERROR at line 14: Class "Test2" must implement "AbstractClass.some_func()" and other inherited abstract methods or be marked as "@abstract".
79
>> ERROR at line 17: Class "Test3" must implement "AbstractClassAgain.some_func()" and other inherited abstract methods or be marked as "@abstract".
810
>> ERROR at line 22: Cannot call the parent class' abstract function "some_func()" because it hasn't been defined.
911
>> ERROR at line 25: Cannot call the parent class' abstract function "some_func()" because it hasn't been defined.
10-
>> ERROR at line 32: Abstract function cannot have a body.
12+
>> ERROR at line 32: An abstract function cannot have a body.
1113
>> ERROR at line 35: A function must either have a ":" followed by a body, or be marked as "@abstract".

0 commit comments

Comments
 (0)