Skip to content

Conversation

@archiecobbs
Copy link
Contributor

@archiecobbs archiecobbs commented Sep 21, 2022

(Description revised)

JDK-8043176 updated the JLS 16.1.10 to describe how within a lambda body, any variables that were DU before the lambda expression must not be considered DU at the start of the lambda block, because arbitrary code could have executed in the meantime:

No rule allows V to be definitely unassigned before a lambda body. This is by design: a variable that was definitely unassigned before the lambda body may end up being assigned to later on, so we cannot conclude that the variable will be unassigned when the body is executed.

This fix to Flow.AssignAnalyzer follows in a straightforward way: mark all variables as not DU before recursing into the lambda body.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8043179: Lambda expression can mutate final field

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10381

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10381.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 21, 2022

👋 Welcome back archiecobbs! A progress list of the required criteria for merging this PR into master 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 added the rfr Pull request is ready for review label Sep 21, 2022
@openjdk
Copy link

openjdk bot commented Sep 21, 2022

@archiecobbs The following label will be automatically applied to this pull request:

  • compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label Sep 21, 2022
@mlbridge
Copy link

mlbridge bot commented Sep 21, 2022

@archiecobbs
Copy link
Contributor Author

I've updated this pull request to include a new unit test (apologies for omitting it previously).

Also there are a couple of changes to other unit tests which get broken because this change reflects changes to the JLS spec per JDK-8043176. I'm a little unclear on precisely what the new behavior is supposed to be, but in any case the actual behavior now makes a lot more sense: basically, everything that happens in a lambda is assumed to happen some indefinite time later in the future (same as if you were instantiating the equivalent local class), so the previous errors expected by T8024809/SelfInitializerInLambdaTestb.java regarding instance field initialization are no longer errors.

In summary:

With the above changes now all javac tests should succeed.

@archiecobbs archiecobbs changed the title 8043179: Lambda expression can mutate final field. 8043179: Lambda expression can mutate final field Sep 24, 2022
@openjdk-notifier
Copy link

@archiecobbs Please do not rebase or force-push to an active PR as it invalidates existing review comments. All changes will be squashed into a single commit automatically when integrating. See OpenJDK Developers’ Guide for more information.

…les.

Before this change, compiling this class:

    class LambdaMutateFinalVar {
        LambdaMutateFinalVar() {
            final String x;
            Runnable r1 = () -> x = "not ok";
            x = "ok";
        }
    }

would report this error:

  local variables referenced from a lambda expression must be final or effectively final

That error is not really appropriate; after all, the variable IS final. The
real problem is that it can't be assigned from within the lambda because it
can't be assumed to be DU.

After this change, this error is reported instead:

  variable x might already have been assigned
@openjdk-notifier
Copy link

@archiecobbs Please do not rebase or force-push to an active PR as it invalidates existing review comments. All changes will be squashed into a single commit automatically when integrating. See OpenJDK Developers’ Guide for more information.

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 17, 2022

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 16, 2022

@archiecobbs This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Dec 16, 2022
@archiecobbs
Copy link
Contributor Author

/open

@openjdk openjdk bot reopened this Dec 16, 2022
@openjdk
Copy link

openjdk bot commented Dec 16, 2022

@archiecobbs This pull request is now open

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 16, 2023

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@archiecobbs
Copy link
Contributor Author

/open

@openjdk
Copy link

openjdk bot commented Jan 19, 2023

@archiecobbs This pull request is already open

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 16, 2023

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@archiecobbs
Copy link
Contributor Author

/keepalive

@openjdk
Copy link

openjdk bot commented Feb 17, 2023

@archiecobbs Unknown command keepalive - for a list of valid commands use /help.

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 17, 2023

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@archiecobbs
Copy link
Contributor Author

/pingbot

@openjdk
Copy link

openjdk bot commented Mar 17, 2023

@archiecobbs Unknown command pingbot - for a list of valid commands use /help.

// body. This is by design: a variable that was definitely unassigned before the
// lambda body may end up being assigned to later on, so we cannot conclude that
// the variable will be unassigned when the body is executed.
uninits.excludeFrom(firstadr);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will exclude all tracked variables not only fields, shouldn't we exclude fields only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This bug refers only to final fields, but JLS section 16 applies to both fields and variables, so I think this code is following the spec as written.

Here's an example of where this change would catch an illegal final variable assignment:

class LambdaMutateFinalVariable {
    void foo() {
        final String x;
        Runnable r1 = () -> x = "not ok";
        x = "ok";
    }
}

