Skip to content

Commit

Permalink
ECJ has two parallel mechanisms for canonical constructor generation for
Browse files Browse the repository at this point in the history
records

* Fixes #1090
  • Loading branch information
srikanth-sankaran committed Feb 12, 2025
1 parent b2442a6 commit 9460ee2
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,8 @@ public void resolveStatements() {
resolveStatements(this.statements, this.scope);
} else if ((this.bits & UndocumentedEmptyBlock) != 0) {
if (!this.isConstructor() || this.arguments != null) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319626
this.scope.problemReporter().undocumentedEmptyBlock(this.bodyStart-1, this.bodyEnd+1);
if ((this.bits & IsImplicit) == 0)
this.scope.problemReporter().undocumentedEmptyBlock(this.bodyStart-1, this.bodyEnd+1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void analyseCode(ClassScope classScope, InitializationFlowContext initial
this.bits |= ASTNode.NeedFreeReturn;
}

if (this.isCompactConstructor()) {
if (this.isCompactConstructor() || (this.bits & IsImplicit) != 0) {
for (FieldBinding field : this.binding.declaringClass.fields()) {
if (!field.isStatic()) {
flowInfo.markAsDefinitelyAssigned(field);
Expand Down Expand Up @@ -483,7 +483,7 @@ private void internalGenerateCode(ClassScope classScope, ClassFile classFile) {
throw new AbortMethod(this.scope.referenceCompilationUnit().compilationResult, null);
}
if ((this.bits & ASTNode.NeedFreeReturn) != 0) { //
if (this.isCompactConstructor()) {
if (this.isCompactConstructor() || (this.bits & IsImplicit) != 0) {
// Note: the body of a compact constructor may not contain a return statement and so will need a injected return
for (RecordComponent rc : classScope.referenceContext.recordComponents) {
LocalVariableBinding parameter = this.scope.findVariable(rc.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.IProblem;
Expand Down Expand Up @@ -400,26 +396,6 @@ public ConstructorDeclaration createDefaultConstructorForRecord(boolean needExpl
constructor.constructorCall.sourceStart = this.sourceStart;
constructor.constructorCall.sourceEnd = this.sourceEnd;
}
/* The body of the implicitly declared canonical constructor initializes each field corresponding
* to a record component with the corresponding formal parameter in the order that they appear
* in the record component list.*/
List<Statement> statements = new ArrayList<>();
int l = this.recordComponents != null ? this.recordComponents.length : 0;
if (l > 0 && this.fields != null) {
List<String> fNames = Arrays.stream(this.fields)
.filter(f -> f.isARecordComponent)
.map(f ->new String(f.name))
.collect(Collectors.toList());
for (int i = 0; i < l; ++i) {
RecordComponent component = this.recordComponents[i];
if (!fNames.contains(new String(component.name)))
continue;
FieldReference lhs = new FieldReference(component.name, 0);
lhs.receiver = ThisReference.implicitThis();
statements.add(new Assignment(lhs, new SingleNameReference(component.name, 0), 0));
}
}
constructor.statements = statements.toArray(new Statement[0]);

//adding the constructor in the methods list: rank is not critical since bindings will be sorted
if (needToInsert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5644,11 +5644,6 @@ public void testBug564672_042() {
"----------\n" +
"1. ERROR in X.java (at line 1)\n" +
" record Point(record x, int i) { }\n" +
" ^\n" +
"record cannot be resolved to a type\n" +
"----------\n" +
"2. ERROR in X.java (at line 1)\n" +
" record Point(record x, int i) { }\n" +
" ^^^^^^\n" +
"\'record\' is not a valid type name; it is a restricted identifier and not allowed as a type identifier in Java 16\n" +
"----------\n");
Expand Down

0 comments on commit 9460ee2

Please sign in to comment.