Skip to content

Commit a7a0b04

Browse files
Googlercopybara-github
Googler
authored andcommitted
[J2KT] Run MakeVariablesNonNull pass also before PropagateNullability, to avoid propagating from nullable local variables which would eventually become non-null.
Also, fix infinite-loop bug in `MakeVariablesNonNull` pass. PiperOrigin-RevId: 735470036
1 parent 1929d61 commit a7a0b04

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

transpiler/java/com/google/j2cl/transpiler/backend/Backend.java

+1
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ public ImmutableList<Supplier<NormalizationPass>> getPassFactories(BackendOption
729729
FixJavaKotlinMethodOverrideMismatch::new,
730730
AnnotateProtobufMethodsAsKtProperties::new,
731731
RewriteAnnotationTypesJ2kt::new,
732+
MakeVariablesNonNull::new,
732733
PropagateNullability::new,
733734
NormalizeNullLiterals::new,
734735
NormalizeMinValueIntegralLiterals::new,

transpiler/java/com/google/j2cl/transpiler/passes/MakeVariablesNonNull.java

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.j2cl.transpiler.passes;
1717

18+
import static com.google.common.base.Preconditions.checkState;
1819
import static com.google.j2cl.transpiler.ast.TypeDescriptors.isBoxedType;
1920

2021
import com.google.j2cl.transpiler.ast.AbstractVisitor;
@@ -94,8 +95,17 @@ private void exitOperator(
9495

9596
private void exitAssignment(Variable variable, Expression expression) {
9697
TypeDescriptor typeDescriptor = variable.getTypeDescriptor();
98+
99+
if (typeDescriptor.isPrimitive()) {
100+
// Variables of primitve types can never be assigned nullable values, but when this
101+
// pass is run, unboxing passes might not have run yet and we might see a
102+
// potentially nullable expression.
103+
return;
104+
}
105+
97106
if (!typeDescriptor.isNullable() && expression.canBeNull()) {
98107
variable.setTypeDescriptor(typeDescriptor.toNullable());
108+
checkState(variable.getTypeDescriptor().isNullable());
99109
propagateChanges[0] = true;
100110
}
101111
}

transpiler/javatests/com/google/j2cl/readable/java/j2kt/output_kt/NullabilityInferenceWithLocalVariables.kt.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ open class NullabilityInferenceWithLocalVariables {
3838
@ObjCName("testArray")
3939
fun testArray(): Array<String> {
4040
val local: String = ""
41-
return arrayOf<String?>(local, "") as Array<String>
41+
return arrayOf<String>(local, "")
4242
}
4343

4444
@JvmStatic

0 commit comments

Comments
 (0)