godotengine / godotengine/godot
Cannot infer type when using unknown field
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
Godot Version: v4.3.stable.official [77dcf97d8]
### System information
Godot v4.3.stable - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce RTX 3080 (NVIDIA; 31.0.15.5241) - 12th Gen Intel(R) Core(TM) i7-12700KF (20 Threads)
### Issue description
When using 'unknown' fields, the parser can't infer the type of variables.
While the type `a` is correctly inferred, the parser throws an error for `b` and `c`.
```gdscript
var a := event is InputEventMouseButton and (event as InputEventMouseButton).button_index == MOUSE_BUTTON_LEFT
var b := event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT
var c := event is InputEventMouseButton and event.get('button_index') == MOUSE_BUTTON_LEFT
```
For `b` it's reasonable that the type can't be inferred, since that expression throws an exception when the accessed field doesn't exist.
(for example with a typo: `event.button_indexx`)
But the parser should be able to infer the type of `c` since the expression is always `boolean`.
(regardless whether the value exists or what the value is)
### Steps to reproduce
1. Create `Area2D` scene
2. Add a rectangular `CollisionShape2D`
* optional: use a sprite to visualize it
3. Add a script to the scene
4. Add the `_input_event` function to the script
5. Add the variables to the function
```gdscript
var a := event is InputEventMouseButton and (event as InputEventMouseButton).button_index == MOUSE_BUTTON_LEFT
# type shouldn't be inferred (since it can cause a runtime exception)
var b := event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT
# type should be inferred
var c := event is InputEventMouseButton and event.get('button_index') == MOUSE_BUTTON_LEFT
```
7. Currently only the type of `a` is inferred
### Minimal reproduction project (MRP)
[infer-test.zip](https://github.com/user-attachments/files/17610518/infer-test.zip)
Contributor guide
Research direction
Start with the attached infer-test.zip minimal reproduction and run the three GDScript declarations in the issue. Trace the parser's type inference for the `event.get('button_index')` expression, comparing it with the `is` and direct-field cases. Done means `c` is inferred as boolean while the expected behavior for `b` remains unchanged, with regression coverage added where the existing parser tests belong.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers, game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100