Skip to content

Commit 24d663a

Browse files
author
svtk
committed
KT-455 Do not repeat errors in definite assignment checks
1 parent 1cbee4b commit 24d663a

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public Pair<Map<VariableDescriptor, InitializationPoints>, Map<VariableDescripto
182182
initialMapForStartInstruction,
183183
true);
184184

185+
final Collection<VariableDescriptor> varWithUninitializedErrorGenerated = Sets.newHashSet();
186+
final Collection<VariableDescriptor> varWithValReassignErrorGenerated = Sets.newHashSet();
185187
traverser.traverseAndAnalyzeInstructionGraph(new JetControlFlowGraphTraverser.InstructionDataAnalyzeStrategy<Map<VariableDescriptor, InitializationPoints>>() {
186188
@Override
187189
public void execute(Instruction instruction, @Nullable Map<VariableDescriptor, InitializationPoints> enterData, @Nullable Map<VariableDescriptor, InitializationPoints> exitData) {
@@ -200,7 +202,8 @@ public void execute(Instruction instruction, @Nullable Map<VariableDescriptor, I
200202
isInitialized = true;
201203
}
202204
}
203-
if (!analyzeLocalDeclaration && !isInitialized) {
205+
if (!analyzeLocalDeclaration && !isInitialized && !varWithUninitializedErrorGenerated.contains(variableDescriptor)) {
206+
varWithUninitializedErrorGenerated.add(variableDescriptor);
204207
trace.report(Errors.UNINITIALIZED_VARIABLE.on((JetSimpleNameExpression) element, variableDescriptor));
205208
}
206209
}
@@ -222,9 +225,10 @@ else if (instruction instanceof WriteValueInstruction) {
222225
}
223226
}
224227
JetExpression expression = (JetExpression) element;
225-
if (!analyzeLocalDeclaration && hasInitializer && !variableDescriptor.isVar()) {
228+
if (!analyzeLocalDeclaration && hasInitializer && !variableDescriptor.isVar() && !varWithValReassignErrorGenerated.contains(variableDescriptor)) {
226229
PsiElement psiElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor);
227230
JetProperty property = psiElement instanceof JetProperty ? (JetProperty) psiElement : null;
231+
varWithValReassignErrorGenerated.add(variableDescriptor);
228232
trace.report(Errors.VAL_REASSIGNMENT.on(expression, variableDescriptor, property == null ? new JetProperty[0] : new JetProperty[] { property }));
229233
}
230234
if (expression instanceof JetSimpleNameExpression && inAnonymousInitializers &&

compiler/testData/checkerWithErrorTypes/quick/LValueAssignment.jet

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ class C() : B() {
1212
this.b = 123
1313
super.b = 23
1414
<!VAL_REASSIGNMENT!>this.c<!> = 34
15-
<!VAL_REASSIGNMENT!>super.c<!> = 3535
15+
super.c = 3535 //repeat for 'c'
1616

1717
<!VARIABLE_EXPECTED!>getInt()<!> = 12
1818
}
1919

20+
fun foo1(c: C) {
21+
<!VAL_REASSIGNMENT!>super.c<!> = 34
22+
}
23+
2024
fun bar(c: C) {
2125
<!VARIABLE_EXPECTED!>this<!> = c //should be an error
2226
}
@@ -47,12 +51,16 @@ fun canBe(var i: Int, val j: Int) {
4751
(@label i) = 34
4852

4953
(<!VAL_REASSIGNMENT!>j<!>: Int) = 36
50-
(@label <!VAL_REASSIGNMENT!>j<!>) = 34
54+
(@label j) = 34 //repeat for j
5155

5256
val a = A()
5357
(@ a.a) = 3894
5458
}
5559

60+
fun canBe2(val j: Int) {
61+
(@label <!VAL_REASSIGNMENT!>j<!>) = 34
62+
}
63+
5664
class A() {
5765
var a: Int = 3
5866
}
@@ -92,16 +100,22 @@ class Test() {
92100
(a : Int) += 34
93101

94102
<!VAL_REASSIGNMENT!>b<!> += 34
95-
(@l <!VAL_REASSIGNMENT!>b<!>) += 34
96-
(<!VAL_REASSIGNMENT!>b<!> : Int) += 34
97-
(<!VAL_REASSIGNMENT!>b<!>) += 3
98103

99104
a++
100105
(@ a)++
101106
(a : Int)++
102107
(a)++
103108
}
104109

110+
fun testVariables1() {
111+
val b: Int = 34
112+
113+
(@l <!VAL_REASSIGNMENT!>b<!>) += 34
114+
//repeat for b
115+
(b : Int) += 34
116+
(b) += 3
117+
}
118+
105119
fun testArrays(a: Array<Int>, ab: Ab) {
106120
a[3] = 4
107121
a[4]++

compiler/testData/checkerWithErrorTypes/quick/UninitializedOrReassignedVariables.jet

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fun t1(b : Boolean) {
3030
doSmth(<!UNINITIALIZED_VARIABLE!>t<!>)
3131
else
3232
t = "ss"
33-
doSmth(<!UNINITIALIZED_VARIABLE!>t<!>)
33+
doSmth(t) //repeat for t
3434

3535
val i = 3
3636
doSmth(i)
@@ -82,13 +82,13 @@ abstract enum class ProtocolState {
8282
abstract fun signal() : ProtocolState
8383
}
8484

85-
fun t2() {
85+
fun t3() {
8686
val x: ProtocolState = ProtocolState.WAITING
8787
<!VAL_REASSIGNMENT!>x<!> = x.signal()
88-
<!VAL_REASSIGNMENT!>x<!> = x.signal()
88+
x = x.signal() //repeat for x
8989
}
9090

91-
fun t3() {
91+
fun t4() {
9292
val x = 1
9393
<!VAL_REASSIGNMENT!>x<!> += 2
9494
val y = 3
@@ -97,7 +97,7 @@ fun t3() {
9797
z -= y
9898
}
9999

100-
fun t4() {
100+
fun t5() {
101101
for (i in 0..2) {
102102
<!VAL_REASSIGNMENT!>i<!> += 1
103103
fun t5() {
@@ -119,7 +119,7 @@ class AnonymousInitializers(var a: String, val b: String) {
119119
a = "s"
120120

121121
<!VAL_REASSIGNMENT!>$b<!> = "3"
122-
<!VAL_REASSIGNMENT!>b<!> = "tt"
122+
b = "tt" //repeat for b
123123
}
124124

125125
val i: Int
@@ -345,6 +345,9 @@ class M() {
345345
fun test(m : M) {
346346
<!VAL_REASSIGNMENT!>m.x<!> = 23
347347
m.y = 23
348+
}
349+
350+
fun test1(m : M) {
348351
<!VAL_REASSIGNMENT!>m.x<!>++
349352
m.y--
350353
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//KT-455 Do not repeat errors in definite assignment checks
2+
3+
namespace kt455
4+
5+
fun foo() {
6+
val a: Int
7+
doSmth(<!UNINITIALIZED_VARIABLE!>a<!>) //error
8+
doSmth(a) //no repeat error
9+
}
10+
fun doSmth(i: Int) {}

0 commit comments

Comments
 (0)