godotengine / godotengine/godot

Typed Dictionary cannot use subclass of type in key with Invalid index type for a base type

Open
#101,925 6 comments 4 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

v4.4.beta1.official [d33da79d3]

### System information

Godot v4.4.beta1 - Windows 10 (build 19045) - Multi-window, 3 monitors - Vulkan (Forward+) - dedicated GeForce GTX 1050 - Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz (12 threads)

### Issue description

A typed Dictionary with index of type X cannot use a variable of type Y (that extends X) in keys.

### Steps to reproduce

1) Create a base class
2) Create another class that extends first class
3) Create a typed dictionary. Use the base class as the key type
4) Create a variable of the subtype
5) attempt to use that variable as a key in dictionary

Expected: can use any subclass of index type as a key

What happens: Parser Error: Invalid index type "(subclass name)" for a base of type "Dictionary[(base type), String]"

### Minimal reproduction project (MRP)

[mpr-typed-dictionary-subclass-index.zip](https://github.com/user-attachments/files/18509677/mpr-typed-dictionary-subclass-index.zip)

Contributor guide

Open the contributing guide

Research direction

Start by opening the attached minimal reproduction project and reproduce the parser error with a typed Dictionary whose key type is a base class and whose key value is a subclass. Trace the type-checking path for Dictionary indexing; done means the reproduction accepts the subclass key while preserving rejection of incompatible key types.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.