Fix bug when boxing primitive value inside lambda to supertype (e.g. java.lang.Object) of corresponding wrapper type

This commit is contained in:
Alexey Andreev 2019-11-18 12:34:55 +03:00
parent 631111bdb3
commit d71aec7c40
2 changed files with 21 additions and 0 deletions

View File

@ -748,6 +748,9 @@ public class ValueEmitter {
if (!pe.hierarchy.isSuperType(targetClass, boxClassName, false)) {
throw new EmitException("Can't convert " + this.type + " to " + targetClass);
}
if (!result.type.equals(type)) {
result.type = type;
}
return result;
}

View File

@ -18,6 +18,7 @@ package org.teavm.vm;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.Serializable;
import java.util.function.IntPredicate;
import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -41,6 +42,23 @@ public class LambdaTest {
assertTrue("Supplier is expected to implement Serializable", supplier instanceof Serializable);
}
@Test
public void boxParameterToSupertype() {
assertEquals(".*.*.*.*.*", acceptIntPredicate(this::oddPredicate));
}
private String acceptIntPredicate(IntPredicate p) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; ++i) {
sb.append(p.test(i) ? '*' : '.');
}
return sb.toString();
}
private boolean oddPredicate(Object o) {
return o instanceof Integer && ((Integer) o) % 2 != 0;
}
interface A {
}
}