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

[BETA_JAVA28] With preview enabled, allow fields to be read in early construction contexts

Open
#5,329 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

https://openjdk.org/jeps/401#Other-construction-enhancements states:

`We loosen some of the restrictions on accessing the fields of all larval objects, whether they are value objects or identity objects. In Java 27, a new object's fields could be set in the early construction phase, but not read. In Java 28, with preview features enabled, these fields can be both written and read in the early construction phase. It continues to be illegal to refer to inherited fields, invoke instance methods, or share this with other code until the late construction phase.`

So this illegal program:

```
public class X {
int x;
int y;

public X() {
x = 10;
y = x;
super();
}

}
```

that generates the error `X.java:7: error: reference to x may only appear after an explicit constructor invocation` becomes legal at -source 28 --enable-preview

ECJ should adjust accordingly.

Funnily when that program is compiled with javac without --enable-preview at -source 28 we get: (with b11)

```
$ ~/jdk/jdk-28-ea11/jdk-28/bin/javac -g -source 28 X.java
X.java:7: error: value classes are a preview feature and are disabled by default.
y = x;
^
(use --enable-preview to enable value classes)
1 error
```

This program has nothing to do with value classes whatsoever 😆

Contributor guide

Open the contributing guide

Research direction

Start with the X.java reproducer and the JEP 401 section on other-construction enhancements. Check ECJ's validation of field reads before an explicit constructor invocation under -source 28 --enable-preview, and compare the result with the expected legal behavior. Done means the reproducer compiles in that mode while inherited-field access and other restricted operations remain rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
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.