TypeCastException when casting null to T with nullable upper-bound
#KT-3637 Fixed
This commit is contained in:
@@ -3513,7 +3513,7 @@ The "returned" value of try expression with no finally is either the last expres
|
|||||||
value.put(boxType(value.type), v);
|
value.put(boxType(value.type), v);
|
||||||
|
|
||||||
if (opToken != JetTokens.AS_SAFE) {
|
if (opToken != JetTokens.AS_SAFE) {
|
||||||
if (!rightType.isNullable()) {
|
if (!CodegenUtil.isNullableType(rightType)) {
|
||||||
v.dup();
|
v.dup();
|
||||||
Label nonnull = new Label();
|
Label nonnull = new Label();
|
||||||
v.ifnonnull(nonnull);
|
v.ifnonnull(nonnull);
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
fun test1<T>() = null as T
|
||||||
|
fun test2<T>(): T {
|
||||||
|
val a : Any? = null
|
||||||
|
return a as T
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test3<T: Any>() = null as T
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
if (test1<Int?>() != null) return "fail: test1"
|
||||||
|
if (test2<Int?>() != null) return "fail: test2"
|
||||||
|
var result3 = "fail"
|
||||||
|
try {
|
||||||
|
test3<Int>()
|
||||||
|
}
|
||||||
|
catch(e: TypeCastException) {
|
||||||
|
result3 = "OK"
|
||||||
|
}
|
||||||
|
if (result3 != "OK") return "fail: test3"
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+15
-1
@@ -31,12 +31,25 @@ import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
|
|||||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib")
|
@TestMetadata("compiler/testData/codegen/boxWithStdlib")
|
||||||
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class})
|
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.Casts.class, BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class})
|
||||||
public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||||
public void testAllFilesPresentInBoxWithStdlib() throws Exception {
|
public void testAllFilesPresentInBoxWithStdlib() throws Exception {
|
||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithStdlib"), Pattern.compile("^(.+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithStdlib"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("compiler/testData/codegen/boxWithStdlib/casts")
|
||||||
|
public static class Casts extends AbstractBlackBoxCodegenTest {
|
||||||
|
public void testAllFilesPresentInCasts() throws Exception {
|
||||||
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithStdlib/casts"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asWithGeneric.kt")
|
||||||
|
public void testAsWithGeneric() throws Exception {
|
||||||
|
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/casts/asWithGeneric.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/codegen/boxWithStdlib/dataClasses")
|
@TestMetadata("compiler/testData/codegen/boxWithStdlib/dataClasses")
|
||||||
@InnerTestClasses({DataClasses.Copy.class, DataClasses.Equals.class, DataClasses.Hashcode.class, DataClasses.Tostring.class})
|
@InnerTestClasses({DataClasses.Copy.class, DataClasses.Equals.class, DataClasses.Hashcode.class, DataClasses.Tostring.class})
|
||||||
public static class DataClasses extends AbstractBlackBoxCodegenTest {
|
public static class DataClasses extends AbstractBlackBoxCodegenTest {
|
||||||
@@ -749,6 +762,7 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
|||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite("BlackBoxWithStdlibCodegenTestGenerated");
|
TestSuite suite = new TestSuite("BlackBoxWithStdlibCodegenTestGenerated");
|
||||||
suite.addTestSuite(BlackBoxWithStdlibCodegenTestGenerated.class);
|
suite.addTestSuite(BlackBoxWithStdlibCodegenTestGenerated.class);
|
||||||
|
suite.addTestSuite(Casts.class);
|
||||||
suite.addTest(DataClasses.innerSuite());
|
suite.addTest(DataClasses.innerSuite());
|
||||||
suite.addTestSuite(FullJdk.class);
|
suite.addTestSuite(FullJdk.class);
|
||||||
suite.addTestSuite(JdkAnnotations.class);
|
suite.addTestSuite(JdkAnnotations.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user