eclipse-jdt / eclipse-jdt/eclipse.jdt.core

[BETA_JAVA28] Implement strict/safe construction rules for record classes

Open
#5,324 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

The following code should compile with source level 28, but not with source level 28 + --enable-preview:

```
import java.util.List;
record Node(String label, List edges) {

static void nullCheck(Object arg, Object owner) {
if (arg == null) {
String msg = "null arg for " + owner.toString();
throw new IllegalArgumentException(msg);
}
}

public Node {
nullCheck(label, this); // Error with --enable-preview
nullCheck(edges, this); // Error with --enable-preview
}

}
```

javac reports:

```
X.java:12: error: reference to this may only appear after an explicit constructor invocation
nullCheck(label, this); // Error with --enable-preview
^
X.java:13: error: reference to this may only appear after an explicit constructor invocation
nullCheck(edges, this); // Error with --enable-preview
^
2 errors
```

See the section `Other construction enhancements` in https://openjdk.org/jeps/401

As this is not related value classes, I am farming out this task as a separate ticket - I may get to this only later.

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Node record reproducer and compile it with source level 28 both with and without --enable-preview. Read the “Other construction enhancements” section of JEP 401 alongside javac’s current diagnostics. Done means the example has the specified compile behavior and related regression coverage is passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.