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

Statically incorrect type annotations are permitted on for loop variables #82021

Closed
PatchMixolydic opened this issue Sep 21, 2023 · 1 comment · Fixed by #82030
Closed

Statically incorrect type annotations are permitted on for loop variables #82021

PatchMixolydic opened this issue Sep 21, 2023 · 1 comment · Fixed by #82030

Comments

@PatchMixolydic
Copy link

PatchMixolydic commented Sep 21, 2023

Godot version

v4.2.dev5.official [e3e2528], v4.2.dev.custom_build [7b49cbade]

System information

Godot v4.2.dev5 - Debian GNU/Linux 12 (bookworm) 12 - X11 - Vulkan (Mobile) - dedicated NVIDIA GeForce GTX 1060 3GB (nvidia) - AMD Ryzen 7 1700X Eight-Core Processor (16 Threads)

Issue description

Type annotations that are obviously incorrect are accepted on for loop variables, leading to runtime errors (specifically, "Trying to assign a value of type 'int' to a variable of type 'String'"). The implementation PR, #80247, notes that cases where the runtime type is different will cause runtime errors. However, this issue occurs even when the types should be statically known.

Steps to reproduce

  1. Create a project, populate it with a Node3D, and save the scene.
  2. Attach the following script to the Node3D:
extends Node3D

func _ready() -> void:
	for x: String in [1, 2, 3]:
		print(x)
  1. Note that the script passes typecheck.
  2. Run the current project, setting the current scene as the default scene if prompted.
  3. Note that a runtime error occurs.

Extra info

Compare the script above to the following script:

extends Node3D

func _ready() -> void:
	var x: Array[String] = [1, 2, 3]

This correctly produces an error at compile time:

Line 4: Cannot include a value of type "int" as "String".
Line 4: Cannot have an element of type "int" in an array of type "Array[String]".

Minimal reproduction project

godot issue.zip

@dalexeev
Copy link
Member

for x: String in [1, 2, 3]:

I'm not sure if this is a bug. By default, array literals are untyped (Array aka Array[Variant]), we make literals typed only in some contexts. The following code correctly generates an error:

var a: Array[int] = [1, 2, 3]
# Compile time error: Unable to iterate on value of type
# "Array[int]" with variable of type "String".
for x: String in a:
    print(x)
var x: Array[String] = [1, 2, 3]

This is one of the cases where we are implicitly converting a literal to a typed array. Maybe we should make the literal typed in this case too, but I'm not sure if that's a good idea.

This issue was closed.
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.

2 participants