Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

top-level function declaration type fixes. #339

Merged
merged 5 commits into from
Oct 11, 2023
Merged

Conversation

traceyyoshima
Copy link
Contributor

@traceyyoshima traceyyoshima commented Oct 10, 2023

Changes:

  • Function signature and types will contain the correct type from a ConeClassLikeType instead of JavaType.Unknown.
  • Added FirFileSymbol as field to SignatureBuilder and TypeMapping to cover top-level declarations.
  • Fixed signature and type for the owner of JavaType$Variable from top-level method declaration method parameters.
  • Kotlin generic bounds in and out {type} will contain the equivalent java name ?
  • ConeIntersectionType will return a generic with the correct bounds instead of JavaType.Unknown.

@@ -58,15 +58,17 @@ import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.convertFileNa
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.convertKotlinFqToJavaFq

@Incubating(since = "0.0")
class KotlinTypeMapping(typeCache: JavaTypeCache, firSession: FirSession) : JavaTypeMapping<Any> {
class KotlinTypeMapping(typeCache: JavaTypeCache, firSession: FirSession, firFileSymbol: FirFileSymbol) : JavaTypeMapping<Any> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ownerStack will be wrong without access to the file when a SimpleFunction is on the top of the stack for the parameters in a top-level method declaration.

@@ -546,7 +550,7 @@ class KotlinTypeMapping(typeCache: JavaTypeCache, firSession: FirSession) : Java
}
}
if (resolvedDeclaringType == null) {
return null
resolvedDeclaringType = TypeUtils.asFullyQualified(type(firFileSymbol.fir))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knutwannheden I needed to figure out where we should fall back to the FirFileSymbol. This "could" technically set the incorrect owner if we don't assign the owner appropriately, but it also isolates incorrect types to each file.

Curious, what do you think about falling back onto the FirFileSymbol by default?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what do you think about falling back onto the FirFileSymbol by default?

I think that sounds reasonable. I wanted to do something similar myself, but first wanted to discuss that with you. So instead I added this owner stack, which is easy enough to remove again.

@@ -882,6 +886,8 @@ class KotlinTypeMapping(typeCache: JavaTypeCache, firSession: FirSession) : Java
// TODO: fix type attribution for anonymous objects. Currently, returns JavaType.Unknown.
if (symbol.dispatchReceiverType != null) {
resolvedOwner = type(symbol.dispatchReceiverType)
} else if (ownerFallBack is FirFunctionSymbol<*>) {
resolvedOwner = methodDeclarationType(ownerFallBack.fir, null, firFileSymbol)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature is set correctly to the method declaration signature. This change fixes the JavaType returned by the signature.

Added FirFileSymbol as field to SignatureBuilder and TypeMapping.
Fixed owner of top-level method declarations of method parameters.
Fixed owner type of method parameters.
@traceyyoshima traceyyoshima force-pushed the top-level-method-type-fixes branch from c9d5124 to adacc44 Compare October 10, 2023 22:29
@traceyyoshima traceyyoshima marked this pull request as ready for review October 10, 2023 22:34
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
if (methodMatcher.matches(method)) {
assertThat(method.getMethodType().toString())
.isEqualTo("kotlin.collections.CollectionsKt{name=listOf,return=kotlin.collections.List<kotlin.Pair<kotlin.String, Generic{kotlin.Comparable<Generic{*}> & java.io.Serializable}>>,parameters=[kotlin.Array<Generic{? extends Generic{T}}>]}");
Copy link
Contributor Author

@traceyyoshima traceyyoshima Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so you know -- In Java, a generic type intersection contains the bounded generic name like T, V, U. So the type becomes Generic{<name> extends <typeA> & <typeB>}.

In Kotlin, the ConeIntersectionType contains a generic name it and appears as it(<typeA>, <typeB>). So, I left the name out. It's better if we add the name of the Generic type variable. So, I'll check on that tomorrow.

Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Possibly we can now again remove this owner stack, which I think is only necessary for top-level declarations and also variables inside top-level functions, IIRC.

@kunli2 kunli2 merged commit f998075 into main Oct 11, 2023
@kunli2 kunli2 deleted the top-level-method-type-fixes branch October 11, 2023 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants