Skip to content

Commit 5b078fb

Browse files
committed
JS: augmented assignments and increments (KT-14810) fixed
1 parent d2fdc7f commit 5b078fb

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Enable when KT-14833 is fixed.
2+
// IGNORE_BACKEND: JVM
3+
import kotlin.reflect.KProperty
4+
5+
var a = 0
6+
7+
var b: Int
8+
get() = a
9+
set(v: Int) {
10+
a = v
11+
}
12+
13+
class A {
14+
var c: Int
15+
get() = a
16+
set(v: Int) {
17+
a = v
18+
}
19+
}
20+
21+
var A.d: Int
22+
get() = a
23+
set(v: Int) {
24+
a = v
25+
}
26+
27+
var Int.e: Int
28+
get() = a
29+
set(v: Int) {
30+
a = v
31+
}
32+
33+
object SimpleDelegate {
34+
operator fun getValue(thisRef: Any?, desc: KProperty<*>): Int {
35+
return a
36+
}
37+
38+
operator fun setValue(thisRef: Any?, desc: KProperty<*>, value: Int) {
39+
a = value
40+
}
41+
}
42+
43+
var f by SimpleDelegate
44+
45+
fun box(): String {
46+
if (b++ != 0) return "fail b++: $b"
47+
if (++b != 2) return "fail ++b: $b"
48+
if (--b != 1) return "fail --b: $b"
49+
if (b-- != 1) return "fail b--: $b"
50+
b += 10
51+
if (b != 10) return "fail b +=: $b"
52+
b *= 10
53+
if (b != 100) return "fail b *=: $b"
54+
b /= 5
55+
if (b != 20) return "fail b /=: $b"
56+
b -= 10
57+
if (b != 10) return "fail b -=: $b"
58+
b %= 7
59+
if (b != 3) return "fail b %=: $b"
60+
61+
var q = A()
62+
63+
a = 0
64+
if (q.c++ != 0) return "fail q.c++: ${q.c}"
65+
if (++q.c != 2) return "fail ++q.c: ${q.c}"
66+
if (--q.c != 1) return "fail --q.c: ${q.c}"
67+
if (q.c-- != 1) return "fail q.c--: ${q.c}"
68+
q.c += 10
69+
if (q.c != 10) return "fail q.c +=: ${q.c}"
70+
q.c *= 10
71+
if (q.c != 100) return "fail q.c *=: ${q.c}"
72+
q.c /= 5
73+
if (q.c != 20) return "fail q.c /=: ${q.c}"
74+
q.c -= 10
75+
if (q.c != 10) return "fail q.c -=: ${q.c}"
76+
q.c %= 7
77+
if (q.c != 3) return "fail q.c %=: ${q.c}"
78+
79+
a = 0
80+
if (q.d++ != 0) return "fail q.d++: ${q.d}"
81+
if (++q.d != 2) return "fail ++q.d: ${q.d}"
82+
if (--q.d != 1) return "fail --q.d: ${q.d}"
83+
if (q.d-- != 1) return "fail q.d--: ${q.d}"
84+
q.d += 10
85+
if (q.d != 10) return "fail q.d +=: ${q.d}"
86+
q.d *= 10
87+
if (q.d != 100) return "fail q.d *=: ${q.d}"
88+
q.d /= 5
89+
if (q.d != 20) return "fail q.d /=: ${q.d}"
90+
q.d -= 10
91+
if (q.d != 10) return "fail q.d -=: ${q.d}"
92+
q.d %= 7
93+
if (q.d != 3) return "fail q.d %=: ${q.d}"
94+
95+
a = 0
96+
if (0.e++ != 0) return "fail 0.e++: ${0.e}"
97+
if (++0.e != 2) return "fail ++0.e: ${0.e}"
98+
if (--0.e != 1) return "fail --0.e: ${0.e}"
99+
if (0.e-- != 1) return "fail 0.e--: ${0.e}"
100+
0.e += 10
101+
if (0.e != 10) return "fail 0.e +=: ${0.e}"
102+
0.e *= 10
103+
if (0.e != 100) return "fail 0.e *=: ${0.e}"
104+
0.e /= 5
105+
if (0.e != 20) return "fail 0.e /=: ${0.e}"
106+
0.e -= 10
107+
if (0.e != 10) return "fail 0.e -=: ${0.e}"
108+
0.e %= 7
109+
if (0.e != 3) return "fail 0.e %=: ${0.e}"
110+
111+
a = 0
112+
if (f++ != 0) return "fail f++: $f"
113+
if (++f != 2) return "fail ++f: $f"
114+
if (--f != 1) return "fail --f: $f"
115+
if (f-- != 1) return "fail f--: $f"
116+
f += 10
117+
if (f != 10) return "fail f +=: $f"
118+
f *= 10
119+
if (f != 100) return "fail f *=: $f"
120+
f /= 5
121+
if (f != 20) return "fail f /=: $f"
122+
f -= 10
123+
if (f != 10) return "fail f -=: $f"
124+
f %= 7
125+
if (f != 3) return "fail f %=: $f"
126+
127+
128+
var g by SimpleDelegate
129+
130+
a = 0
131+
if (g++ != 0) return "fail g++: $g"
132+
if (++g != 2) return "fail ++g: $g"
133+
if (--g != 1) return "fail --g: $g"
134+
if (g-- != 1) return "fail g--: $g"
135+
g += 10
136+
if (g != 10) return "fail g +=: $g"
137+
g *= 10
138+
if (g != 100) return "fail g *=: $g"
139+
g /= 5
140+
if (g != 20) return "fail g /=: $g"
141+
g -= 10
142+
if (g != 10) return "fail g -=: $g"
143+
g %= 7
144+
if (g != 3) return "fail g %=: $g"
145+
146+
return "OK"
147+
}

compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10895,6 +10895,18 @@ public void testAllFilesPresentInProperties() throws Exception {
1089510895
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
1089610896
}
1089710897

10898+
@TestMetadata("augmentedAssignmentsAndIncrements.kt")
10899+
public void testAugmentedAssignmentsAndIncrements() throws Exception {
10900+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/augmentedAssignmentsAndIncrements.kt");
10901+
try {
10902+
doTest(fileName);
10903+
}
10904+
catch (Throwable ignore) {
10905+
return;
10906+
}
10907+
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
10908+
}
10909+
1089810910
@TestMetadata("classArtificialFieldInsideNested.kt")
1089910911
public void testClassArtificialFieldInsideNested() throws Exception {
1090010912
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/classArtificialFieldInsideNested.kt");

compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10895,6 +10895,18 @@ public void testAllFilesPresentInProperties() throws Exception {
1089510895
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
1089610896
}
1089710897

10898+
@TestMetadata("augmentedAssignmentsAndIncrements.kt")
10899+
public void testAugmentedAssignmentsAndIncrements() throws Exception {
10900+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/augmentedAssignmentsAndIncrements.kt");
10901+
try {
10902+
doTest(fileName);
10903+
}
10904+
catch (Throwable ignore) {
10905+
return;
10906+
}
10907+
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
10908+
}
10909+
1089810910
@TestMetadata("classArtificialFieldInsideNested.kt")
1089910911
public void testClassArtificialFieldInsideNested() throws Exception {
1090010912
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/classArtificialFieldInsideNested.kt");

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13223,6 +13223,12 @@ public void testAllFilesPresentInProperties() throws Exception {
1322313223
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
1322413224
}
1322513225

13226+
@TestMetadata("augmentedAssignmentsAndIncrements.kt")
13227+
public void testAugmentedAssignmentsAndIncrements() throws Exception {
13228+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/augmentedAssignmentsAndIncrements.kt");
13229+
doTest(fileName);
13230+
}
13231+
1322613232
@TestMetadata("classArtificialFieldInsideNested.kt")
1322713233
public void testClassArtificialFieldInsideNested() throws Exception {
1322813234
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/classArtificialFieldInsideNested.kt");

js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/IntrinsicAssignmentTranslator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
2525
import org.jetbrains.kotlin.js.translate.reference.AccessTranslator;
26-
import org.jetbrains.kotlin.js.translate.utils.TranslationUtils;
2726
import org.jetbrains.kotlin.lexer.KtToken;
2827
import org.jetbrains.kotlin.psi.KtBinaryExpression;
2928
import org.jetbrains.kotlin.types.expressions.OperatorConventions;

js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/TranslationUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ public static boolean isSimpleNameExpressionNotDelegatedLocalVar(@Nullable KtExp
275275
return false;
276276
}
277277
DeclarationDescriptor descriptor = context.bindingContext().get(BindingContext.REFERENCE_TARGET, ((KtSimpleNameExpression) expression));
278-
return !(descriptor instanceof LocalVariableDescriptor) || !((LocalVariableDescriptor) descriptor).isDelegated();
278+
return !((descriptor instanceof LocalVariableDescriptor) && ((LocalVariableDescriptor) descriptor).isDelegated()) &&
279+
!((descriptor instanceof PropertyDescriptor) && propertyAccessedByFunctionsInternally((PropertyDescriptor) descriptor, context));
280+
}
281+
282+
private static boolean propertyAccessedByFunctionsInternally(@NotNull PropertyDescriptor p, @NotNull TranslationContext context) {
283+
return !JsDescriptorUtils.isSimpleFinalProperty(p) && context.isFromCurrentModule(p) || shouldAccessViaFunctions(p);
279284
}
280285

281286
public static boolean shouldAccessViaFunctions(@NotNull CallableDescriptor descriptor) {

0 commit comments

Comments
 (0)