diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 35eb21b81664..468063a71ed8 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -6158,6 +6158,15 @@ void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_identifier } else if (GDScriptParser::get_builtin_type(name) < Variant::VARIANT_MAX) { parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in type"); return; + } else if (name == "PI" || name == "TAU" || name == "INF" || name == "NAN") { + parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global constant"); + return; + } else if (CoreConstants::is_global_constant(name)) { + parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global constant"); + return; + } else if (CoreConstants::is_global_enum(name)) { + parser->push_warning(p_identifier, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global enum"); + return; } } diff --git a/modules/gdscript/gdscript_warning.h b/modules/gdscript/gdscript_warning.h index eae1013f095e..7469b20a4ca3 100644 --- a/modules/gdscript/gdscript_warning.h +++ b/modules/gdscript/gdscript_warning.h @@ -54,7 +54,7 @@ class GDScriptWarning { UNUSED_SIGNAL, // Signal is defined but never explicitly used in the class. SHADOWED_VARIABLE, // A local variable/constant shadows a current class member. SHADOWED_VARIABLE_BASE_CLASS, // A local variable/constant shadows a base class member. - SHADOWED_GLOBAL_IDENTIFIER, // A global class or function has the same name as variable. + SHADOWED_GLOBAL_IDENTIFIER, // A local variable/constant/enum/function shadows a global type, class, function, enum, or constant. UNREACHABLE_CODE, // Code after a return statement. UNREACHABLE_PATTERN, // Pattern in a match statement after a catch all pattern (wildcard or bind). STANDALONE_EXPRESSION, // Expression not assigned to a variable. diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.gd b/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.gd index 0624f3fd379e..7af68bd0a180 100644 --- a/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.gd +++ b/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.gd @@ -19,5 +19,9 @@ func test(): var base_variable_member const base_function_member = 1 var base_const_member + var PI = 4 + var OK = -1 + var Side = -2 + var UINT8_MAX = -3 print('warn') diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.out b/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.out index 702361f8a391..d0eff1baec34 100644 --- a/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.out +++ b/modules/gdscript/tests/scripts/analyzer/warnings/shadowning.out @@ -10,4 +10,8 @@ GDTEST_OK ~~ WARNING at line 19: (SHADOWED_VARIABLE_BASE_CLASS) The local variable "base_variable_member" is shadowing an already-declared variable at line 4 in the base class "ShadowingBase". ~~ WARNING at line 20: (SHADOWED_VARIABLE_BASE_CLASS) The local constant "base_function_member" is shadowing an already-declared function at line 6 in the base class "ShadowingBase". ~~ WARNING at line 21: (SHADOWED_VARIABLE_BASE_CLASS) The local variable "base_const_member" is shadowing an already-declared constant at line 3 in the base class "ShadowingBase". +~~ WARNING at line 22: (SHADOWED_GLOBAL_IDENTIFIER) The variable "PI" has the same name as a global constant. +~~ WARNING at line 23: (SHADOWED_GLOBAL_IDENTIFIER) The variable "OK" has the same name as a global constant. +~~ WARNING at line 24: (SHADOWED_GLOBAL_IDENTIFIER) The variable "Side" has the same name as a global enum. +~~ WARNING at line 25: (SHADOWED_GLOBAL_IDENTIFIER) The variable "UINT8_MAX" has the same name as a global constant. warn diff --git a/modules/gdscript/tests/scripts/parser/features/allowed_keywords_as_identifiers.out b/modules/gdscript/tests/scripts/parser/features/allowed_keywords_as_identifiers.out index 8ac8e92ef7a1..f8fdcf4bd4ee 100644 --- a/modules/gdscript/tests/scripts/parser/features/allowed_keywords_as_identifiers.out +++ b/modules/gdscript/tests/scripts/parser/features/allowed_keywords_as_identifiers.out @@ -1,4 +1,8 @@ GDTEST_OK +~~ WARNING at line 6: (SHADOWED_GLOBAL_IDENTIFIER) The variable "PI" has the same name as a global constant. +~~ WARNING at line 9: (SHADOWED_GLOBAL_IDENTIFIER) The variable "INF" has the same name as a global constant. +~~ WARNING at line 12: (SHADOWED_GLOBAL_IDENTIFIER) The variable "NAN" has the same name as a global constant. +~~ WARNING at line 15: (SHADOWED_GLOBAL_IDENTIFIER) The variable "TAU" has the same name as a global constant. match PI INF