JVM_IR fix 'remove' in inline class implementing MutableCollection

This commit is contained in:
Dmitry Petrov
2020-10-15 16:43:48 +03:00
parent 95edcea9a9
commit ee5edf4caa
31 changed files with 738 additions and 25 deletions
@@ -0,0 +1,99 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: javaCollectionWithRemovePrimitiveInt.kt
fun box(): String {
val j = JIntCollection(arrayListOf(1, 2, 3))
j.remove(1) // remove(int)
if (j.removed != 1) throw AssertionError("${j.removed}")
return "OK"
}
// FILE: JIntCollection.java
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Iterator;
public class JIntCollection implements Collection<Integer> {
private final Collection<Integer> collection;
public int removed = 0;
public JIntCollection(Collection<Integer> collection) {
this.collection = collection;
}
@Override
public int size() {
return collection.size();
}
@Override
public boolean isEmpty() {
return collection.isEmpty();
}
@Override
public boolean contains(Object o) {
return collection.contains(o);
}
@NotNull
@Override
public Iterator<Integer> iterator() {
return collection.iterator();
}
@NotNull
@Override
public Object[] toArray() {
return collection.toArray();
}
@NotNull
@Override
public <T> T[] toArray(@NotNull T[] a) {
return collection.toArray(a);
}
@Override
public boolean add(Integer integer) {
return collection.add(integer);
}
@Override
public boolean remove(Object o) {
return collection.remove(o);
}
public boolean remove(int x) {
removed = x;
return true;
}
@Override
public boolean containsAll(@NotNull Collection<?> c) {
return collection.containsAll(c);
}
@Override
public boolean addAll(@NotNull Collection<? extends Integer> c) {
return collection.addAll(c);
}
@Override
public boolean removeAll(@NotNull Collection<?> c) {
return collection.removeAll(c);
}
@Override
public boolean retainAll(@NotNull Collection<?> c) {
return collection.retainAll(c);
}
@Override
public void clear() {
collection.clear();
}
}
@@ -0,0 +1,48 @@
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
inline class Z(val x: Int)
inline class Z2(val x: Z)
fun z2(x: Int) = Z2(Z(x))
inline class ZMutableCollection(private val ms: MutableCollection<Z>) : MutableCollection<Z> {
override fun add(element: Z): Boolean = ms.add(element)
override fun addAll(elements: Collection<Z>): Boolean = ms.addAll(elements)
override fun clear() { ms.clear() }
override fun iterator(): MutableIterator<Z> = ms.iterator()
override fun remove(element: Z): Boolean = ms.remove(element)
override fun removeAll(elements: Collection<Z>): Boolean = ms.removeAll(elements)
override fun retainAll(elements: Collection<Z>): Boolean = ms.retainAll(elements)
override val size: Int get() = ms.size
override fun contains(element: Z): Boolean = ms.contains(element)
override fun containsAll(elements: Collection<Z>): Boolean = ms.containsAll(elements)
override fun isEmpty(): Boolean = ms.isEmpty()
}
inline class Z2MutableCollection(private val ms: MutableCollection<Z2>) : MutableCollection<Z2> {
override fun add(element: Z2): Boolean = ms.add(element)
override fun addAll(elements: Collection<Z2>): Boolean = ms.addAll(elements)
override fun clear() { ms.clear() }
override fun iterator(): MutableIterator<Z2> = ms.iterator()
override fun remove(element: Z2): Boolean = ms.remove(element)
override fun removeAll(elements: Collection<Z2>): Boolean = ms.removeAll(elements)
override fun retainAll(elements: Collection<Z2>): Boolean = ms.retainAll(elements)
override val size: Int get() = ms.size
override fun contains(element: Z2): Boolean = ms.contains(element)
override fun containsAll(elements: Collection<Z2>): Boolean = ms.containsAll(elements)
override fun isEmpty(): Boolean = ms.isEmpty()
}
fun box(): String {
val zc1 = ZMutableCollection(mutableListOf(Z(1), Z(2), Z(3)))
zc1.remove(Z(1))
if (Z(1) in zc1) throw AssertionError("Z(1) in zc1")
val zc2 = Z2MutableCollection(mutableListOf(z2(1), z2(2), z2(3)))
zc2.remove(z2(1))
if (z2(1) in zc2) throw AssertionError("z2(1) in zc2")
return "OK"
}