Skip to content

Commit

Permalink
[Records] Subsume
Browse files Browse the repository at this point in the history
org.eclipse.jdt.internal.compiler.ast.CompactConstructorDeclaration into
org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration

* #3729
  • Loading branch information
srikanth-sankaran committed Feb 20, 2025
1 parent 754dab0 commit 9cb6321
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public void endVisit(ClassLiteralAccess classLiteral, BlockScope scope) {
public void endVisit(Clinit clinit, ClassScope scope) {
// do nothing by default
}
public void endVisit(CompactConstructorDeclaration ccd, ClassScope scope) {
// do nothing by default
}
public void endVisit(
CompilationUnitDeclaration compilationUnitDeclaration,
CompilationUnitScope scope) {
Expand Down Expand Up @@ -607,9 +604,6 @@ public boolean visit(Clinit clinit, ClassScope scope) {
public boolean visit(ModuleDeclaration module, CompilationUnitScope scope) {
return true;
}
public boolean visit(CompactConstructorDeclaration ccd, ClassScope scope) {
return true; // do nothing by default, keep traversing
}
public boolean visit(
CompilationUnitDeclaration compilationUnitDeclaration,
CompilationUnitScope scope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ public boolean visit(ModuleDeclaration module, CompilationUnitScope scope) {
return visitNode(module);
}

@Override
public boolean visit(CompactConstructorDeclaration ccd, ClassScope scope) {
return visitNode(ccd);
}

@Override
public boolean visit(CompilationUnitDeclaration compilationUnitDeclaration, CompilationUnitScope scope) {
return visitNode(compilationUnitDeclaration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ public boolean isCanonicalConstructor() {

return false;
}

public boolean isCompactConstructor() {
return false;
}

public boolean isDefaultConstructor() {

return false;
Expand Down Expand Up @@ -606,6 +611,11 @@ public void resolve(ClassScope upperScope) {
this.ignoreFurtherInvestigation = true;
}

if (this.isCompactConstructor() && !upperScope.referenceContext.isRecord()) {
upperScope.problemReporter().compactConstructorsOnlyInRecords(this);
return;
}

try {
bindArguments();
resolveReceiver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public TypeBinding bind(MethodScope scope, TypeBinding typeBinding, boolean used
if (!this.isUnnamed(scope)) {
if ((this.bits & ASTNode.ShadowsOuterLocal) != 0 && scope.isLambdaSubscope()) {
scope.problemReporter().lambdaRedeclaresArgument(this);
} else if (scope.referenceContext instanceof CompactConstructorDeclaration) {
} else if (scope.referenceContext instanceof ConstructorDeclaration cd && cd.isCompactConstructor()) {
// skip error reporting - hidden params - already reported in record components
} else {
scope.problemReporter().redefineArgument(this);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ public void analyseCode(ClassScope classScope, InitializationFlowContext initial
this.bits |= ASTNode.NeedFreeReturn;
}

if (this.isCompactConstructor()) {
for (FieldBinding field : this.binding.declaringClass.fields()) {
if (!field.isStatic()) {
flowInfo.markAsDefinitelyAssigned(field);
}
}
}

// reuse the initial reach mode for diagnosing missing blank finals
// no, we should use the updated reach mode for diagnosing uninitialized blank finals.
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=235781
Expand Down Expand Up @@ -475,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 instanceof CompactConstructorDeclaration) {
if (this.isCompactConstructor()) {
// 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 Expand Up @@ -606,7 +614,9 @@ public boolean isRecursive(ArrayList visited) {
@Override
public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
//fill up the constructor body with its statements
if (((this.bits & ASTNode.IsDefaultConstructor) != 0) && this.constructorCall == null){
if (this.isCompactConstructor()) {
this.constructorCall = SuperReference.implicitSuperConstructorCall();
} else if (((this.bits & ASTNode.IsDefaultConstructor) != 0) && this.constructorCall == null){
this.constructorCall = SuperReference.implicitSuperConstructorCall();
this.constructorCall.sourceStart = this.sourceStart;
this.constructorCall.sourceEnd = this.sourceEnd;
Expand Down Expand Up @@ -680,7 +690,7 @@ public void resolveStatements() {
}
this.constructorCall = null;
} else if (sourceType.isRecord() &&
!(this instanceof CompactConstructorDeclaration) && // compact constr should be marked as canonical?
!this.isCompactConstructor() && // compact constr should be marked as canonical?
(this.binding != null && !this.binding.isCanonicalConstructor()) &&
this.constructorCall.accessMode != ExplicitConstructorCall.This) {
this.scope.problemReporter().recordMissingExplicitConstructorCallInNonCanonicalConstructor(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public void resolve(BlockScope scope) {
}
}
if (hasError) {
if (!(methodDeclaration instanceof CompactConstructorDeclaration)) {// already flagged for CCD
if (!(methodDeclaration.isCompactConstructor())) {// already flagged for CCD
if (JavaFeature.FLEXIBLE_CONSTRUCTOR_BODIES.isSupported(scope.compilerOptions())) {
boolean isTopLevel = Arrays.stream(methodDeclaration.statements).anyMatch(this::equals);
if (isTopLevel)
Expand Down Expand Up @@ -495,7 +495,7 @@ private boolean checkAndFlagExplicitConstructorCallInCanonicalConstructor(Abstra
if (methodDecl.binding == null || methodDecl.binding.declaringClass == null
|| !methodDecl.binding.declaringClass.isRecord())
return true;
boolean isInsideCCD = methodDecl instanceof CompactConstructorDeclaration;
boolean isInsideCCD = methodDecl.isCompactConstructor();
if (this.accessMode != ExplicitConstructorCall.ImplicitSuper) {
if (isInsideCCD)
scope.problemReporter().recordCompactConstructorHasExplicitConstructorCall(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public FlowInfo analyseAssignment(BlockScope currentScope, FlowContext flowConte
&& !(this.receiver instanceof QualifiedThisReference)
&& ((this.receiver.bits & ASTNode.ParenthesizedMASK) == 0) // (this).x is forbidden
&& currentScope.allowBlankFinalFieldAssignment(this.binding)
&& !currentScope.methodScope().isCompactConstructorScope) {
&& (!(currentScope.methodScope().referenceContext instanceof ConstructorDeclaration cd) || !cd.isCompactConstructor())) {
if (flowInfo.isPotentiallyAssigned(this.binding)) {
currentScope.problemReporter().duplicateInitializationOfBlankFinalField(
this.binding,
Expand All @@ -107,7 +107,7 @@ public FlowInfo analyseAssignment(BlockScope currentScope, FlowContext flowConte
}
flowInfo.markAsDefinitelyAssigned(this.binding);
} else {
if (currentScope.methodScope().isCompactConstructorScope)
if (currentScope.methodScope().referenceContext instanceof ConstructorDeclaration cd && cd.isCompactConstructor())
currentScope.problemReporter().recordIllegalExplicitFinalFieldAssignInCompactConstructor(this.binding, this);
else
// assigning a final field outside an initializer or constructor or wrong reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,7 @@ public ConstructorDeclaration getConstructor(Parser parser) {
this.methods[i] = m;
}
} else {
if (am instanceof CompactConstructorDeclaration) {
CompactConstructorDeclaration ccd = (CompactConstructorDeclaration) am;
if (am instanceof ConstructorDeclaration ccd && ccd.isCompactConstructor()) {
if (ccd.arguments == null)
ccd.arguments = getArgumentsFromComponents(this.recordComponents);
return ccd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public class MethodScope extends BlockScope {
// remember suppressed warning re missing 'default:' to give hints on possibly related flow problems
public boolean hasMissingSwitchDefault; // TODO(stephan): combine flags to a bitset?

public boolean isCompactConstructorScope = false;

static {
if (Boolean.getBoolean("jdt.flow.test.extra")) { //$NON-NLS-1$
baseAnalysisIndex = 64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,12 @@ protected void consumeConstructorHeaderName(boolean isCompact) {

// ConstructorHeaderName ::= Modifiersopt 'Identifier' '('
// CompactConstructorHeaderName ::= Modifiersopt 'Identifier'
ConstructorDeclaration cd = isCompact ? new CompactConstructorDeclaration(this.compilationUnit.compilationResult) : new ConstructorDeclaration(this.compilationUnit.compilationResult);
ConstructorDeclaration cd = new ConstructorDeclaration(this.compilationUnit.compilationResult) {
@Override
public boolean isCompactConstructor() {
return isCompact;
}
};

//name -- this is not really revelant but we do .....
cd.selector = this.identifierStack[this.identifierPtr];
Expand Down Expand Up @@ -10409,7 +10414,7 @@ protected void consumeRecordDeclaration() {
typeDecl.createDefaultConstructor(!(this.diet && this.dietInt == 0), true);
//convert constructor that do not have the type's name into methods
ConstructorDeclaration cd = typeDecl.getConstructor(this);
if (cd instanceof CompactConstructorDeclaration
if (cd.isCompactConstructor()
|| ((typeDecl.recordComponents == null || typeDecl.recordComponents.length == 0)
&& (cd.arguments == null || cd.arguments.length == 0))) {
cd.bits |= ASTNode.IsCanonicalConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11913,7 +11913,7 @@ public void recordCompactConstructorHasReturnStatement(ReturnStatement stmt) {
stmt.sourceStart,
stmt.sourceEnd);
}
public void compactConstructorsOnlyInRecords(CompactConstructorDeclaration ccd) {
public void compactConstructorsOnlyInRecords(AbstractMethodDeclaration ccd) {
this.handle(
IProblem.CompactConstructorOnlyInRecords,
NoArgument,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public ASTNode convert(boolean isInterface, org.eclipse.jdt.internal.compiler.as
methodName.internalSetIdentifier(new String(methodDeclaration.selector));
int start = methodDeclaration.sourceStart;
int end;
if (DOMASTUtil.isRecordDeclarationSupported(this.ast) && methodDeclaration instanceof CompactConstructorDeclaration) {
if (DOMASTUtil.isRecordDeclarationSupported(this.ast) && methodDeclaration.isCompactConstructor()) {
methodDecl.setCompactConstructor(true);
end = start + methodDeclaration.selector.length -1;
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public int resolveLevel(Binding binding) {
if (binding == null) return INACCURATE_MATCH;
if( binding instanceof LocalVariableBinding) {
// for matching the component in constructor of a record
if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof CompactConstructorDeclaration) {
if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof ConstructorDeclaration cd && cd.isCompactConstructor()) {
return matchLocal((LocalVariableBinding) binding, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.CompactConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
import org.eclipse.jdt.internal.compiler.ast.NameReference;
Expand Down Expand Up @@ -127,7 +127,7 @@ public int resolveLevel(Binding binding) {
return matchField(binding, true);
}
if(binding instanceof LocalVariableBinding) {
if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof CompactConstructorDeclaration) {
if ( ((LocalVariableBinding)binding).declaringScope.referenceContext() instanceof ConstructorDeclaration cd && cd.isCompactConstructor()) {
//update with binding
if( this.pattern instanceof FieldPattern) {
return matchField(binding, true);
Expand Down

0 comments on commit 9cb6321

Please sign in to comment.