Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Disallow strings as comments
HolonProduction4e7388953938
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cppindex 59473db96f9..a318f04d78a 100644--- a/modules/gdscript/gdscript_parser.cpp+++ b/modules/gdscript/gdscript_parser.cpp@@ -763,14 +763,10 @@ void GDScriptParser::parse_program() {break;}}- } else if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) {- // Allow strings in class body as multiline comments.- advance();- if (!match(GDScriptTokenizer::Token::NEWLINE)) {- push_error("Expected newline after comment string.");- }} else {- break;+ if (!parse_standalone_string()) {+ break;+ }}}@@ -806,12 +802,7 @@ void GDScriptParser::parse_program() {can_have_class_or_extends = false;break;case GDScriptTokenizer::Token::LITERAL:- if (current.literal.get_type() == Variant::STRING) {- // Allow strings in class body as multiline comments.- advance();- if (!match(GDScriptTokenizer::Token::NEWLINE)) {- push_error("Expected newline after comment string.");- }+ if (parse_standalone_string()) {break;}[[fallthrough]];@@ -1201,12 +1192,7 @@ void GDScriptParser::parse_class_body(bool p_is_multiline) {class_end = true;break;case GDScriptTokenizer::Token::LITERAL:- if (current.literal.get_type() == Variant::STRING) {- // Allow strings in class body as multiline comments.- advance();- if (!match(GDScriptTokenizer::Token::NEWLINE)) {- push_error("Expected newline after comment string.");- }+ if (parse_standalone_string()) {break;}[[fallthrough]];@@ -2204,12 +2190,6 @@ GDScriptParser::Node *GDScriptParser::parse_statement() {// 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;- case Node::LITERAL:- // Allow strings as multiline comments.- if (static_cast<GDScriptParser::LiteralNode *>(expression)->value.get_type() != Variant::STRING) {- push_warning(expression, GDScriptWarning::STANDALONE_EXPRESSION);- }- break;case Node::TERNARY_OPERATOR:push_warning(expression, GDScriptWarning::STANDALONE_TERNARY);break;@@ -3930,6 +3910,21 @@ GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) {return type;}+bool GDScriptParser::parse_standalone_string() {+ if (check(GDScriptTokenizer::Token::LITERAL) && current.literal.get_type() == Variant::STRING) {+ // For compatibility we allow standalone strings without erroring.+ advance();+#ifdef DEBUG_ENABLED+ push_warning(previous.start_line, previous.start_column, previous.end_line, previous.end_column, GDScriptWarning::STANDALONE_EXPRESSION);+#endif+ if (!match(GDScriptTokenizer::Token::NEWLINE)) {+ push_error("Expected newline after comment string.");+ }+ return true;+ }+ return false;+}+#ifdef TOOLS_ENABLEDenum DocLineState {DOC_LINE_NORMAL,diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.hindex 60a94b32a7d..a1ed6bab85f 100644--- a/modules/gdscript/gdscript_parser.h+++ b/modules/gdscript/gdscript_parser.h@@ -1654,6 +1654,8 @@ private:ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);TypeNode *parse_type(bool p_allow_void = false);+ // TODO: Remove in 5.x.+ bool parse_standalone_string();#ifdef TOOLS_ENABLEDint max_script_doc_line = INT_MAX;diff --git a/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.gd b/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.gddeleted file mode 100644index 3ecd65ad9cd..00000000000--- a/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.gd+++ /dev/null@@ -1,21 +0,0 @@-"""-This is a comment.-"""--@tool--"""-This is also a comment.-"""--extends RefCounted--'''-This is a comment too.-'''--func test():- """- This too is a comment.- """- print("ok")diff --git a/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.out b/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.outdeleted file mode 100644index 1b47ed10dc0..00000000000--- a/modules/gdscript/tests/scripts/parser/features/allow_strings_as_comments.out+++ /dev/null@@ -1,2 +0,0 @@-GDTEST_OK-okdiff --git a/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.gd b/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.gdindex 74f42b012b2..ace01ce92a9 100644--- a/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.gd+++ b/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.gd@@ -1,11 +1,29 @@+"""+This is not a comment.+"""++@tool++"This also isn't a comment."++extends RefCounted++'''+This one neither.+'''+func test():# The following statements should all be reported as standalone expressions:12340.0 + 0.0- Color(1, 1, 1)+ Color(1, 1, 1) # TODOVector3.ZERO[true, false]- float(125)+ float(125) # TODO+ """+ Python-like "comment".+ """+ 'single line string'# The following statements should not produce `STANDALONE_EXPRESSION`:var _a = 1_a = 2 # Assignment is a local (or global) side effect.@@ -14,8 +32,7 @@ func test():absi(4) # A call (in general) can have side effects.@warning_ignore("return_value_discarded")preload("../../utils.notest.gd") # A static initializer may have side effects.- """- Python-like "comment".- """@warning_ignore("standalone_ternary")1 if 2 else 3 # Produces `STANDALONE_TERNARY` instead.++'Still no comment...'diff --git a/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.out b/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.outindex a2b0d37e6b5..0297fb99369 100644--- a/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.out+++ b/modules/gdscript/tests/scripts/parser/warnings/standalone_expression.out@@ -1,5 +1,11 @@GDTEST_OK-~~ WARNING at line 3: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).-~~ WARNING at line 4: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).-~~ WARNING at line 6: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 1: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).~~ WARNING at line 7: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 11: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 17: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 18: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 20: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 21: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 23: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 26: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).+~~ WARNING at line 38: (STANDALONE_EXPRESSION) Standalone expression (the line may have no effect).