Skip to content

Commit

Permalink
[J2KT] Fix FixJavaToKotlinCollectionMismatch pass to handle return …
Browse files Browse the repository at this point in the history
…type conversion explicitly, instead of relying on other passes.

PiperOrigin-RevId: 702876141
  • Loading branch information
Googler authored and copybara-github committed Dec 4, 2024
1 parent 797a460 commit 0745f99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import com.google.j2cl.transpiler.ast.Method;
import com.google.j2cl.transpiler.ast.MethodCall;
import com.google.j2cl.transpiler.ast.MethodDescriptor;
import com.google.j2cl.transpiler.ast.MethodLike;
import com.google.j2cl.transpiler.ast.Node;
import com.google.j2cl.transpiler.ast.ReturnStatement;
import com.google.j2cl.transpiler.ast.Statement;
import com.google.j2cl.transpiler.ast.SuperReference;
import com.google.j2cl.transpiler.ast.TypeDescriptor;
Expand Down Expand Up @@ -171,7 +173,7 @@ public Node rewriteMethod(Method method) {
if (methodMapping == null) {
return method;
}
return methodMapping.fixMethodParameters(method);
return methodMapping.fixMethodParametersAndReturnStatements(method);
}

@Override
Expand Down Expand Up @@ -259,10 +261,10 @@ private boolean isOrOverrides(MethodDescriptor methodDescriptor) {
}

/**
* Fixes parameters in {@code method} to match Kotlin, and rewrites the body of the method to
* introduce local variables with original types.
* Fixes parameters and return statements in {@code method} to match Kotlin, and rewrites the
* body of the method to introduce local variables with original types.
*/
private Method fixMethodParameters(Method method) {
private Method fixMethodParametersAndReturnStatements(Method method) {
MethodDescriptor javaMethodDescriptor = getJavaMethodDescriptor();
MethodDescriptor kotlinMethodDescriptor = getKotlinMethodDescriptor();
MethodDescriptor methodDescriptor = method.getDescriptor();
Expand Down Expand Up @@ -320,6 +322,16 @@ private Method fixMethodParameters(Method method) {
}
}

TypeDescriptor javaDeclarationReturnTypeDescriptor =
javaMethodDescriptor.getReturnTypeDescriptor();
TypeDescriptor kotlinDeclarationReturnTypeDescriptor =
kotlinMethodDescriptor.getReturnTypeDescriptor();
if (!javaDeclarationReturnTypeDescriptor.equals(kotlinDeclarationReturnTypeDescriptor)) {
TypeDescriptor kotlinReturnTypeDescriptor =
kotlinDeclarationReturnTypeDescriptor.specializeTypeVariables(parameterization);
body = (Block) insertReturnStatementCasts(body, kotlinReturnTypeDescriptor);
}

TypeDescriptor kotlinReturnTypeDescriptor =
kotlinMethodDescriptor
.getReturnTypeDescriptor()
Expand All @@ -338,6 +350,27 @@ private Method fixMethodParameters(Method method) {
.build();
}

private static Node insertReturnStatementCasts(Node node, TypeDescriptor castTypeDescriptor) {
return node.rewrite(
new AbstractRewriter() {
@Override
public Node rewriteReturnStatement(ReturnStatement returnStatement) {
if (getParent(MethodLike.class::isInstance) != null) {
// This is a return in other method-like node, nothing to do.
return returnStatement;
}

return ReturnStatement.Builder.from(returnStatement)
.setExpression(
CastExpression.newBuilder()
.setExpression(returnStatement.getExpression())
.setCastTypeDescriptor(castTypeDescriptor)
.build())
.build();
}
});
}

/**
* Fixes method call to match Kotlin signature. Ordinary method calls are delegated to "java_"
* bridge extension functions, and explicit casts are inserted for arguments to super calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ open class Collections {
val defaultValue_1: String? = defaultValue
var key_1: Any? = key
key_1 = Collections.convert<Any?>(key_1)
return super<Collections.CustomMap>.getOrDefault(key_1 as String, defaultValue_1 as String)!!
return super<Collections.CustomMap>.getOrDefault(key_1 as String, defaultValue_1 as String) as String
}
}
}

0 comments on commit 0745f99

Please sign in to comment.