Skip to content

Allow local class creation in early construction context#1089

Closed
mcimadamore wants to merge 1 commit into
openjdk:code-reflectionfrom
mcimadamore:allow_early_local_constr
Closed

Allow local class creation in early construction context#1089
mcimadamore wants to merge 1 commit into
openjdk:code-reflectionfrom
mcimadamore:allow_early_local_constr

Conversation

@mcimadamore

@mcimadamore mcimadamore commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Some time ago, I added a restriction to reject local class creation in early context. The reason this restriction was added is that capture of "this" in local classes in early context work differently, in the sense that a local class in such context is allowed to capture an outer this, but not the current this.

Example:

class Outer {
    void m() { }
    
    class Inner {
        Inner() {
            class Foo {
                void t() { m(); }
            }
            new Foo();
	    super();
	}
    }
}

This is legal, as the local class needs Outer.this, not Inner.this.

But, the babylon compiler features several restriction when it comes to inner classes and constructors:

  1. code reflection in constructors is disabled
  2. reflectable program elements in inner/local classes are skipped

Now, (1) seems promising (after all, you only are in early construction context if you are... in a constructor). But, you can still have a reflectable lambda in a constructor:

class Outer {
    void m() { }
    
    class Inner {
        Inner() {
            class Foo {
                void t() { m(); }
            }
            @Reflect
            Runnable r = () -> new Foo();
	    super();
	}
    }
}

Except -- this still fails, beause of (2) -- the reflectable lambda is inside an inner class (Inner).

For these reasons, I don't think it's possible to ever trigger a case where strange outer this capturing would be relevant. For this to happen the local class has to be inside a constructor of an inner class. And, inside that constructor we can't, by definition, have reflectable elements.

The equivalent example w/o inner classes:

class Outer {
    Outer() {
        class Foo {
            void t() { }
        }
        @Reflect
        Runnable r = () -> new Foo();
        super();
    }   
}

Is still rejected by the current AST check, but in fact there's nothing problematic about this. So this PR re-allows this.

I've added a test to make sure this shape works correctly -- unfortunately I couldn't rely on the @IR test harness (as that requires the reflectable element to be inside a reflectable method, which is not the case here), so I have hand-rolled a new test.



Progress

  • Change must not contain extraneous whitespace

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/1089/head:pull/1089
$ git checkout pull/1089

Update a local copy of the PR:
$ git checkout pull/1089
$ git pull https://git.openjdk.org/babylon.git pull/1089/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 1089

View PR using the GUI difftool:
$ git pr show -t 1089

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/1089.diff

Using Webrev

Link to Webrev Comment

@mcimadamore

Copy link
Copy Markdown
Collaborator Author

/template append

@bridgekeeper

bridgekeeper Bot commented Jul 7, 2026

Copy link
Copy Markdown

👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into code-reflection will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Jul 7, 2026

Copy link
Copy Markdown

@mcimadamore This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

Allow local class creation in early construction context

Reviewed-by: psandoz

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 1 new commit pushed to the code-reflection branch:

Please see this link for an up-to-date comparison between the source branch of this pull request and the code-reflection branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the code-reflection branch, type /integrate in a new comment.

@openjdk

openjdk Bot commented Jul 7, 2026

Copy link
Copy Markdown

@mcimadamore The pull request template has been appended to the pull request body

@openjdk openjdk Bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jul 7, 2026
@mlbridge

mlbridge Bot commented Jul 7, 2026

Copy link
Copy Markdown

Webrevs

@PaulSandoz PaulSandoz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good. This expands the use of where records can be used in reflectable lambdas.

@mcimadamore

Copy link
Copy Markdown
Collaborator Author

/integrate

@openjdk

openjdk Bot commented Jul 7, 2026

Copy link
Copy Markdown

Going to push as commit 835ba45.
Since your change was applied there has been 1 commit pushed to the code-reflection branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Jul 7, 2026
@openjdk openjdk Bot closed this Jul 7, 2026
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jul 7, 2026
@openjdk

openjdk Bot commented Jul 7, 2026

Copy link
Copy Markdown

@mcimadamore Pushed as commit 835ba45.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

2 participants