From 9460ee248005ffa81089a6f91b031bf7b031a33f Mon Sep 17 00:00:00 2001 From: Srikanth Sankaran Date: Wed, 12 Feb 2025 14:44:23 +0530 Subject: [PATCH] ECJ has two parallel mechanisms for canonical constructor generation for records * Fixes https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1090 --- .../ast/AbstractMethodDeclaration.java | 3 ++- .../compiler/ast/ConstructorDeclaration.java | 4 ++-- .../compiler/ast/TypeDeclaration.java | 24 ------------------- .../RecordsRestrictedClassTest.java | 5 ---- 4 files changed, 4 insertions(+), 32 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java index bc9546b0034..ca717ac6067 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java @@ -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); } } } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java index e8b59dc85eb..c3f71227f58 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java @@ -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); @@ -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); diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java index 6782a773a0a..d46c6841a62 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java @@ -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; @@ -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 statements = new ArrayList<>(); - int l = this.recordComponents != null ? this.recordComponents.length : 0; - if (l > 0 && this.fields != null) { - List 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) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java index 4916e61ffa7..9b63088027a 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java @@ -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");