godotengine / godotengine/godot

[GDScript] Conflict between Variable Name and Type in Unresolvable Scope

Open
#92,008 3 comments 0 reactions 0 assignees View on GitHub
bug discussion topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

v4.2.1.stable.official [b09f793f5]
v4.2.2.stable.official [15073afe3]

### System information

N/A

### Issue description

When declaring a variable with the syntax `var a: TypeName` followed by `var TypeName` in Godot Engine, an error is raised stating "Local variable 'TypeName' cannot be used as a type." This issue arises due to an unresolvable scope conflict where the engine cannot distinguish between `TypeName` as both a type and a variable.

Example Code:
```gdscript
func my_func():
var a: TypeName # error
var TypeName
```

Error Message:
```
Local variable "TypeName" cannot be used as a type.
```

The engine should either:
1. Allow the declaration without raising an error, correctly differentiating between the type and the variable name.
2. Provide a more informative error message that clearly explains the conflict and suggests using a different variable name.

Also,
```gdscript
var Variant
func my_func():
var a: Variant # no error
```
```gdscript
func my_func():
var Variant
var a: Variant # error
```
```gdscript
func my_func():
var a: Variant # error
var Variant
```

### Steps to reproduce

```gdscript
func my_func():
var a: Variant # error
var Variant
```

### Minimal reproduction project (MRP)

N/A

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported GDScript snippets, especially the local `Variant` and `TypeName` declarations. Trace the scope and type-resolution path involved in the diagnostic. Done means the conflict is either handled correctly or produces a clear, actionable error message, with regression coverage for the examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
godot
Domain
compilers, game-dev
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.