IDEA-230380 Assignments to record components are erroneously allowed in non-canonical constructors

GitOrigin-RevId: 4a427ce68ddfbd9b2d274c131a42e40e02f306f7
This commit is contained in:
Tagir Valeev
2020-01-10 07:02:33 +00:00
committed by intellij-monorepo-bot
parent 5724865d36
commit c7557bba96
2 changed files with 20 additions and 6 deletions
@@ -543,11 +543,15 @@ public class HighlightControlFlowUtil {
(PsiMethod)codeBlock.getParent() : null;
// assignment to final field in several constructors threatens us only if these are linked (there is this() call in the beginning)
final List<PsiMethod> redirectedConstructors = ctr != null && ctr.isConstructor() ? JavaHighlightUtil.getChainedConstructors(ctr) : Collections.emptyList();
for (PsiMethod redirectedConstructor : redirectedConstructors) {
PsiCodeBlock body = redirectedConstructor.getBody();
if (body != null && variableDefinitelyAssignedIn(variable, body)) {
alreadyAssigned = true;
break;
if (!redirectedConstructors.isEmpty() && aClass.isRecord()) {
alreadyAssigned = true;
} else {
for (PsiMethod redirectedConstructor : redirectedConstructors) {
PsiCodeBlock body = redirectedConstructor.getBody();
if (body != null && variableDefinitelyAssignedIn(variable, body)) {
alreadyAssigned = true;
break;
}
}
}
}
@@ -35,7 +35,7 @@ record VarArgMismatch2(int[] x) {
record Delegate(int x) {
public Delegate(int x) {
<error descr="Canonical constructor cannot delegate to another constructor">this()</error>;
this.x = 0;
<error descr="Variable 'x' might already have been assigned to">this.x</error> = 0;
}
public <error descr="Non-canonical record constructor must delegate to another constructor">Delegate</error>() {
@@ -57,4 +57,14 @@ record ImplicitCanonicalConstructor(String s) {
static void test() {
new ImplicitCanonicalConstructor("Asdasd");
}
}
record AssignmentInNonCanonical(int x, int y, long depth) {
public AssignmentInNonCanonical(int x, int y) {
this(x, y, 10);
<error descr="Variable 'x' might already have been assigned to">this.x</error> = x;
}
void method() {
<error descr="Cannot assign a value to final variable 'x'">this.x</error> = 0;
}
}