Skip to content

Commit 7d91178

Browse files
fixed some bugs; reimplemented unit tests
1 parent c04e9aa commit 7d91178

File tree

11 files changed

+64
-10
lines changed

11 files changed

+64
-10
lines changed

doc/classes/ProjectSettings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@
480480
<member name="debug/gdscript/warnings/assert_always_true" type="int" setter="" getter="" default="1">
481481
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when an [code]assert[/code] call always evaluates to [code]true[/code].
482482
</member>
483+
<member name="debug/gdscript/warnings/call_private_method" type="int" setter="" getter="" default="1">
484+
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when a class method is called from external places, such as calling a private method from other classes that are not derived from the class where the method is defined.
485+
</member>
483486
<member name="debug/gdscript/warnings/confusable_capture_reassignment" type="int" setter="" getter="" default="1">
484487
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when a local variable captured by a lambda is reassigned, since this does not modify the outer local variable.
485488
</member>

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,15 +1965,14 @@ void GDScriptAnalyzer::check_access_private_member(GDScriptParser::IdentifierNod
19651965
}
19661966

19671967
GDScriptParser::DataType target_resolved = type_from_metatype(p_datatype);
1968-
bool target_has_member = target_resolved.class_type != nullptr && !target_resolved.class_type->has_member(p_identifier->name);
1969-
if (!target_has_member && p_is_call && !ClassDB::has_method(target_resolved.native_type, p_identifier->name)) {
1968+
if (target_resolved.class_type != nullptr && !target_resolved.class_type->has_member(p_identifier->name)) {
19701969
return; // Accessing an inexisting method
19711970
}
1972-
if (is_type_compatible(p_datatype, parser->current_class->get_datatype(), true)) {
1971+
if (is_type_compatible(target_resolved, type_from_metatype(parser->current_class->get_datatype()))) {
19731972
return;
19741973
}
19751974

1976-
parser->push_warning(p_identifier, p_is_call ? GDScriptWarning::CALLING_PRIVATE_METHOD : GDScriptWarning::ACCESS_PRIVATE_MEMBER, p_identifier->name);
1975+
parser->push_warning(p_identifier, p_is_call ? GDScriptWarning::CALL_PRIVATE_METHOD : GDScriptWarning::ACCESS_PRIVATE_MEMBER, p_identifier->name);
19771976

19781977
/*GDScriptParser::ClassNode *base_class = parser->current_class->base_type.class_type;
19791978
while (base_class != nullptr) {

modules/gdscript/gdscript_warning.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ String GDScriptWarning::get_message() const {
165165
case ACCESS_PRIVATE_MEMBER:
166166
CHECK_SYMBOLS(1);
167167
return vformat(R"(Trying to access a private member "%s" from an external place, which would cause problems during runtime.)", symbols[0]);
168-
case CALLING_PRIVATE_METHOD:
168+
case CALL_PRIVATE_METHOD:
169169
CHECK_SYMBOLS(1);
170170
return vformat(R"*(Trying to call a private method "%s()" from an external place, which would cause problems during runtime.)*", symbols[0]);
171171
#ifndef DISABLE_DEPRECATED
@@ -245,7 +245,7 @@ String GDScriptWarning::get_name_from_code(Code p_code) {
245245
"GET_NODE_DEFAULT_WITHOUT_ONREADY",
246246
"ONREADY_WITH_EXPORT",
247247
"ACCESS_PRIVATE_MEMBER",
248-
"CALLING_PRIVATE_METHOD",
248+
"CALL_PRIVATE_METHOD",
249249
#ifndef DISABLE_DEPRECATED
250250
"PROPERTY_USED_AS_FUNCTION",
251251
"CONSTANT_USED_AS_FUNCTION",

modules/gdscript/gdscript_warning.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class GDScriptWarning {
9090
GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation.
9191
ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended.
9292
ACCESS_PRIVATE_MEMBER, // Accessing a private member from external places. E.g. accessing an `_`-prefixed member from other classes that are not derived from the class where the member is defined.
93-
CALLING_PRIVATE_METHOD, // Calling a private method from external places. E.g. calling an `_`-prefixed method from other classes that are not derived from the class where the method is defined.
93+
CALL_PRIVATE_METHOD, // Calling a private method from external places. E.g. calling an `_`-prefixed method from other classes that are not derived from the class where the method is defined.
9494
#ifndef DISABLE_DEPRECATED
9595
PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.
9696
CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.
@@ -149,7 +149,7 @@ class GDScriptWarning {
149149
ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.
150150
ERROR, // ONREADY_WITH_EXPORT // May not work as expected.
151151
WARN, // ACCESS_PRIVATE_MEMBER
152-
WARN, // CALLING_PRIVATE_METHOD
152+
WARN, // CALL_PRIVATE_METHOD
153153
#ifndef DISABLE_DEPRECATED
154154
WARN, // PROPERTY_USED_AS_FUNCTION
155155
WARN, // CONSTANT_USED_AS_FUNCTION

modules/gdscript/tests/scripts/analyzer/features/virtual_method_implemented.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class SuperMethodsRecognized extends BaseClass:
1515
return result
1616

1717
func test():
18+
@warning_ignore_start("call_private_method")
1819
var test1 = SuperClassMethodsRecognized.new()
1920
print(test1._get_property_list()) # Calls base class's method.
2021
var test2 = SuperMethodsRecognized.new()

modules/gdscript/tests/scripts/analyzer/features/warning_ignore_warnings.gd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,30 @@ func test_unsafe_void_return() -> void:
151151
func get_class():
152152
pass
153153

154+
class PrivA:
155+
var _t = 1
156+
157+
func _priv_method():
158+
_t = 5
159+
160+
class PrivB:
161+
var a = PrivA.new()
162+
163+
func _foo():
164+
@warning_ignore("access_private_member")
165+
a._t = 2
166+
@warning_ignore("call_private_method")
167+
a._priv_method()
168+
169+
class PrivC extends PrivA:
170+
var a = PrivA.new()
171+
172+
func _foo():
173+
@warning_ignore("access_private_member")
174+
a._t = 2
175+
@warning_ignore("call_private_method")
176+
a._priv_method()
177+
154178
# We don't want to execute it because of errors, just analyze.
155179
func test():
156180
pass
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
GDTEST_OK
1+
GDTEST_PARSER_ERROR
2+
Used space character for indentation instead of tab as used before in the file.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class A:
2+
var _t = 1
3+
4+
func _priv_method():
5+
_t = 5
6+
7+
class B:
8+
var a = A.new()
9+
10+
func _foo():
11+
a._t = 2
12+
a._priv_method()
13+
14+
class C extends A:
15+
var a = A.new()
16+
17+
func _foo():
18+
a._t = 2
19+
a._priv_method()
20+
21+
func test():
22+
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GDTEST_OK
2+
~~ WARNING at line 11: (ACCESS_PRIVATE_MEMBER) Trying to access a private member "_t" from an external place, which would cause problems during runtime.
3+
~~ WARNING at line 12: (CALL_PRIVATE_METHOD) Trying to call a private method "_priv_method()" from an external place, which would cause problems during runtime.

modules/gdscript/tests/scripts/runtime/features/onready_base_before_subclass.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ class B extends A:
1414

1515
func test():
1616
var node := B.new()
17+
@warning_ignore("call_private_method")
1718
node._ready()
1819
node.free()

0 commit comments

Comments
 (0)