Skip to content

Commit

Permalink
Added support for ConeStubTypeForChainInference and ConeTypeParameter…
Browse files Browse the repository at this point in the history
…BasedTypeVariable of ConeTypeVariable in KotlinTypeSignatureBuilder. (#335)

fixes #303
  • Loading branch information
traceyyoshima authored Oct 9, 2023
1 parent 6e46b0e commit c83e881
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod
import org.jetbrains.kotlin.fir.java.declarations.FirJavaValueParameter
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable
import org.jetbrains.kotlin.fir.resolve.toFirRegularClass
import org.jetbrains.kotlin.fir.resolve.toFirRegularClassSymbol
import org.jetbrains.kotlin.fir.resolve.toSymbol
Expand Down Expand Up @@ -189,6 +190,14 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
is FirOuterClassTypeParameterRef -> {
return signature(type.symbol)
}

is ConeTypeVariable -> {
when (type) {
is ConeTypeParameterBasedTypeVariable -> {
return signature(type.typeParameterSymbol)
}
}
}
}
return "{undefined}"
}
Expand Down Expand Up @@ -387,6 +396,11 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
s.append("}")
} else if (type is ConeCapturedType && type.lowerType == null) {
s.append("*")
} else if (type is ConeStubTypeForChainInference) {
if (type.typeArguments.isNotEmpty()) {
throw UnsupportedOperationException("Unsupported ConeTypeProjection contains type arguments" + type.javaClass.getName())
}
s.append(signature(type.constructor.variable))
} else {
throw IllegalArgumentException("Unsupported ConeTypeProjection " + type.javaClass.getName())
}
Expand Down
27 changes: 19 additions & 8 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
Expand All @@ -29,6 +29,7 @@
import org.openrewrite.test.RewriteTest;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -306,18 +307,28 @@ void javaLangObject() {
class ParsingTest implements RewriteTest {
@Test
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/303")
@ExpectedToFail
void coneTypeProjection() {
rewriteRun(
kotlin(
"""
val labels: List<String> = listOf("")
val label: String = ""
val newLabels = buildList {
addAll(labels)
add(label)
val newList = buildList {
addAll(listOf(""))
}
"""
""", spec -> spec.afterRecipe(cu -> {
MethodMatcher methodMatcher = new MethodMatcher("kotlin.collections.MutableList addAll(..)");
AtomicBoolean found = new AtomicBoolean(false);
new KotlinIsoVisitor<AtomicBoolean>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean found) {
if (methodMatcher.matches(method)) {
assertThat(method.getMethodType().toString()).isEqualTo("kotlin.collections.MutableList<Generic{E}>{name=addAll,return=kotlin.Boolean,parameters=[kotlin.collections.Collection<Generic{E}>]}");
found.set(true);
}
return super.visitMethodInvocation(method, found);
}
}.visit(cu, found);
assertThat(found.get()).isTrue();
})
)
);
}
Expand Down

0 comments on commit c83e881

Please sign in to comment.