[FIR] Support captured type parameters in java symbol provider

This commit is contained in:
simon.ogorodnik
2020-04-09 19:02:23 +03:00
parent af27ca013b
commit 276c393141
9 changed files with 53 additions and 89 deletions
@@ -305,7 +305,7 @@ class Fir2IrDeclarationStorage(
parentPropertyReceiverType: FirTypeRef?
) {
val parent = this
if (function is FirSimpleFunction) {
if (function is FirSimpleFunction || function is FirConstructor) {
with(classifierStorage) {
setTypeParameters(function)
}
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
import org.jetbrains.kotlin.fir.declarations.addDefaultBoundIfNecessary
import org.jetbrains.kotlin.fir.declarations.builder.FirTypeParameterBuilder
import org.jetbrains.kotlin.fir.declarations.builder.buildConstructedClassTypeParameterRef
import org.jetbrains.kotlin.fir.declarations.builder.buildOuterClassTypeParameterRef
import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotationCall
import org.jetbrains.kotlin.fir.java.declarations.*
import org.jetbrains.kotlin.fir.resolve.AbstractFirSymbolProvider
@@ -152,13 +153,13 @@ class JavaSymbolProvider(
) { firSymbol, foundClass ->
foundClass?.let { javaClass ->
val javaTypeParameterStack = JavaTypeParameterStack()
val parentFqName = classId.relativeClassName.parent()
val isTopLevel = parentFqName.isRoot
if (!isTopLevel) {
val parentId = ClassId(classId.packageFqName, parentFqName, false)
val parentClassSymbol = getClassLikeSymbolByFqName(parentId)
val outerClassId = classId.outerClassId
val parentClassSymbol = if (outerClassId != null) {
getClassLikeSymbolByFqName(outerClassId)
} else null
if (parentClassSymbol != null) {
val parentStack = parentClassTypeParameterStackCache[parentClassSymbol]
?: (parentClassSymbol?.fir as? FirJavaClass)?.javaTypeParameterStack
?: (parentClassSymbol.fir as? FirJavaClass)?.javaTypeParameterStack
if (parentStack != null) {
javaTypeParameterStack.addStack(parentStack)
}
@@ -171,13 +172,19 @@ class JavaSymbolProvider(
visibility = javaClass.visibility
modality = javaClass.modality
classKind = javaClass.classKind
this.isTopLevel = isTopLevel
this.isTopLevel = outerClassId == null
isStatic = javaClass.isStatic
this.javaTypeParameterStack = javaTypeParameterStack
parentClassTypeParameterStackCache[firSymbol] = javaTypeParameterStack
existingNestedClassifierNames += javaClass.innerClassNames
scopeProvider = this@JavaSymbolProvider.scopeProvider
typeParameters += foundClass.typeParameters.convertTypeParameters(javaTypeParameterStack)
val classTypeParameters = foundClass.typeParameters.convertTypeParameters(javaTypeParameterStack)
typeParameters += classTypeParameters
if (!isStatic && parentClassSymbol != null) {
typeParameters += parentClassSymbol.fir.typeParameters.map {
buildOuterClassTypeParameterRef { symbol = it.symbol }
}
}
addAnnotationsFrom(this@JavaSymbolProvider.session, javaClass, javaTypeParameterStack)
// TODO: may be we can process fields & methods later.
// However, they should be built up to override resolve stage
@@ -234,7 +241,6 @@ class JavaSymbolProvider(
isPrimary: Boolean = false,
): FirJavaConstructorBuilder {
val constructorSymbol = FirConstructorSymbol(constructorId)
val classTypeParameters = javaClass.typeParameters.convertTypeParameters(javaTypeParameterStack)
return FirJavaConstructorBuilder().apply {
source = psi?.toFirPsiSourceElement()
session = this@JavaSymbolProvider.session
@@ -244,7 +250,7 @@ class JavaSymbolProvider(
isInner = javaClass.outerClass != null && !javaClass.isStatic
returnTypeRef = buildResolvedTypeRef {
type = firSymbol.constructType(
classTypeParameters.map { ConeTypeParameterTypeImpl(it.symbol.toLookupTag(), false) }.toTypedArray(),
this@buildJavaClass.typeParameters.map { ConeTypeParameterTypeImpl(it.symbol.toLookupTag(), false) }.toTypedArray(),
false,
)
}
@@ -1,28 +0,0 @@
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER
// FILE: Outer.java
public class Outer<E> {
public class Inner<F> {
E foo() {}
F bar() {}
Outer<E> outer() {}
}
Inner<E> baz() { }
void set(Inner<String> x) {}
}
// FILE: main.kt
fun main() {
var outerStr: Outer<String> = Outer()
outerStr.baz().checkType { <!INAPPLICABLE_CANDIDATE!>_<!><Outer<String>.Inner<String>>() }
val strInt: Outer<String>.Inner<Int> = outerStr.Inner()
strInt.foo().checkType { <!INAPPLICABLE_CANDIDATE!>_<!><String>() }
strInt.bar().checkType { _<Int>() }
strInt.outer().checkType { <!INAPPLICABLE_CANDIDATE!>_<!><Outer<String>>() }
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !CHECK_TYPE
// !DIAGNOSTICS: -UNUSED_EXPRESSION -UNUSED_PARAMETER
// FILE: Outer.java
@@ -1,35 +0,0 @@
// !CHECK_TYPE
// FILE: BaseOuter.java
// See KT-10285
public class BaseOuter<H> {
abstract public class BaseInner<E, F> {
public H foo1() {
return null;
}
public E foo2() {
return null;
}
public F foo3() {
return null;
}
}
}
// FILE: Outer.java
public class Outer<H> extends BaseOuter<H> {
public BaseInner<Double, String> bar() { return null; }
public class Inner extends BaseOuter<H>.BaseInner<Double, String> {}
}
// FILE: main.kt
fun foo(x1: Outer<Int>, x2: Outer<Int>.Inner) {
x1.bar().foo1().checkType { <!INAPPLICABLE_CANDIDATE!>_<!><Int>() }
x1.bar().foo2().checkType { _<Double>() }
x1.bar().foo3().checkType { _<String>() }
x2.foo1().checkType { <!INAPPLICABLE_CANDIDATE!>_<!><Int>() }
x2.foo2().checkType { _<Double>() }
x2.foo3().checkType { _<String>() }
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !CHECK_TYPE
// FILE: BaseOuter.java
// See KT-10285
@@ -6,10 +6,15 @@ FILE fqName:<root> fileName:/constructorWithOwnTypeParametersCall.kt
<class: T2>: kotlin.String
$outer: CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.K1' type=<root>.K1<kotlin.Int> origin=null
<class: T1>: kotlin.Int
FUN name:testJava visibility:public modality:FINAL <> () returnType:<root>.J1.J2<kotlin.Double?>
FUN name:testJava visibility:public modality:FINAL <> () returnType:<root>.J1.J2<kotlin.Double?, kotlin.Int?>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun testJava (): <root>.J1.J2<kotlin.Double?> declared in <root>'
ERROR_EXPR 'Cannot bind 2 type arguments to ??? call with 1 type parameters' type=<root>.J1.J2<kotlin.Double?>
RETURN type=kotlin.Nothing from='public final fun testJava (): <root>.J1.J2<kotlin.Double?, kotlin.Int?> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> <Y2> () declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Double?, kotlin.Int?> origin=null
<class: X2>: kotlin.Double?
<Y2>: kotlin.CharSequence?
$outer: CONSTRUCTOR_CALL 'public constructor <init> <Y1> () declared in <root>.J1' type=<root>.J1<kotlin.Int?> origin=null
<class: X1>: kotlin.Int?
<Y1>: kotlin.String?
CLASS CLASS name:K1 modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.K1<T1 of <root>.K1>
TYPE_PARAMETER name:T1 index:0 variance: superTypes:[kotlin.Number]
@@ -1,10 +1,10 @@
FILE fqName:<root> fileName:/samConversionInGenericConstructorCall_NI.kt
FUN name:test3 visibility:public modality:FINAL <> (f1:kotlin.Function1<kotlin.String, kotlin.String>, f2:kotlin.Function1<kotlin.Int, kotlin.String>) returnType:<root>.C.D<kotlin.Int?>
FUN name:test3 visibility:public modality:FINAL <> (f1:kotlin.Function1<kotlin.String, kotlin.String>, f2:kotlin.Function1<kotlin.Int, kotlin.String>) returnType:<root>.C.D<kotlin.Int?, kotlin.String?>
VALUE_PARAMETER name:f1 index:0 type:kotlin.Function1<kotlin.String, kotlin.String>
VALUE_PARAMETER name:f2 index:1 type:kotlin.Function1<kotlin.Int, kotlin.String>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test3 (f1: kotlin.Function1<kotlin.String, kotlin.String>, f2: kotlin.Function1<kotlin.Int, kotlin.String>): <root>.C.D<kotlin.Int?> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> (jxy: <root>.J<X of <root>.C?, Y of <root>.C.D?>?) declared in <root>.C.D' type=<root>.C.D<kotlin.Int?> origin=null
RETURN type=kotlin.Nothing from='public final fun test3 (f1: kotlin.Function1<kotlin.String, kotlin.String>, f2: kotlin.Function1<kotlin.Int, kotlin.String>): <root>.C.D<kotlin.Int?, kotlin.String?> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> (jxy: <root>.J<X of <root>.C?, Y of <root>.C.D?>?) declared in <root>.C.D' type=<root>.C.D<kotlin.Int?, kotlin.String?> origin=null
<class: Y>: kotlin.Int?
$outer: CONSTRUCTOR_CALL 'public constructor <init> (jxx: <root>.J<X of <root>.C?, X of <root>.C?>?) declared in <root>.C' type=<root>.C<kotlin.String?> origin=null
<class: X>: kotlin.String?
@@ -90,10 +90,17 @@ FILE fqName:<root> fileName:/samConversionInGenericConstructorCall_NI.kt
VALUE_PARAMETER name:f index:0 type:kotlin.Function1<kotlin.String, kotlin.Int>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun testGenericJavaCtor1 (f: kotlin.Function1<kotlin.String, kotlin.Int>): <root>.G<kotlin.String?> declared in <root>'
ERROR_EXPR 'Cannot bind 2 type arguments to ??? call with 1 type parameters' type=<root>.G<kotlin.String?>
CONSTRUCTOR_CALL 'public constructor <init> <TCtor> (x: <root>.J<TCtor of <root>.G.<init>?, TClass of <root>.G?>?) declared in <root>.G' type=<root>.G<kotlin.String?> origin=null
<class: TClass>: kotlin.String?
<TCtor>: kotlin.Int?
x: GET_VAR 'f: kotlin.Function1<kotlin.String, kotlin.Int> declared in <root>.testGenericJavaCtor1' type=kotlin.Function1<kotlin.String, kotlin.Int> origin=null
FUN name:testGenericJavaCtor2 visibility:public modality:FINAL <> (x:kotlin.Any) returnType:kotlin.Unit
VALUE_PARAMETER name:x index:0 type:kotlin.Any
BLOCK_BODY
TYPE_OP type=kotlin.Function1<kotlin.String, kotlin.Int> origin=CAST typeOperand=kotlin.Function1<kotlin.String, kotlin.Int>
GET_VAR 'x: kotlin.Any declared in <root>.testGenericJavaCtor2' type=kotlin.Any origin=null
ERROR_EXPR 'Cannot bind 2 type arguments to ??? call with 1 type parameters' type=<root>.G<kotlin.String?>
CONSTRUCTOR_CALL 'public constructor <init> <TCtor> (x: <root>.J<TCtor of <root>.G.<init>?, TClass of <root>.G?>?) declared in <root>.G' type=<root>.G<kotlin.String?> origin=null
<class: TClass>: kotlin.String?
<TCtor>: kotlin.Int?
x: TYPE_OP type=kotlin.Function1<kotlin.String, kotlin.Int> origin=IMPLICIT_CAST typeOperand=kotlin.Function1<kotlin.String, kotlin.Int>
GET_VAR 'x: kotlin.Any declared in <root>.testGenericJavaCtor2' type=kotlin.Any origin=null
@@ -7,16 +7,23 @@ FILE fqName:<root> fileName:/javaConstructorWithTypeParameters.kt
FUN name:test2 visibility:public modality:FINAL <> () returnType:<root>.J1<kotlin.Int?>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (): <root>.J1<kotlin.Int?> declared in <root>'
ERROR_EXPR 'Cannot bind 2 type arguments to ??? call with 1 type parameters' type=<root>.J1<kotlin.Int?>
FUN name:test3 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int?>
CONSTRUCTOR_CALL 'public constructor <init> <X1> (x1: X1 of <root>.J1.<init>?) declared in <root>.J1' type=<root>.J1<kotlin.Int?> origin=null
<class: T1>: kotlin.Int?
<X1>: kotlin.Int?
x1: CONST Int type=kotlin.Int value=1
FUN name:test3 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int?, kotlin.Any>
VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test3 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int?> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Int?> origin=null
RETURN type=kotlin.Nothing from='public final fun test3 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int?, kotlin.Any> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Int?, kotlin.Any> origin=null
<class: T2>: kotlin.Int?
$outer: GET_VAR 'j1: <root>.J1<kotlin.Any> declared in <root>.test3' type=<root>.J1<kotlin.Any> origin=null
FUN name:test4 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int?>
FUN name:test4 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int?, kotlin.Any>
VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test4 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int?> declared in <root>'
ERROR_EXPR 'Cannot bind 2 type arguments to ??? call with 1 type parameters' type=<root>.J1.J2<kotlin.Int?>
RETURN type=kotlin.Nothing from='public final fun test4 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int?, kotlin.Any> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> <X2> (x2: X2 of <root>.J1.J2.<init>?) declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Int?, kotlin.Any> origin=null
<class: T2>: kotlin.Int?
<X2>: kotlin.Int?
$outer: GET_VAR 'j1: <root>.J1<kotlin.Any> declared in <root>.test4' type=<root>.J1<kotlin.Any> origin=null
x2: CONST Int type=kotlin.Int value=1