Skip to content
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

8318144: Match on enum constants with body compiles but fails with MatchException #16499

Closed
wants to merge 3 commits into from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Nov 3, 2023

For code like:

enum E {A {}, B {} }
Object o = E.A;
switch (o) {
     case E.A -> System.err.println(o);
     default -> System.err.println("default");
}

The result is default, not A, due to incorrect classes being compared. Thanks for @liach for noting the solution here:
#16489 (comment)


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-8318144: Match on enum constants with body compiles but fails with MatchException (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 16499

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 3, 2023

👋 Welcome back jlahoda! 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 Nov 3, 2023
@openjdk
Copy link

openjdk bot commented Nov 3, 2023

@lahodaj The following labels will be automatically applied to this pull request:

  • compiler
  • core-libs

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

@openjdk openjdk bot added core-libs core-libs-dev@openjdk.org compiler compiler-dev@openjdk.org labels Nov 3, 2023
@mlbridge
Copy link

mlbridge bot commented Nov 3, 2023

Webrevs

@@ -396,7 +396,8 @@ private static boolean enumEqCheck(Object value, EnumDesc<?> label, MethodHandle
try {
Class<?> clazz = label.constantType().resolveConstantDesc(lookup);

if (value.getClass() != clazz) {
if (!(value instanceof Enum<?> enumValue) ||
Copy link
Member

Choose a reason for hiding this comment

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

Minor comment: If we are more concerned about performance when the enum is in a mix-and-match with other types (say primitives etc.), we can move the instanceof test before the constantType().resolveConstantDesc() to avoid a Class lookup.

@@ -394,9 +394,13 @@ private static boolean enumEqCheck(Object value, EnumDesc<?> label, MethodHandle
Object resolved;

try {
if (!(value instanceof Enum<?> enumValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this code seems to be here with the only purpose of casting value to Enum<?>, if this is the case shouldn't this intention be more explicit?

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 method is also used in the typeSwitch (i.e. ordinary pattern matching switch, which may contain qualified enum constants intermixed with patterns), and so the value may be any object. If it is not an Enum, we can avoid the potential costly resolution.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see shouldn't we have a test that stresses this code? I mean that we are returning false for cases that are not enums?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought there's quite a few such tests, but turns out there's just a few (or maybe only one). So adding some more: cbc9cb5. Thanks!

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.

lgtm, thanks!

@openjdk
Copy link

openjdk bot commented Nov 6, 2023

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

8318144: Match on enum constants with body compiles but fails with MatchException

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

  • e4803e0: 8318580: "javax/swing/MultiMonitor/MultimonVImage.java failing with Error. Can't find library: /open/test/jdk/java/awt/regtesthelpers" after JDK-8316053
  • cdf3373: 8319316: Clarify text around which layouts a linker supports
  • 1696603: 8308453: Convert JKS test keystores in test/jdk/javax/net/ssl/etc to PKCS12
  • b3126b6: 8319455: Test compiler/print/CompileCommandMemLimit.java times out
  • 1c2ea1d: 8319153: Fix: Class is a raw type in ProcessTools
  • 96e6e67: 4365952: Cannot disable JFileChooser
  • 2d4bbf4: 8319465: Typos in javadoc of com.sun.management.OperatingSystemMXBean methods
  • 8fb94fd: 8319379: G1: gc/logging/TestUnifiedLoggingSwitchStress.java crashes after JDK-8318894
  • b5c863b: 8316533: C2 compilation fails with assert(verify(phase)) failed: missing Value() optimization
  • 377138c: 8318959: C2: define MachNode::fill_new_machnode() statically
  • ... and 8 more: https://git.openjdk.org/jdk/compare/1a21c1a783d64ca0930c358c06a43975f96ffac6...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.

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

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 6, 2023
@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 7, 2023

/integrate

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 7, 2023

/backport jdk21u

@openjdk
Copy link

openjdk bot commented Nov 7, 2023

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

  • 42f43c5: 8319563: Reformat code in the FFM API
  • ac0ee20: 8319613: Complier error in benchmark TestLoadSegmentVarious
  • a7c0190: 8319238: JMX ThreadPoolAccTest.java is too verbose and should fail before timeout
  • 9ac6ac8: 8319559: [JVMCI] ensureLinked must be able to call Java
  • 1c0e7b7: 8319408: RISC-V: MaxVectorSize is not consistently checked in several situations
  • 74f1889: 8318678: Vector access on heap MemorySegments only works for byte[]
  • e1cae72: 8319197: Exclude hb-subset and hb-style from compilation
  • 419ed90: 8319450: New methods java.net.InetXAddress.ofLiteral() miss @SInCE tag
  • 439ed04: 8319233: AArch64: Build failure with clang due to -Wformat-nonliteral warning
  • e4803e0: 8318580: "javax/swing/MultiMonitor/MultimonVImage.java failing with Error. Can't find library: /open/test/jdk/java/awt/regtesthelpers" after JDK-8316053
  • ... and 17 more: https://git.openjdk.org/jdk/compare/1a21c1a783d64ca0930c358c06a43975f96ffac6...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Nov 7, 2023

@lahodaj Pushed as commit 541ff71.

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

@openjdk
Copy link

openjdk bot commented Nov 7, 2023

@lahodaj the backport was successfully created on the branch lahodaj-backport-541ff714 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 541ff714 from the openjdk/jdk repository.

The commit being backported was authored by Jan Lahoda on 7 Nov 2023 and was reviewed by Chen Liang 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 lahodaj-backport-541ff714:lahodaj-backport-541ff714
$ git checkout lahodaj-backport-541ff714
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git lahodaj-backport-541ff714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
3 participants