Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix analyzer pushing SHADOWED_VARIABLE warning for members shadowed in subclass #98873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

girdenis-p
Copy link

@girdenis-p girdenis-p commented Nov 6, 2024

This fixes #74395. It does this by comparing the class which contains the shadowed member with the class containing the variable, and pushing the SHADOWED_VARIABLE warning only if the classes are the same, otherwise the SHADOWED_VARIABLE_BASE_CLASS warning is pushed.

Additionally, SHADOWED_VARIABLE_BASE_CLASS can now take an extra symbol which helps to specify the line for non native base class.

Comments and docs have been updated to highlight that these warnings are related to shadowed members in the concerning class instead of just shadowed variables.

@girdenis-p girdenis-p requested review from a team as code owners November 6, 2024 00:56
@dalexeev dalexeev added this to the 4.4 milestone Nov 6, 2024
doc/classes/ProjectSettings.xml Outdated Show resolved Hide resolved
doc/classes/ProjectSettings.xml Outdated Show resolved Hide resolved
modules/gdscript/gdscript_analyzer.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_warning.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_warning.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_warning.h Outdated Show resolved Hide resolved
Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. It would be great to check the grammar in documentation and warning messages.

@@ -0,0 +1,7 @@
class_name ShadowingBase

const base_const_member = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks unused.

Copy link
Author

@girdenis-p girdenis-p Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test cast for it in the shadowning.gd script.

modules/gdscript/gdscript_warning.cpp Outdated Show resolved Hide resolved
@@ -5809,8 +5809,7 @@ void GDScriptAnalyzer::validate_call_arg(const List<GDScriptParser::DataType> &p
#ifdef DEBUG_ENABLED
void GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_identifier, const String &p_context, const bool p_in_local_scope) {
const StringName &name = p_identifier->name;
GDScriptParser::DataType base = parser->current_class->get_datatype();
GDScriptParser::ClassNode *base_class = base.class_type;
const GDScriptParser::DataType current_class_type = parser->current_class->get_datatype();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: this isn't used until line 5841. It's recommended to declare variables as close to the first use as possible, if it doesn't affect anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it above:

if (p_in_local_scope) {
	GDScriptParser::ClassNode *base_class = current_class_type.class_type;

this way it is accessible for base_class and native_base_class (defined later).

…n subclasses

This fixes a bug in the analyzer where it did not push the SHADOWED_VARIABLE_BASE_CLASS
warning for members shadowed by variable in subclass. It does this by comparing the class
which contains the shadowed member with the class containing the variable, and pushing
SHADOWED_VARIABLE only if the classes are the same. Additionally, SHADOWED_VARIABLE_BASE_CLASS
can take an extra symbol which helps to specify the line for non native base class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SHADOWED_VARIABLE warning is unclear for derived classes
3 participants