apache / apache/netbeans

var in Pattern Matching for Switch

Open
#7,388 6 comments 0 reactions 0 assignees View on GitHub
Ant Java kind:bug
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 21

### What happened

Seems using var in pattern matching when implementing Algebraic Data Type patterns results "_Exception in thread "main" java.lang.RuntimeException: Uncompilable code_"

### Language / Project Type / NetBeans Component

_No response_

### How to reproduce

Seems using var in pattern matching when implementing Algebraic Data Type patterns results "_Exception in thread "main" java.lang.RuntimeException: Uncompilable code_"

For example, the code below throws the exception when run on netbeans

```
public class ADTEvaluation {

sealed interface Expr{}

record ConstExpr(int i) implements Expr{}
record SumExpr(Expr left, Expr right) implements Expr{}

static int eval(Expr e){
return switch(e){
case SumExpr(var a, var b) -> eval(a) + eval(b);
case ConstExpr(int i) -> i;
default -> 3;
};
}


public static void main(String... args)
{
var v = new ConstExpr(3);

var x = eval(v);
System.out.println(x);

}
}
```

But if I change the SumExpr(var a, var b), to SumExpr(ConstExpr a, ConstExpr b) in the pattern matching in switch, the following code works

```
public class ADTEvaluation {

sealed interface Expr{}

record ConstExpr(int i) implements Expr{}
record SumExpr(Expr left, Expr right) implements Expr{}

static int eval(Expr e){
return switch(e){
case SumExpr(ConstExpr a, ConstExpr b) -> eval(a) + eval(b);
case ConstExpr(int i) -> i;
default -> 3;
};
}


public static void main(String... args)
{
var v = new ConstExpr(3);

var x = eval(v);
System.out.println(x);

}
}
```

I'm running Netbeans 21, using JDK 22. There is no problem if I run the code in jdk 22 directly.

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Windows 10

### JDK

22

### Apache NetBeans packaging

Apache NetBeans provided installer

### Anything else

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure in Apache NetBeans 21 with JDK 22 using the provided ADTEvaluation example, then compare it with running the same code directly on JDK 22. Trace the NetBeans Java support involved in compiling or running record patterns; done means the var patterns compile and run in NetBeans as they do directly on JDK 22.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.