-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[J2KT] Propagate inferred nullability in array literals and new array…
… expressions, which were causing insertion of `!!` in value expressions. PiperOrigin-RevId: 735341118
- Loading branch information
1 parent
541a73a
commit b72dfe2
Showing
6 changed files
with
131 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
transpiler/java/com/google/j2cl/transpiler/passes/PropagateNullability.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* 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.ArrayLiteral; | ||
import com.google.j2cl.transpiler.ast.ArrayTypeDescriptor; | ||
import com.google.j2cl.transpiler.ast.CompilationUnit; | ||
import com.google.j2cl.transpiler.ast.Expression; | ||
import com.google.j2cl.transpiler.ast.NewArray; | ||
import com.google.j2cl.transpiler.ast.Node; | ||
import com.google.j2cl.transpiler.ast.TypeDescriptor; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Propagates nullability in inferred types from actual nullability in expressions. | ||
* | ||
* <p>At this moment it propagates nullability of inferred component types in array literals, by | ||
* inferring them from value expressions. Eventually, it'll also fix type arguments in invocations | ||
* by inferring them from arguments. | ||
* | ||
* <p>In the following statement: | ||
* | ||
* <pre>{@code | ||
* @Nullable String[] arr = {null}; | ||
* }</pre> | ||
* | ||
* the inferred type of array literal is {@code String[]} instead of {@code @Nullable String[]}. It | ||
* causes insertion of {@code !!} in transpiled Kotlin code: | ||
* | ||
* <pre>{@code | ||
* val arr: Array<String?> = arrayOf<String>(null!!) as Array<String?> | ||
* }</pre> | ||
* | ||
* This pass fixes the inferred array component type to be {@code @Nullable String[]}, which fixes | ||
* the problem in transpiled Kotlin code: | ||
* | ||
* <pre>{@code | ||
* val arr: Array<String?> = arrayOf<String?>(null) | ||
* }</pre> | ||
*/ | ||
public class PropagateNullability extends AbstractJ2ktNormalizationPass { | ||
|
||
@Override | ||
public void applyTo(CompilationUnit compilationUnit) { | ||
compilationUnit.accept( | ||
new AbstractRewriter() { | ||
@Override | ||
public Node rewriteArrayLiteral(ArrayLiteral arrayLiteral) { | ||
return propagateNullabilityFromValueExpressions(arrayLiteral); | ||
} | ||
|
||
@Override | ||
public Node rewriteNewArray(NewArray newArray) { | ||
Expression initializer = newArray.getInitializer(); | ||
if (initializer == null) { | ||
return newArray; | ||
} | ||
// Update type of NewArray expression from rewritten initializer. | ||
return NewArray.Builder.from(newArray) | ||
.setTypeDescriptor((ArrayTypeDescriptor) initializer.getTypeDescriptor()) | ||
.build(); | ||
} | ||
}); | ||
} | ||
|
||
private ArrayLiteral propagateNullabilityFromValueExpressions(ArrayLiteral arrayLiteral) { | ||
ArrayTypeDescriptor arrayTypeDescriptor = arrayLiteral.getTypeDescriptor(); | ||
TypeDescriptor componentTypeDescriptor = | ||
propagateNullabilityFrom( | ||
arrayTypeDescriptor.getComponentTypeDescriptor(), | ||
arrayLiteral.getValueExpressions().stream().map(Expression::getTypeDescriptor)); | ||
return arrayLiteral.toBuilder() | ||
.setTypeDescriptor( | ||
ArrayTypeDescriptor.Builder.from(arrayTypeDescriptor) | ||
.setComponentTypeDescriptor(componentTypeDescriptor) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
private static TypeDescriptor propagateNullabilityFrom( | ||
TypeDescriptor typeDescriptor, TypeDescriptor from) { | ||
return !from.equals(typeDescriptor) && from.canBeNull() | ||
? typeDescriptor.toNullable() | ||
: typeDescriptor; | ||
} | ||
|
||
private static TypeDescriptor propagateNullabilityFrom( | ||
TypeDescriptor typeDescriptor, Stream<TypeDescriptor> fromTypeDescriptors) { | ||
return fromTypeDescriptors.reduce( | ||
typeDescriptor, PropagateNullability::propagateNullabilityFrom, (a, b) -> a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters