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

[21][pattern switch] IndexOutOfBoundsException regression against preview 4 #1466

Closed
kriegaex opened this issue Oct 5, 2023 · 4 comments · Fixed by #1565
Closed

[21][pattern switch] IndexOutOfBoundsException regression against preview 4 #1466

kriegaex opened this issue Oct 5, 2023 · 4 comments · Fixed by #1565
Assignees
Labels
bug Something isn't working regression Something was broken by a previous change
Milestone

Comments

@kriegaex
Copy link
Contributor

kriegaex commented Oct 5, 2023

Relates to #587.

This was working fine in ECJ 3.35.0 (Java 20), but leads to problems in 3.36.0-SNAPSHOT (Java 21)

public class SwitchPatternPreview4OK {
  public static void main(String[] args) {
    constantLabelMustAppearBeforePatternInteger(-1);
    constantLabelMustAppearBeforePatternInteger(0);
    constantLabelMustAppearBeforePatternInteger(42);
    constantLabelMustAppearBeforePatternInteger(-99);
    constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123));
    constantLabelMustAppearBeforePatternInteger(null);
  }

  static String constantLabelMustAppearBeforePatternInteger(Integer i) {
    switch (i) {
      case null -> System.out.println("value unavailable: " + i);
      case -1, 1 -> System.out.println("absolute value 1: " + i);
      case Integer value when value > 0 -> System.out.println("positive integer: " + i);
      default -> System.out.println("other integer: " + i);
    }
    return i == null ? "null" : i.toString();
  }
}

JDK 20 and ECJ 3.35.0

XXX> java -jar ecj-3.35.0.jar -20 --enable-preview SwitchPatternPreview4OK.java
----------
1. WARNING in XXX\SwitchPatternPreview4OK.java (at line 13)
        case null -> System.out.println("value unavailable: " + i);
             ^^^^
You are using a preview language feature that may or may not be supported in a future release
----------
2. WARNING in XXX\SwitchPatternPreview4OK.java (at line 15)
        case Integer value when value > 0 -> System.out.println("positive integer: " + i);
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You are using a preview language feature that may or may not be supported in a future release
----------
2 problems (2 warnings)

XXX> "c:\Program Files\Java\jdk-20\bin\java.exe" --enable-preview SwitchPatternPreview4OK
absolute value 1: -1
other integer: 0
positive integer: 42
other integer: -99
positive integer: 123
value unavailable: null

JDK 21 and Javac

XXX> javac SwitchPatternPreview4OK.java

XXX> java SwitchPatternPreview4OK
absolute value 1: -1
other integer: 0
positive integer: 42
other integer: -99
positive integer: 123
value unavailable: null

JDK 21 and ECJ 3.36.0-SNAPSHOT

XXX> java -jar ecj-3.36.0-20231005.001952-71.jar -21 SwitchPatternPreview4OK.java

XXX> java SwitchPatternPreview4OK
absolute value 1: -1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.util.Objects.checkIndex(Objects.java:385)
        at SwitchPatternPreview4OK.constantLabelMustAppearBeforePatternInteger(SwitchPatternPreview4OK.java:12)
        at SwitchPatternPreview4OK.main(SwitchPatternPreview4OK.java:4)

You see, I used a snapshot from today. Please fix the problem. Thank you.

@kriegaex
Copy link
Contributor Author

kriegaex commented Oct 5, 2023

Note to myself: Reactivate code commented out it in

  • features1921/java21/SwitchPatternPreview3Aspect.aj,
  • features1921/java21/SwitchPatternPreview4OK.java

after this has been fixed and merged the AspectJ fork of JDT Core.

kriegaex added a commit to eclipse-aspectj/aspectj that referenced this issue Oct 5, 2023
This mostly affects pattern matching for switch and record patterns.

Two tests which were working before for pattern matching for switch
(preview 4), started to fail, so the corresponding code was commented
out and eclipse-jdt/eclipse.jdt.core#1466
recorded.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
@iloveeclipse iloveeclipse added bug Something isn't working regression Something was broken by a previous change labels Oct 5, 2023
@mpalat mpalat self-assigned this Oct 27, 2023
mpalat added a commit to mpalat/eclipse.jdt.core that referenced this issue Oct 27, 2023
@mpalat mpalat added this to the 4.30 M3 milestone Oct 27, 2023
@mpalat mpalat closed this as completed in c757fc7 Oct 30, 2023
@kriegaex
Copy link
Contributor Author

kriegaex commented Nov 1, 2023

Sorry, this does not seem to be fixed. The switch seems to enter the wrong branches, The test output should be, as quoted above in the Javac-compiled output:

absolute value 1: -1
other integer: 0
positive integer: 42
other integer: -99
positive integer: 123
value unavailable: null

But actually, it is:

absolute value 1: -1
positive integer: 0
positive integer: 42
positive integer: -99
positive integer: 123
value unavailable: null

Neither 0 nor -99 are positive integers.

Your test is even asserting on the wrong values.

Please reopen, @mpalat.

@iloveeclipse iloveeclipse reopened this Nov 1, 2023
@mpalat
Copy link
Contributor

mpalat commented Nov 3, 2023

will take a look

@kriegaex
Copy link
Contributor Author

The corresponding tests in AspectJ also work now. Thanks for fixing the fix. 😉

kriegaex added a commit to eclipse-aspectj/aspectj that referenced this issue Dec 1, 2023
eclipse-jdt/eclipse.jdt.core#1466 has finally
been fixed.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression Something was broken by a previous change
Projects
None yet
3 participants