So I don't think the code in the PR is wrong. However, there is a subtlety: currently, the compiler already correctly handles the above example, but that's only by coincidence and thanks to the separate logic for "effectively final":

LambdaMutateFinalVariable.java:4: error: local variables referenced from a lambda expression must be final or effectively final
        Runnable r1 = () -> x = "not ok";
                            ^

With the change in this PR, the DA/DU analysis will instead get there first, and so you get a different error:

LambdaMutateFinalVariable.java:4: error: variable x might already have been assigned
        Runnable r1 = () -> x = "not ok";
                            ^

It seems to me either error is appropriate, because the code violates both rules. But I think the change in the error message is actually an improvement, because the current error is complaining about the variable not being final even though it actually is final, which is confusing.

Your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep I agree that the error message is better with the patch. My concern was not that the patch was wrong but about the fact that we were applying the change for all variables and fields, but yes it probably makes sense to go for this and have the side effect of better error messages

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

Choose a reason for hiding this comment

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

looks good

@openjdk
Copy link

openjdk bot commented Mar 21, 2023

@archiecobbs 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:

8043179: Lambda expression can mutate final field

Reviewed-by: vromero

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 34 new commits pushed to the master branch:

  • bf917ba: 8304687: Move add_to_hierarchy
  • 63d4afb: 8304671: javac regression: Compilation with --release 8 fails on underscore in enum identifiers
  • e2cfcfb: 6817009: Action.SELECTED_KEY not toggled when using key binding
  • af4d560: 8303951: Add asserts before record_method_not_compilable where possible
  • c433862: 6245410: javax.swing.text.html.CSS.Attribute: BACKGROUND_POSITION is not w3c spec compliant
  • 91f407d: 8029301: Confusing error message for array creation method reference
  • e73411a: 8304376: Rename t1/t2 classes in com/sun/jdi/CLETest.java to avoid class duplication error in IDE
  • a2d8f63: 8288730: Add type parameter to Lookup::accessClass and Lookup::ensureInitialized
  • 3777455: 8302191: Performance degradation for float/double modulo on Linux
  • 760c012: 8304683: Memory leak in WB_IsMethodCompatible
  • ... and 24 more: https://git.openjdk.org/jdk/compare/42723dcb1862da598092bb499056940d78a8bdac...master

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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@vicente-romero-oracle) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 21, 2023
@archiecobbs
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Mar 21, 2023
@openjdk
Copy link

openjdk bot commented Mar 21, 2023

@archiecobbs
Your change (at version 86808b2) is now ready to be sponsored by a Committer.

@openjdk openjdk bot removed the sponsor Pull request is ready to be sponsored label Mar 21, 2023
@vicente-romero-oracle
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Mar 23, 2023

@vicente-romero-oracle The PR has been updated since the change author (@archiecobbs) issued the integrate command - the author must perform this command again.

@archiecobbs
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Mar 23, 2023
@openjdk
Copy link

openjdk bot commented Mar 23, 2023

@archiecobbs
Your change (at version e1cd797) is now ready to be sponsored by a Committer.

@vicente-romero-oracle
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Mar 23, 2023

Going to push as commit c00d088.
Since your change was applied there have been 35 commits pushed to the master branch:

  • 147f347: 8219083: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java failed in same binary run on windows x64
  • bf917ba: 8304687: Move add_to_hierarchy
  • 63d4afb: 8304671: javac regression: Compilation with --release 8 fails on underscore in enum identifiers
  • e2cfcfb: 6817009: Action.SELECTED_KEY not toggled when using key binding
  • af4d560: 8303951: Add asserts before record_method_not_compilable where possible
  • c433862: 6245410: javax.swing.text.html.CSS.Attribute: BACKGROUND_POSITION is not w3c spec compliant
  • 91f407d: 8029301: Confusing error message for array creation method reference
  • e73411a: 8304376: Rename t1/t2 classes in com/sun/jdi/CLETest.java to avoid class duplication error in IDE
  • a2d8f63: 8288730: Add type parameter to Lookup::accessClass and Lookup::ensureInitialized
  • 3777455: 8302191: Performance degradation for float/double modulo on Linux
  • ... and 25 more: https://git.openjdk.org/jdk/compare/42723dcb1862da598092bb499056940d78a8bdac...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 23, 2023
@openjdk openjdk bot closed this Mar 23, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Mar 23, 2023
@openjdk
Copy link

openjdk bot commented Mar 23, 2023

@vicente-romero-oracle @archiecobbs Pushed as commit c00d088.

💡 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

compiler compiler-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

2 participants