Introduced simple propagation of nullability on loading Java.

#KT-2776 in progress
This commit is contained in:
Evgeny Gerashchenko
2012-11-02 15:12:24 +04:00
parent 961fde3c8a
commit 1f4d994480
21 changed files with 367 additions and 2 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.resolve.java.resolver;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.psi.HierarchicalMethodSignature;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;
@@ -135,6 +136,20 @@ public final class JavaFunctionResolver {
.resolveParameterDescriptors(functionDescriptorImpl, method.getParameters(), methodTypeVariableResolver);
JetType returnType = makeReturnType(returnPsiType, method, methodTypeVariableResolver);
Set<JetType> typesFromSuperMethods = Sets.newHashSet();
for (HierarchicalMethodSignature superSignature : method.getPsiMethod().getHierarchicalMethodSignature().getSuperSignatures()) {
DeclarationDescriptor superFun = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, superSignature.getMethod());
if (superFun instanceof FunctionDescriptor) {
typesFromSuperMethods.add(((FunctionDescriptor) superFun).getReturnType());
}
}
typesFromSuperMethods.add(returnType);
JetType intersectionType = TypeUtils.intersect(JetTypeChecker.INSTANCE, typesFromSuperMethods);
if (intersectionType != null && intersectionType.getConstructor().getDeclarationDescriptor() != null) {
returnType = intersectionType;
}
// TODO consider better place for this check
AlternativeMethodSignatureData alternativeMethodSignatureData =
new AlternativeMethodSignatureData(method, valueParameterDescriptors, returnType, methodTypeParameters);
@@ -0,0 +1,19 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class AddNotNullJavaSubtype {
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends AddNotNullJavaSubtype {
@NotNull
public String foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class AddNotNullJavaSubtype : java.lang.Object() {
public open fun foo(): CharSequence? = throw UnsupportedOperationException()
public open class Sub: AddNotNullJavaSubtype() {
override fun foo(): String = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.AddNotNullJavaSubtype : java.lang.Object {
public final /*constructor*/ fun <init>(): test.AddNotNullJavaSubtype
public open fun foo(): jet.CharSequence?
public open class test.AddNotNullJavaSubtype.Sub : test.AddNotNullJavaSubtype {
public final /*constructor*/ fun <init>(): test.AddNotNullJavaSubtype.Sub
public open override /*1*/ fun foo(): jet.String
}
}
@@ -0,0 +1,19 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class AddNotNullSameJavaType {
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends AddNotNullSameJavaType {
@NotNull
public CharSequence foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class AddNotNullSameJavaType : java.lang.Object() {
public open fun foo(): CharSequence? = throw UnsupportedOperationException()
public open class Sub: AddNotNullSameJavaType() {
override fun foo(): CharSequence = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.AddNotNullSameJavaType : java.lang.Object {
public final /*constructor*/ fun <init>(): test.AddNotNullSameJavaType
public open fun foo(): jet.CharSequence?
public open class test.AddNotNullSameJavaType.Sub : test.AddNotNullSameJavaType {
public final /*constructor*/ fun <init>(): test.AddNotNullSameJavaType.Sub
public open override /*1*/ fun foo(): jet.CharSequence
}
}
@@ -0,0 +1,20 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class AddNullabilityJavaSubtype {
@NotNull
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends AddNullabilityJavaSubtype {
@KotlinSignature("fun String? foo()")
public String foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class AddNullabilityJavaSubtype : java.lang.Object() {
public open fun foo(): CharSequence = throw UnsupportedOperationException()
public open class Sub: AddNullabilityJavaSubtype() {
override fun foo(): String = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.AddNullabilityJavaSubtype : java.lang.Object {
public final /*constructor*/ fun <init>(): test.AddNullabilityJavaSubtype
public open fun foo(): jet.CharSequence
public open class test.AddNullabilityJavaSubtype.Sub : test.AddNullabilityJavaSubtype {
public final /*constructor*/ fun <init>(): test.AddNullabilityJavaSubtype.Sub
public open override /*1*/ fun foo(): jet.String
}
}
@@ -0,0 +1,20 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class AddNullabilitySameJavaType {
@NotNull
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends AddNullabilitySameJavaType {
@KotlinSignature("fun CharSequence? foo()")
public CharSequence foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class AddNullabilitySameJavaType : java.lang.Object() {
public open fun foo(): CharSequence = throw UnsupportedOperationException()
public open class Sub: AddNullabilitySameJavaType() {
override fun foo(): CharSequence = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.AddNullabilitySameJavaType : java.lang.Object {
public final /*constructor*/ fun <init>(): test.AddNullabilitySameJavaType
public open fun foo(): jet.CharSequence
public open class test.AddNullabilitySameJavaType.Sub : test.AddNullabilitySameJavaType {
public final /*constructor*/ fun <init>(): test.AddNullabilitySameJavaType.Sub
public open override /*1*/ fun foo(): jet.CharSequence
}
}
@@ -0,0 +1,19 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class InheritNullabilityJavaSubtype {
@NotNull
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends InheritNullabilityJavaSubtype {
public String foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class InheritNullabilityJavaSubtype : java.lang.Object() {
public open fun foo(): CharSequence = throw UnsupportedOperationException()
public open class Sub: InheritNullabilityJavaSubtype() {
override fun foo(): String = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.InheritNullabilityJavaSubtype : java.lang.Object {
public final /*constructor*/ fun <init>(): test.InheritNullabilityJavaSubtype
public open fun foo(): jet.CharSequence
public open class test.InheritNullabilityJavaSubtype.Sub : test.InheritNullabilityJavaSubtype {
public final /*constructor*/ fun <init>(): test.InheritNullabilityJavaSubtype.Sub
public open override /*1*/ fun foo(): jet.String
}
}
@@ -0,0 +1,19 @@
package test;
import org.jetbrains.annotations.NotNull;
import java.lang.CharSequence;
import jet.runtime.typeinfo.KotlinSignature;
public class InheritNullabilitySameJavaType {
@NotNull
public CharSequence foo() {
throw new UnsupportedOperationException();
}
public class Sub extends InheritNullabilitySameJavaType {
public CharSequence foo() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,11 @@
package test
import org.jetbrains.annotations.NotNull
public open class InheritNullabilitySameJavaType : java.lang.Object() {
public open fun foo(): CharSequence = throw UnsupportedOperationException()
public open class Sub: InheritNullabilitySameJavaType() {
override fun foo(): CharSequence = throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
namespace test
public open class test.InheritNullabilitySameJavaType : java.lang.Object {
public final /*constructor*/ fun <init>(): test.InheritNullabilitySameJavaType
public open fun foo(): jet.CharSequence
public open class test.InheritNullabilitySameJavaType.Sub : test.InheritNullabilitySameJavaType {
public final /*constructor*/ fun <init>(): test.InheritNullabilitySameJavaType.Sub
public open override /*1*/ fun foo(): jet.CharSequence
}
}
@@ -234,7 +234,7 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature")
@InnerTestClasses({KotlinSignature.Error.class})
@InnerTestClasses({KotlinSignature.Error.class, KotlinSignature.Propagation.class})
public static class KotlinSignature extends AbstractLoadJavaTest {
public void testAllFilesPresentInKotlinSignature() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature"), "java", true);
@@ -433,10 +433,64 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature/propagation")
@InnerTestClasses({Propagation.Return.class})
public static class Propagation extends AbstractLoadJavaTest {
public void testAllFilesPresentInPropagation() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation"), "java", true);
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature/propagation/return")
public static class Return extends AbstractLoadJavaTest {
@TestMetadata("AddNotNullJavaSubtype.java")
public void testAddNotNullJavaSubtype() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNotNullJavaSubtype.java");
}
@TestMetadata("AddNotNullSameJavaType.java")
public void testAddNotNullSameJavaType() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNotNullSameJavaType.java");
}
@TestMetadata("AddNullabilityJavaSubtype.java")
public void testAddNullabilityJavaSubtype() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNullabilityJavaSubtype.java");
}
@TestMetadata("AddNullabilitySameJavaType.java")
public void testAddNullabilitySameJavaType() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNullabilitySameJavaType.java");
}
public void testAllFilesPresentInReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation/return"), "java", true);
}
@TestMetadata("InheritNullabilityJavaSubtype.java")
public void testInheritNullabilityJavaSubtype() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilityJavaSubtype.java");
}
@TestMetadata("InheritNullabilitySameJavaType.java")
public void testInheritNullabilitySameJavaType() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilitySameJavaType.java");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Propagation");
suite.addTestSuite(Propagation.class);
suite.addTestSuite(Return.class);
return suite;
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("KotlinSignature");
suite.addTestSuite(KotlinSignature.class);
suite.addTestSuite(Error.class);
suite.addTest(Propagation.innerSuite());
return suite;
}
}
@@ -1129,7 +1129,7 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature")
@InnerTestClasses({KotlinSignature.Error.class})
@InnerTestClasses({KotlinSignature.Error.class, KotlinSignature.Propagation.class})
public static class KotlinSignature extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInKotlinSignature() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature"), "kt", true);
@@ -1328,10 +1328,64 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature/propagation")
@InnerTestClasses({Propagation.Return.class})
public static class Propagation extends AbstractLazyResolveNamespaceComparingTest {
public void testAllFilesPresentInPropagation() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation"), "kt", true);
}
@TestMetadata("compiler/testData/loadJava/kotlinSignature/propagation/return")
public static class Return extends AbstractLazyResolveNamespaceComparingTest {
@TestMetadata("AddNotNullJavaSubtype.kt")
public void testAddNotNullJavaSubtype() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNotNullJavaSubtype.kt");
}
@TestMetadata("AddNotNullSameJavaType.kt")
public void testAddNotNullSameJavaType() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNotNullSameJavaType.kt");
}
@TestMetadata("AddNullabilityJavaSubtype.kt")
public void testAddNullabilityJavaSubtype() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNullabilityJavaSubtype.kt");
}
@TestMetadata("AddNullabilitySameJavaType.kt")
public void testAddNullabilitySameJavaType() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/AddNullabilitySameJavaType.kt");
}
public void testAllFilesPresentInReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation/return"), "kt", true);
}
@TestMetadata("InheritNullabilityJavaSubtype.kt")
public void testInheritNullabilityJavaSubtype() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilityJavaSubtype.kt");
}
@TestMetadata("InheritNullabilitySameJavaType.kt")
public void testInheritNullabilitySameJavaType() throws Exception {
doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/InheritNullabilitySameJavaType.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Propagation");
suite.addTestSuite(Propagation.class);
suite.addTestSuite(Return.class);
return suite;
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("KotlinSignature");
suite.addTestSuite(KotlinSignature.class);
suite.addTestSuite(Error.class);
suite.addTest(Propagation.innerSuite());
return suite;
}
}