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

GDScript cannot infer the type of a variable initialized with a typed method call #68069

Closed
rblopes opened this issue Oct 31, 2022 · 10 comments · Fixed by #69471
Closed

GDScript cannot infer the type of a variable initialized with a typed method call #68069

rblopes opened this issue Oct 31, 2022 · 10 comments · Fixed by #69471

Comments

@rblopes
Copy link

rblopes commented Oct 31, 2022

Godot version

v4.0.beta3.official [01ae26d]

System information

KDE neon 22.04

Issue description

(Sorry in advance if the following bug was reported before, or is already fixed. I couldn't find anything related.)

(Edit: Added the MRP.)

It seems that the GDScript parser cannot infer the type of a typed user method ahead of its definition.

extends Node

var some_var := some_func()

# NOTE: It doesn't matter if you qualify the method as `static` or not.
static func some_func() -> int:
	return 42

The code above will trigger a parser error notification on both the script editor and the debug console, as follows:

  modules/gdscript/gdscript_analyzer.cpp:668 - Parser bug (please report): tried to assign unset node without an identifier.
  res://bug_2.gd:3 - Parse Error: Cannot infer the type of "some_var" variable because the initial value doesn't have a set type.
  modules/gdscript/gdscript_analyzer.cpp:668 - Parser bug (please report): tried to assign unset node without an identifier.
  modules/gdscript/gdscript_analyzer.cpp:668 - Parser bug (please report): tried to assign unset node without an identifier.

editor_screenshot_2022-10-30T221805

For comparison: that same code works on Godot 3.5.

Either declaring the variable after the method or replacing the type inferred assignment (:=) works fine.

Steps to reproduce

Creating a script with the following content on any project triggers the error:

extends Node

var some_var := some_func()

static func some_func() -> int:
	return 42

# func some_func() -> int:
#	return 42

Minimal reproduction project

68069.zip

@adamscott
Copy link
Member

I confirm the issue for Godot 4.0.

@rblopes
Copy link
Author

rblopes commented Dec 1, 2022

Still getting the same error messages in Godot 4 beta 7.

@adamscott
Copy link
Member

CC @rune-scape @anvilfolk

@anvilfolk
Copy link
Contributor

anvilfolk commented Dec 1, 2022

I'd be pretty excited to take a look at this one if no one else is. Recently did a little bit of work on initializers so it's a part of the code I'm not completely clueless on :)

@rune-scape
Copy link
Contributor

i'm sorry to do this to you again @anvilfolk, but my out of order member resolution branch fixes this

@anvilfolk
Copy link
Contributor

anvilfolk commented Dec 2, 2022

For a moment I thought this was for a PR that I had already submitted! It isn't, and no worries even if it was :) I started looking into it a little bit but didn't get super far. It makes a ton of sense that PR would fix it!

Curious how you did it! I'm guessing something like parsing non-inferred types or functions first, and inferred types later?

@rune-scape
Copy link
Contributor

rune-scape commented Dec 2, 2022

@anvilfolk i typed up an explanation and thought i sent it, but now its eaten by my browser :(, my PR has a bit if an explanation, and ill flesh it out a bit more :)

@anvilfolk
Copy link
Contributor

Sweet, thank you. It is mostly curiosity, and I'm trying to review the PR now so it gets more upvotes to get merged sooner rather than later! :)

@akien-mga akien-mga added this to the 4.0 milestone Dec 15, 2022
@Angular-Angel
Copy link

So I had this problem show up again for me, using the Godot_v4.0.2-stable_linux.x86_64 build. I loaded the FPS engine template and tried to run it, and got: var texture_folder := _devblock_color_to_foldername[block_color_group]. I was able to fix this easily enough by just changing it to var texture_folder : String = _devblock_color_to_foldername[block_color_group], but it still shows that this bug isn't entirely solved.

@adamscott
Copy link
Member

adamscott commented Apr 14, 2023

So I had this problem show up again for me, using the Godot_v4.0.2-stable_linux.x86_64 build. I loaded the FPS engine template and tried to run it, and got: var texture_folder := _devblock_color_to_foldername[block_color_group]. I was able to fix this easily enough by just changing it to var texture_folder : String = _devblock_color_to_foldername[block_color_group], but it still shows that this bug isn't entirely solved.

What is the type of _devblock_color_to_foldername?

And just to note, the bug is about a typed method call. In your example, there's no method to be seen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants