RFR: 8308637: AssertionError when using Trees.getScope in plug-in

Jan Lahoda jlahoda at openjdk.org
Mon Feb 16 10:57:25 UTC 2026


The Trees API has a method to get information about elements available at a given place in the source code: `Trees.getScope(TreePath)`. It works by duplicating a piece of the AST, passing it to `Attr`, and stopping the attribution at the point specified by the `TreePath`, returning the current `Env` at that point.

For code like:

class B {
    public class Test {}
    void m() {
        new Test() {};
        this.new Test() {};
    }
}


when the TreePath is pointing at `Test` in `this.new Test`, this does not work very well for two reasons:
- when attributing the NewClassTree with enclosing expression, `Attr` is not really attributing the `Test` tree, but it synthetizes a temporary qualified name and attributes this synthetized name. The attribution then does not stop properly. This can be fixed by adding a check to stop the attribution at the appropriate place.
- if the enclosing classes are not-yet attributed, attributing the anonymous body for `new Test() {};` will trigger attribution of `Test`, which will trigger an attribution of `B`, and that will try to re-attribute the original method `m`. This is attributing the conceptually same method (with same local classes) twice, which leads to problems downstream. The proposal is to avoid superclass and enclosing class attribution when evaluating `Scope`.

-------------

Commit messages:
 - Updating copyright years,
 - A better place for the checkBreaktTree
 - 8308637: AssertionError when using Trees.getScope in plug-in

Changes: https://git.openjdk.org/jdk/pull/29739/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=29739&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8308637
  Stats: 127 lines in 2 files changed: 116 ins; 5 del; 6 mod
  Patch: https://git.openjdk.org/jdk/pull/29739.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/29739/head:pull/29739

PR: https://git.openjdk.org/jdk/pull/29739


More information about the compiler-dev mailing list