A DESCRIPTION OF THE PROBLEM :
The following java code produces a "Internal error: stack sim error on ..."
---
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
---
This is most related to the fact that the only "result expression" is reachable but because of the if(true) it can be stripped out.
The following similar variants do compile however and behave as expected.
---
static int test2(int i, int p){
return switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
static int test3(int i, int p){
return i < 0 ? 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
} : i;
}
static int test4(int i, int p){
return switch (i){
case 5000:
yield 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
default: yield i;
};
}
---
Looking at the compiled bytecode 3 and 4, it does seem like the `5` is pushed to the stack, from some minor debugging it seems the stack simulation fails due to an unexpected integer on the stack
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
use the following code in a project
---
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
---
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It compiles without errors
ACTUAL -
It causes javac the throw an Internal Error
---------- BEGIN SOURCE ----------
Note: the following code does NOT compile
---
class Main{
public static void main(String[] args) {
test1(0,0);
}
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
}
---
---------- END SOURCE ----------
The following java code produces a "Internal error: stack sim error on ..."
---
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
---
This is most related to the fact that the only "result expression" is reachable but because of the if(true) it can be stripped out.
The following similar variants do compile however and behave as expected.
---
static int test2(int i, int p){
return switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
static int test3(int i, int p){
return i < 0 ? 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
} : i;
}
static int test4(int i, int p){
return switch (i){
case 5000:
yield 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
default: yield i;
};
}
---
Looking at the compiled bytecode 3 and 4, it does seem like the `5` is pushed to the stack, from some minor debugging it seems the stack simulation fails due to an unexpected integer on the stack
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
use the following code in a project
---
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
---
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It compiles without errors
ACTUAL -
It causes javac the throw an Internal Error
---------- BEGIN SOURCE ----------
Note: the following code does NOT compile
---
class Main{
public static void main(String[] args) {
test1(0,0);
}
static int test1(int i, int p){
return 5 + switch (p){
case 1 -> throw new RuntimeException("1");
case 2 -> throw new RuntimeException("2");
case 3 -> throw new RuntimeException("3");
default -> {if(true) throw new RuntimeException(); else yield 0;}
};
}
}
---
---------- END SOURCE ----------