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

VerifyError in switch expression on double #2382

Closed
aumann opened this issue Apr 24, 2024 · 5 comments · Fixed by #2385
Closed

VerifyError in switch expression on double #2382

aumann opened this issue Apr 24, 2024 · 5 comments · Fixed by #2385
Assignees
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues
Milestone

Comments

@aumann
Copy link

aumann commented Apr 24, 2024

This code compiles, but fails at runtime:

package test;
public class SwitchVerifyError {

  public static void main(String[] args) {
    double d = 3;

    double r = switch (d) {
      case 1.0 -> 0.2;
      case 2.0 -> 0.5;
      case 8.0 -> 2;
      case 9.0 -> 3;
      default -> 3;
    };
    System.out.println(r);
  }

}

The error is:

Fehler: Hauptklasse test.SwitchVerifyError kann nicht initialisiert werden
Ursache: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    test/SwitchVerifyError.main([Ljava/lang/String;)V @5: tableswitch
  Reason:
    Type double_2nd (current frame, stack[1]) is not assignable to integer
  Current Frame:
    bci: @5
    flags: { }
    locals: { '[Ljava/lang/String;', double, double_2nd }
    stack: { double, double_2nd }
  Bytecode:
    0000000: 1400 1048 27aa 0000 0000 004b 0000 0001
    0000010: 0000 0009 0000 0033 0000 0039 0000 004b
    0000020: 0000 004b 0000 004b 0000 004b 0000 004b
    0000030: 0000 003f 0000 0045 1400 12a7 0018 1400
    0000040: 14a7 0012 1400 16a7 000c 1400 10a7 0006
    0000050: 1400 104a b200 1829 b600 1eb1          
  Stackmap Table:
    append_frame(@56,Double)
    same_frame(@62)
    same_frame(@68)
    same_frame(@74)
    same_frame(@80)
    same_locals_1_stack_item_frame(@83,Double)

The JDT versions according to my eclipse installation details are: 3.37.0.v20240215-1452 org.eclipse.jdt.core and 3.19.400.v20240229-0520 org.eclipse.jdt

I do not know if the bug is that it compiles at all, or that it fails at runtime...

@iloveeclipse
Copy link
Member

I can reproduce on latest master, where I also see a new warning attached to main method:
Internal inconsistency: Inappropriate operand stack size encountered during translation
I assume this is one of the cases that @srikanth-sankaran is already working on.

@iloveeclipse iloveeclipse added bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues labels Apr 24, 2024
@srikanth-sankaran srikanth-sankaran self-assigned this Apr 24, 2024
@srikanth-sankaran
Copy link
Contributor

I'll follow up.

@srikanth-sankaran
Copy link
Contributor

This code is not supposed to compile in the first place.

14.11 states:

The switch statement transfers control to one of several statements or expressions,
depending on the value of an expression.
SwitchStatement:
switch ( Expression ) SwitchBlock
The Expression is called the selector expression. The type of the selector expression
must be char, byte, short, int, or a reference type, or a compile-time error occurs.

JDK21 rejects with (I renamed the class to be X.java)

X.java:7: error: constant label of type double is not compatible with switch selector type double
      case 1.0 -> 0.2;
           ^
X.java:8: error: constant label of type double is not compatible with switch selector type double
      case 2.0 -> 0.5;
           ^
X.java:9: error: constant label of type double is not compatible with switch selector type double
      case 8.0 -> 2;
           ^
X.java:10: error: constant label of type double is not compatible with switch selector type double
      case 9.0 -> 3;
           ^
4 errors

which is weird.

JDK22 rejects it with:

X.java:6: error: selector type double is not allowed
    double r = switch (d) {
                      ^
1 error

which is bang on.

@srikanth-sankaran
Copy link
Contributor

On master, we reject the code correctly for language levels <=20. The verify error is encountered for levels JDK21 and JDK22 (only)

@stephan-herrmann
Copy link
Contributor

On master, we reject the code correctly for language levels <=20. The verify error is encountered for levels JDK21 and JDK22 (only)

@srikanth-sankaran What exactly is fixed here? I still see javac and ecj disagreeing, whereas above you said that javac is right. So?

I started to prepare SwitchExpressionYieldTest for run.javac mode, but currently that's more than I can chew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants