Generator object body scope is surprising
- Dominant language
- Java
- Stars
- 11.5k
- Forks
- 402
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 20
Description
This snippet:
```pkl
qux = "outer"
foo {
when (true) {
local qux = "inner"
}
prop = qux
}
```
Produces:
```pkl
qux = "outer"
foo {
prop = "inner"
}
```
This is because generator object bodies are visible to the enclosing object.
Sometimes, names resolve either to a generator property, and sometimes, they resolve to an outer property. At parse time, we can't tell what these should resolve to. For example:
```pkl
qux = "outer"
foo {
when (true) {
qux = "inner"
}
prop = qux // resolves to inner qux
}
bar {
when (false) {
qux = "inner"
}
prop = qux // resolves to outer qux
}
```
It would be better if names declared inside of generator bodies are not visible from an enclosing object body.
Most code would still work if it resolved to implicit `this`.
We need to fix this before we can do https://github.com/apple/pkl/issues/1580
Contributor guide
Research direction
No files or tests are named. Start by reproducing both snippets and tracing name resolution for generator object bodies; compare the inner and outer `qux` cases. Done means declarations in generator bodies no longer leak into enclosing object bodies while the shown implicit-`this` behavior remains intact, allowing issue 1580 to proceed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100