From 065aef581d57e227ff32e866326c4b4a7c9956be Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 12 Aug 2024 17:32:54 +0200 Subject: [PATCH] jso: fix incorrect type inference for array element types Fix #937 --- .../java/org/teavm/jso/impl/JSTypeInference.java | 3 +++ tests/src/test/java/org/teavm/vm/VMTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSTypeInference.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSTypeInference.java index 4985a5b49..81a8412b2 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSTypeInference.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSTypeInference.java @@ -82,6 +82,9 @@ class JSTypeInference extends BaseTypeInference { @Override protected JSType elementType(JSType jsType) { + if (jsType == JSType.NULL) { + return JSType.NULL; + } return jsType instanceof JSType.ArrayType ? ((JSType.ArrayType) jsType).elementType : JSType.MIXED; } diff --git a/tests/src/test/java/org/teavm/vm/VMTest.java b/tests/src/test/java/org/teavm/vm/VMTest.java index 69f4b72ad..c075b9829 100644 --- a/tests/src/test/java/org/teavm/vm/VMTest.java +++ b/tests/src/test/java/org/teavm/vm/VMTest.java @@ -657,6 +657,20 @@ public class VMTest { assertEquals("ap", callA(new B())); } + @Test + public void typeInferenceForArrayMerge() { + int[][] a = falseBoolean() ? null : array(); + assertEquals(23, a[0][0]); + } + + private boolean falseBoolean() { + return false; + } + + private int[][] array() { + return new int[][] { { 23 } }; + } + private static String callA(A a) { return a.a(); }