Skip to content

Commit

Permalink
[J2KT] Remove redundant casts which were added implicitly by various …
Browse files Browse the repository at this point in the history
…passes, which are not needed in Kotlin.

PiperOrigin-RevId: 719284651
  • Loading branch information
Googler authored and copybara-github committed Jan 24, 2025
1 parent 59575e5 commit b114bc5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
import com.google.j2cl.transpiler.passes.RemoveNoopStatements;
import com.google.j2cl.transpiler.passes.RemoveUnnecessaryLabels;
import com.google.j2cl.transpiler.passes.RemoveUnneededCasts;
import com.google.j2cl.transpiler.passes.RemoveUnneededCastsJ2kt;
import com.google.j2cl.transpiler.passes.RemoveUnneededJsDocCasts;
import com.google.j2cl.transpiler.passes.RemoveUnneededNotNullChecks;
import com.google.j2cl.transpiler.passes.RemoveUnreachableCode;
Expand Down Expand Up @@ -775,6 +776,7 @@ public ImmutableList<Supplier<NormalizationPass>> getPassFactories(BackendOption
() -> new RemoveUnnecessaryLabels(/* onlyLoopsAreBreakable= */ true),
RemoveNestedBlocks::new,
RemoveNoopStatements::new,
RemoveUnneededCastsJ2kt::new,

// Passes that breaks the invariants for running ConversionContextVisitor related passes.
NormalizeVarargInvocationsJ2kt::new,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.j2cl.transpiler.passes;

import com.google.j2cl.transpiler.ast.AbstractRewriter;
import com.google.j2cl.transpiler.ast.CastExpression;
import com.google.j2cl.transpiler.ast.CompilationUnit;
import com.google.j2cl.transpiler.ast.Expression;
import com.google.j2cl.transpiler.ast.Node;

/** Removes casts which are unneeded in J2KT. */
public class RemoveUnneededCastsJ2kt extends NormalizationPass {

@Override
public void applyTo(CompilationUnit compilationUnit) {
compilationUnit.accept(
new AbstractRewriter() {
@Override
public Node rewriteCastExpression(CastExpression castExpression) {
Expression innerExpression = castExpression.getExpression();
if (!(innerExpression instanceof CastExpression)) {
return castExpression;
}

// Deduplicate casts of the same base type.
CastExpression innerCastExpression = (CastExpression) innerExpression;
if (!castExpression
.getCastTypeDescriptor()
.toRawTypeDescriptor()
.isSameBaseType(
innerCastExpression.getCastTypeDescriptor().toRawTypeDescriptor())) {
return castExpression;
}

return CastExpression.Builder.from(castExpression)
.setExpression(innerCastExpression.getExpression())
.build();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ open class CastGenerics<T, E: Number?> {
open fun testCastToRawToGeneric(
@ObjCName("withCastCastGenerics_Foo") foo: CastGenerics.Foo<Any?>?
): CastGenerics.Foo<String?>? {
return foo as CastGenerics.Foo<Any?>? as CastGenerics.Foo<String?>?
return foo as CastGenerics.Foo<String?>?
}

@ObjCName("outerGenericMethod")
Expand All @@ -81,7 +81,7 @@ open class CastGenerics<T, E: Number?> {
@ObjCName("withId") o: Any?
): EE where EE: CastGenerics.Empty1?, EE: CastGenerics.Empty2<EE>? {
if (o is CastGenerics.Empty1) {
return o as EE? as EE
return o as EE
}
return null as EE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ open class IntersectionTypeTest<U> {
private fun <T> m(): T where T: IntersectionTypeTest.A?, T: IntersectionTypeTest.EmptyA? {
return IntersectionTypeTest.get<Any?>(
Any(),
) as T? as T
) as T
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ open class NotNullAssertionProblems {
@ObjCName("J2ktJ2ktNotNullAssertionProblems_C", exact = true)
open class C<V> internal constructor() {
@JvmField
internal var defaultValue_pp_j2kt: V = null as V? as V
internal var defaultValue_pp_j2kt: V = null as V

internal open fun f_pp_j2kt(): V {
return (if (true) this.defaultValue_pp_j2kt else this.defaultValue_pp_j2kt) as V
Expand Down

0 comments on commit b114bc5

Please sign in to comment.