Magical type deduction deduces type of function result stored in variable not declared with static type, and enables autocompletion on direct members, but not indirect members (members of members) #78003
Labels
Milestone
Godot version
v4.0.2.stable.official [7a0977c]
System information
Linux Ubuntu 22.04 with Unity desktop
Issue description
I found a super edge case for autocompletion breaking, where autocompletion will work on a variable not declared with static type, but only for 1st-degree members. I don't know if the bug if that the parser goes too far (it shouldn't even try to guess non-static variable type), or not far enough (if it does, it might as well autocomplete the 2nd member).
I suppose that the "magical" unsollicited type deduction on non-static-type variables has been added because it's convenient in every day use like doing math computation or trivial calls without needing a
:=
instead of=
every single time, but it causes inconsistent behavior when dealing with 2nd-degree members; and may confuse the user as autocompletion works on the 1st level so they don't immediately realize that they forgot to static type with:=
.So it'd be better if either autocompletion was working all along, or not at all (then user could immediately spot they forgot to static type).
Take a custom class:
and
and a script that uses the class, defining some methods, typed or not:
Now we add a final method to this script to test autocompletion.
Note that behavior slightly differs between single vars and arrays: it is the same with single vars whether you use a typed function result or not (and I don't understand how; maybe untyped function is simpler to magic-parse so they can deduce the return type anyway). Whereas a function returning an array but without explicit type will not autocomplete at all when using the result (unless you locally
In addition, adding code using the members above in _ready, such as
will hint the parser and enable autocompletion on indirect members. So when reproducing the bug, you must avoid adding extra code that use the custom members to avoid hiding the bug.
The MRP has even more cases, I just put the unexpected ones (the one where the magical type deduction stops working after 2 members) here.
Workaround
The bug occurs where user forgot to statically type, so fortunately a simple
:=
generally does the trick. If the function does not return a static type, you can even explicitly declare type with: MyType =
.However, fixing the bug by either not completing at all, or completing all the way down, would help the user avoid being lost.
Steps to reproduce
custom_field
,get_parent
, etc.)Minimal reproduction project
v4.0.2 - Autocompletion doesnt work for 2nd degree member (member of custom class).zip
The text was updated successfully, but these errors were encountered: