eclipse-xtext / eclipse-xtext/xtext

Compiler picks wrong `it` to access an external property inside an anonymous class when mapping a list

Open
#2,329 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Description

class Test {
  static class Thing {
    var Object property
  }

  def test(List<Object> objects) {
    objects.map [
      new Thing => [
        property = new Object() {
          val oldValue = property // Java error
        }
      ]
    ]
  }
}

The 2.28.0.v20220829-0536 compiler produced invalid Java for this by selecting the wrong it for the access to property inside the anonymous object.

It is worth mentioning that the issue is only present when objects is a List and the transformation is a map, so somehow the logic breaks due to some difference between the implementations of map in IterableExtensions and ListExtensions.

The issue can be avoided by explicitly defining the lambda variable it for the arrow operator's lambda: => [it|...]

There is no linking error in either case.

May be related to eclipse/xtext-xtend#1301.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Test reproducer and inspect how the compiler resolves it for the nested lambdas. Compare the map paths in IterableExtensions and ListExtensions, which the issue identifies as differing. Done means the List/map case selects the external property correctly and produces valid Java without requiring an explicit lambda variable.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.