godotengine / godotengine/godot

Member variables are not visible in lambda with explicit type

Open
#112,183 2 comments 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: 4.5.1, 4.6-dev2

### System information

Godot v4.6.dev (07f4c0660) - Ubuntu 24.04.2 LTS 24.04 on X11 - X11 display driver, Multi-window, 1 monitor - OpenGL 3 (Compatibility) - Mesa Intel(R) HD Graphics 4600 (HSW GT2) - Intel(R) Core(TM) i5-4690S CPU @ 3.20GHz (4 threads) - 31.21 GiB memory

### Issue description

When I want to use member variable in lambda from current context class.

If I declared explicit type declaration, analyzer reports `Identifier "b" not declared in the current scope`.

When I declared implicit type declaration, analyzer can find relevant variable.

```
extends Node2D
class_name Test

class AnotherClass:
var another_member_var

var member_var = '123'

func test():
var another_class := AnotherClass.new()
another_class.another_member_var.connect("ready", func():
print(member_var)
)
```

With explicit type declaration `:=`, error reports:

```
SCRIPT ERROR: Parse Error: Could not resolve class "Test".
at: GDScript::reload (res://new_script.gd:1)
ERROR: Failed to load script "res://new_script.gd" with error "Parse error".
at: load (modules/gdscript/gdscript.cpp:3037)
```

With implicit type declaration `=` (no comma), no error reports.

https://github.com/user-attachments/assets/5b55402c-50f3-4834-a67c-27fc48a55b06

### Steps to reproduce

1. We have two classes, Test and AnotherClass.
2. In Test class, define a member variable - `member_var`
3. In Test class, define a local variable for AnotherClass with explicit type declaration
4. In Test class, write a lambda expression on AnotherClass variable and print `member_var` internal
5. When we want to load Test class, the error will occur.

### Minimal reproduction project (MRP)

[member-in-lambda.zip](https://github.com/user-attachments/files/23225589/member-in-lambda.zip)

Contributor guide

Open the contributing guide

Research direction

Start with the linked minimal reproduction project and reproduce the parse error using the explicit `:=` declaration. Then inspect the GDScript reload path in `modules/gdscript/gdscript.cpp` around line 3037 and the analyzer handling for lambda member-variable scope. Done means the explicit declaration loads without the unresolved-class or undeclared-identifier errors while the reproduction still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, godot
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.