godotengine / godotengine/godot

Incorrect parsing error 'Cannot use subscript operator on a base of type "null"'

Open
#98,152 1 comment 0 reactions 0 assignees View on GitHub
bug topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Reproducible in: v4.3.stable.mono.official [77dcf97d8]

### System information

Windows 10.0.19045 - GLES3 (Compatibility) - AMD Radeon RX 7800 XT (Advanced Micro Devices, Inc.; 31.0.24019.1006) - AMD Ryzen 7 1700 Eight-Core Processor (16 Threads)

### Issue description

Godot is in some circumstances incorrectly determining that a value is "null" at parsing time.
It seems to be related to using higher-order functions in conditions - Godot seems to be assuming that the function returned by the high-order function always returns `false`, though that doesn't entirely explain it.
When this occurs, the project refuses to run, (`'Cannot use subscript operator on a base of type "null"'`) though the code is correct.

My full source code is included in "Steps to reproduce"

As an aside, I'm using the GDscript language server, and you can also see the error within VS code
![image](https://github.com/user-attachments/assets/68001e71-bc4b-46fe-845e-32680ddb8309)

### Steps to reproduce

Created a new project, created a single Node2D, added a script to it.
Script is below:

```gdscript
extends Node2D

func returns_callable():
var f = func(): return true
return f

func uses_returns_callable():
if returns_callable().call(): # Always true
return [1, 2, 3]
return null

func _ready():
var value = uses_returns_callable()
print(value) # prints [1, 2, 3]

# Though the following line should work, if uncommented it results in an error during parsing
# 'Cannot use subscript operator on a base of type "null".gdscript(-1)'
# if value: print(value[0])

# Curiously, if I actually replace 'var value = uses_returns_callable()` with `var value = null`
# no parsing error is produced
```

### Minimal reproduction project (MRP)

[minimal-parser-bug-example.zip](https://github.com/user-attachments/files/17357093/minimal-parser-bug-example.zip)

Contributor guide

Open the contributing guide

Research direction

Start with the minimal-parser-bug-example.zip reproduction and run the provided GDScript, focusing on returns_callable(), uses_returns_callable(), and the conditional subscript in _ready(). Trace why the parser reports a null base for the higher-order function result; done means the project runs without that diagnostic while preserving the demonstrated output.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.