godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript: Adjust `STATIC_CALLED_ON_INSTANCE` warning to not force native type

Open 20kdc opened this issue 6 months ago • 1 comments

Defaulting to the native type is less than useful, as:

  • There are very few native types that are extensible and have static methods.
  • Defaulting to the native type does not account for a method being script-defined.

While the "real fix" would be to carefully track the source of the method, the get_function_signature method is already complicated enough.

This will at least ensure the resulting code should always be valid.

This PR was written after seeing the following warning:

*  W 0:00:00:0542   The function "get_absolute_z_index()" is a static function but was called from an instance. Instead, it should be directly called from the type: "Node2D.get_absolute_z_index()".

Where get_absolute_z_index() was a static function declared in a script (and thus did not exist in Node2D, and thus the given code did not work)

  • Bugsquad edit: Fixes #74397.

20kdc avatar Dec 08 '23 12:12 20kdc

Note that for non-global classes the warning will suggest invalid code.

dalexeev avatar Feb 08 '24 13:02 dalexeev

Note that for non-global classes the warning will suggest invalid code.

Example, please? I'm not sure what constitutes a "non-global class" in this case.

20kdc avatar Feb 09 '24 12:02 20kdc

Example, please? I'm not sure what constitutes a "non-global class" in this case.

dalexeev avatar Feb 09 '24 12:02 dalexeev

Note that this would be wrong on the current master also, it would show EditorScript.static_func() which isn't a valid way to call it.

We can either accept this as small improvement or work on a proper fix that finds the origin of method.

PS: I wonder if we should even emit this warning with a direct call for a function in the same class. If it does not have class_name it is quite cumbersome to avoid this warning.

vnen avatar Feb 09 '24 12:02 vnen

I've amended my commit on the basis of that other languages don't warn for these kinds of self-calls to static functions.

There are more examples and counter-examples one could add involving self and var x := self and so forth, but they seem contrived...

20kdc avatar Feb 09 '24 15:02 20kdc

Thanks!

akien-mga avatar Mar 04 '24 13:03 akien-mga