Skip to content

8314226: Series of colon-style fallthrough switch cases with guards compiled incorrectly #15532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from

Conversation

biboudis
Copy link
Member

@biboudis biboudis commented Sep 1, 2023

The switch structure is translated in handleSwitch where we rewrite pattern matching switches. In some occasions a switch has multiple cases with multiple patterns where the n-th case does not complete normally and falls through to the n+1-st case:

switch (obj) {
   case Integer _ when ((Integer) obj) > 0:
   case String _ when !((String) obj).isEmpty():
      return 1;
   default:
      return -1;
}

This PR addresses that by translating the second case correctly and also replicates the body of the latter to the former (which we can do because no bindings are introduced in either statement blocks by the definition of unnamed variables.).

Previously the code was translated into:

switch (java.lang.runtime.SwitchBootstraps.typeSwitch(selector0$temp, index$1)) {
    case 0:
        Integer _;
        if (!(true && ((Integer)obj) > 0)) {
            index$1 = 1;
            continue;
        }

    case 1 when !((String)obj).isEmpty():
        return 1;

    default:
        return -1;
}

This PR adjusts the translation into:

switch (java.lang.runtime.SwitchBootstraps.typeSwitch(selector0$temp, index$1)) {
case 0:
    Integer _;
    if (!(true && ((Integer)obj) > 0)) {
        index$1 = 1;
        continue;
    }
    return 1;

case 1:
    String _;
    if (!((selector0$temp instanceof String || false) && (true && !((String)obj).isEmpty()))) {
        index$1 = 2;
        continue;
    }
    return 1;
}

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-8314226: Series of colon-style fallthrough switch cases with guards compiled incorrectly (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15532

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 1, 2023

👋 Welcome back abimpoudis! 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
Copy link

openjdk bot commented Sep 1, 2023

@biboudis 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 1, 2023
@biboudis biboudis marked this pull request as ready for review September 1, 2023 10:23
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 1, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 1, 2023

@mcimadamore
Copy link
Contributor

So, as we discussed offline, type tests are normally enforced in the switch bootstrap. The first time we hit the test, we know we have some object and that the first label expects an Integer, so we test for that. But if the when clause fails, javac tells us to try again from the second label. Now, while the proposed code to the generated code looks ok - I wonder if this condition shouldn't instead be detected by the switch bootstrap: e.g. we're asking it to start again from a case which expects a String, but we really have some other Object on our hands, so that label index should never be returned by the switch bootstrap, which should either pick another applicable label index (if it exists) or just pick the default.

This would allow us to keep the generated code as is, and avoid introducing coupling between cases in the generated code.

@mcimadamore
Copy link
Contributor

So, as we discussed offline, type tests are normally enforced in the switch bootstrap. The first time we hit the test, we know we have some object and that the first label expects an Integer, so we test for that. But if the when clause fails, javac tells us to try again from the second label. Now, while the proposed code to the generated code looks ok - I wonder if this condition shouldn't instead be detected by the switch bootstrap: e.g. we're asking it to start again from a case which expects a String, but we really have some other Object on our hands, so that label index should never be returned by the switch bootstrap, which should either pick another applicable label index (if it exists) or just pick the default.

This would allow us to keep the generated code as is, and avoid introducing coupling between cases in the generated code.

Btw, looking at the comment in SwitchBootstraps::createRepeatIndexSwitch, it seems that's already the case:

/*
     * Construct test chains for labels inside switch, to handle switch repeats:
     * switch (idx) {
     *     case 0 -> if (selector matches label[0]) return 0; else if (selector matches label[1]) return 1; else ...
     *     case 1 -> if (selector matches label[1]) return 1; else ...
     *     ...
     * }
     */

@mcimadamore
Copy link
Contributor

So, as we discussed offline, type tests are normally enforced in the switch bootstrap. The first time we hit the test, we know we have some object and that the first label expects an Integer, so we test for that. But if the when clause fails, javac tells us to try again from the second label. Now, while the proposed code to the generated code looks ok - I wonder if this condition shouldn't instead be detected by the switch bootstrap: e.g. we're asking it to start again from a case which expects a String, but we really have some other Object on our hands, so that label index should never be returned by the switch bootstrap, which should either pick another applicable label index (if it exists) or just pick the default.
This would allow us to keep the generated code as is, and avoid introducing coupling between cases in the generated code.

Btw, looking at the comment in SwitchBootstraps::createRepeatIndexSwitch, it seems that's already the case:

/*
     * Construct test chains for labels inside switch, to handle switch repeats:
     * switch (idx) {
     *     case 0 -> if (selector matches label[0]) return 0; else if (selector matches label[1]) return 1; else ...
     *     case 1 -> if (selector matches label[1]) return 1; else ...
     *     ...
     * }
     */

Also, in the original test here:
https://bugs.openjdk.org/browse/JDK-8314226

It seems to me that the problem is slightly different - e.g. the client is passing a String, but the when clause doesn't seem to be applied by javac. Which seems to conflict with the original generated code you report above: if javac currently generates this:

 case 1 when !((String)obj).isEmpty():
        return 1;

How can javac go ahead with this case, considering that the provided string (in the original test) is empty?

I wonder if the real issue is that there is no real when guard emitted for the second case (because of javac getting confused with cascading).

@mcimadamore
Copy link
Contributor

mcimadamore commented Sep 1, 2023

I wonder if the real issue is that there is no real when guard emitted for the second case (because of javac getting confused with cascading).

To be clear, IMHO the generated code should be:

switch (java.lang.runtime.SwitchBootstraps.typeSwitch(selector0$temp, index$1)) {
case 0:
    Integer _;
    if (!(true && ((Integer)obj) > 0)) {
        index$1 = 1;
        continue;
    }
    return 1;

case 1:
    String _;
    if (!(true && !((String)obj).isEmpty())) {
        index$1 = 2;
        continue;
    }
    return 1;
}

@biboudis
Copy link
Member Author

biboudis commented Sep 1, 2023

I think you are right @mcimadamore. We do not need to generate the extraneous condition in the if. Removed and updated the PR.

Copy link
Contributor

@mcimadamore mcimadamore 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 Sep 1, 2023

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

8314226: Series of colon-style fallthrough switch cases with guards compiled incorrectly

Reviewed-by: mcimadamore, 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 160 new commits pushed to the master branch:

  • a731a24: 8315934: RISC-V: Disable conservative fences per vendor
  • b3dad24: 8316021: Serial: Remove unused Generation::post_compact
  • f9ab115: 8316050: Use hexadecimal encoding in MemorySegment::toString
  • f804f86: 8314612: TestUnorderedReduction.java fails with -XX:MaxVectorSize=32 and -XX:+AlignVector
  • f8df754: 8311207: Cleanup for Optimization for UUID.toString
  • fecd2fd: 8315898: Open source swing JMenu tests
  • bb6b3f2: 8315761: Open source few swing JList and JMenuBar tests
  • 2d168c5: 8313202: MutexLocker should disallow null Mutexes
  • 36552e7: 8316123: ProblemList serviceability/dcmd/gc/RunFinalizationTest.java on AIX
  • fe5ef5f: 8315677: Open source few swing JFileChooser and other tests
  • ... and 150 more: https://git.openjdk.org/jdk/compare/145d8bc1a3ef4f4fe0c10385be111838e7ed9250...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 (@mcimadamore, @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 Sep 1, 2023
@biboudis
Copy link
Member Author

biboudis commented Sep 4, 2023

/integrate

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

openjdk bot commented Sep 4, 2023

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

@lahodaj
Copy link
Contributor

lahodaj commented Sep 7, 2023

I think I'd like to ask for some more tests. Especially:

  • multiple cases with statements where the fall through goes from one to the other, e.g. something like:
   case Integer _ when ((Integer) obj) > 0:
   case String _ when !((String) obj).isEmpty():
       System.err.println(1);
   case null:
       System.err.println(2);
   default:    
       System.err.println(3);
  • cases with loop statements with break (and/or continue) to the loop. E.g.:
while (true) {break;}

instead of System.err.println(2);

  • break/continue, return, yield(?) going outside of the switch (probably one of them would be enough) in the duplicated section.

@biboudis
Copy link
Member Author

biboudis commented Sep 8, 2023

Thank you Jan. Does multipleCasesWithReturn cover your third bullet? Did I understood correctly?

@openjdk openjdk bot removed the sponsor Pull request is ready to be sponsored label Sep 8, 2023
@biboudis
Copy link
Member Author

biboudis commented Sep 8, 2023

Thank you @lahodaj for the improvement. This is extremely elegant and reduces the code size!!!

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 sensible

@vicente-romero-oracle
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Sep 13, 2023

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

@biboudis
Copy link
Member Author

/integrate

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

openjdk bot commented Sep 13, 2023

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

@vicente-romero-oracle
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Sep 13, 2023

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

  • ff240a9: 8316087: Test SignedLoggerFinderTest.java is still failing
  • a731a24: 8315934: RISC-V: Disable conservative fences per vendor
  • b3dad24: 8316021: Serial: Remove unused Generation::post_compact
  • f9ab115: 8316050: Use hexadecimal encoding in MemorySegment::toString
  • f804f86: 8314612: TestUnorderedReduction.java fails with -XX:MaxVectorSize=32 and -XX:+AlignVector
  • f8df754: 8311207: Cleanup for Optimization for UUID.toString
  • fecd2fd: 8315898: Open source swing JMenu tests
  • bb6b3f2: 8315761: Open source few swing JList and JMenuBar tests
  • 2d168c5: 8313202: MutexLocker should disallow null Mutexes
  • 36552e7: 8316123: ProblemList serviceability/dcmd/gc/RunFinalizationTest.java on AIX
  • ... and 151 more: https://git.openjdk.org/jdk/compare/145d8bc1a3ef4f4fe0c10385be111838e7ed9250...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 13, 2023
@openjdk openjdk bot closed this Sep 13, 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 Sep 13, 2023
@openjdk
Copy link

openjdk bot commented Sep 13, 2023

@vicente-romero-oracle @biboudis Pushed as commit 3b0a6d2.

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

@biboudis
Copy link
Member Author

/backport jdk21u

@openjdk
Copy link

openjdk bot commented Sep 15, 2023

@biboudis the backport was successfully created on the branch biboudis-backport-3b0a6d2a in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 3b0a6d2a from the openjdk/jdk repository.

The commit being backported was authored by Aggelos Biboudis on 13 Sep 2023 and was reviewed by Maurizio Cimadamore and Vicente Romero.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git biboudis-backport-3b0a6d2a:biboudis-backport-3b0a6d2a
$ git checkout biboudis-backport-3b0a6d2a
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git biboudis-backport-3b0a6d2a

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.

4 participants