Skip to content

Commit ea96824

Browse files
Jesse-Goodcopybara-github
authored andcommitted
Make Soy cast a no-op in JBCSRC.
PiperOrigin-RevId: 785456760
1 parent 4d7d549 commit ea96824

File tree

4 files changed

+14
-30
lines changed

4 files changed

+14
-30
lines changed

java/src/com/google/template/soy/jbcsrc/ExpressionCompiler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,8 @@ protected SoyExpression visitBooleanNode(BooleanNode node) {
526526

527527
@Override
528528
protected SoyExpression visitAsOpNode(AsOpNode node) {
529-
// Casting requires boxing since the JVM can't just cast one primitive to another without
530-
// coercing (and thus changing the underlying value).
531-
SoyExpression value = visit(node.getChild(0)).box();
532-
return SoyExpression.forSoyValue(node.getType(), value.checkedSoyCast(node.getType()));
529+
// Casting is a compiler / type checking construct.
530+
return visit(node.getChild(0));
533531
}
534532

535533
@Override
@@ -1605,7 +1603,7 @@ private SoyExpression visitItemAccess(SoyExpression baseExpr, ItemAccessNode nod
16051603
// Special case index lookups on lists to avoid boxing the int key. Maps cannot be
16061604
// optimized the same way because there is no real way to 'unbox' a SoyLegacyObjectMap.
16071605
if (baseExpr.soyRuntimeType().isKnownListOrUnionOfLists()) {
1608-
SoyExpression list = baseExpr.unboxAsListUnchecked();
1606+
Expression list = baseExpr.unboxAsListUnchecked();
16091607
SoyExpression index = keyExpr.coerceToIndex();
16101608
if (analysis.isResolved(node)) {
16111609
soyValueProvider = MethodRefs.RUNTIME_GET_LIST_ITEM.invoke(list, index);

java/src/com/google/template/soy/jbcsrc/ExternCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ private static Expression adaptParameter(
483483
if (!isObject
484484
&& BytecodeUtils.isDefinitelyAssignableFrom(javaType, BytecodeUtils.IMMUTABLE_LIST_TYPE)) {
485485
SoyType elmType = SoyTypes.getIterableElementType(nonNullableSoyType);
486-
SoyExpression unboxedList =
486+
Expression unboxedList =
487487
actualParam.isBoxed() ? actualParam.unboxAsListOrJavaNull() : actualParam;
488488
switch (elmType.getKind()) {
489489
case INT:

java/src/com/google/template/soy/jbcsrc/ProtoUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import com.google.template.soy.types.SoyType;
103103
import com.google.template.soy.types.SoyTypes;
104104
import com.google.template.soy.types.UnionType;
105+
import com.google.template.soy.types.UnknownType;
105106
import java.util.ArrayList;
106107
import java.util.Iterator;
107108
import java.util.LinkedHashMap;
@@ -1285,7 +1286,7 @@ private Statement handleRepeatedNotNull(SoyExpression listArg, FieldDescriptor f
12851286
checkArgument(listArg.isNonSoyNullish());
12861287

12871288
// Unbox listArg as List<SoyValueProvider> and wait until all items are done
1288-
SoyExpression unboxed = listArg.unboxAsListUnchecked();
1289+
Expression unboxed = listArg.unboxAsListUnchecked();
12891290
Expression resolved = detacher.resolveSoyValueProviderList(unboxed);
12901291

12911292
// Enter new scope
@@ -1304,7 +1305,10 @@ private Statement handleRepeatedNotNull(SoyExpression listArg, FieldDescriptor f
13041305
// exitScope must be called after creating all the variables
13051306
Label scopeExit = scope.exitScopeMarker();
13061307
// Expected type info of the list element
1307-
SoyType elementSoyType = unboxed.soyType().asType(ListType.class).getElementType();
1308+
SoyType elementSoyType =
1309+
listArg.soyType() instanceof ListType
1310+
? listArg.soyType().asType(ListType.class).getElementType()
1311+
: UnknownType.getInstance();
13081312
SoyRuntimeType elementType = SoyRuntimeType.getBoxedType(elementSoyType);
13091313

13101314
// Call list.get(i).resolveSoyValueProvider(), then cast to the expected subtype of SoyValue

java/src/com/google/template/soy/jbcsrc/restricted/SoyExpression.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import com.google.template.soy.types.SoyTypes;
6060
import com.google.template.soy.types.StringType;
6161
import com.google.template.soy.types.UndefinedType;
62-
import com.google.template.soy.types.UnknownType;
6362
import java.math.BigInteger;
6463
import java.util.Iterator;
6564
import java.util.List;
@@ -793,7 +792,7 @@ public Expression unboxAsIteratorUnchecked() {
793792
}
794793

795794
/** Unboxes a list and its items. Throws an exception if the boxed list value is nullish. */
796-
public SoyExpression unboxAsListUnchecked() {
795+
public Expression unboxAsListUnchecked() {
797796
return this.asNonSoyNullish().unboxAsListOrJavaNull();
798797
}
799798

@@ -802,33 +801,16 @@ public SoyExpression unboxAsListUnchecked() {
802801
* parameter for an extern or plugin implementation, which both expect Java null rather than
803802
* NullData or UndefinedData.
804803
*/
805-
public SoyExpression unboxAsListOrJavaNull() {
804+
public Expression unboxAsListOrJavaNull() {
806805
if (alreadyUnboxed(List.class)) {
807806
return this;
808807
}
809808
assertBoxed(List.class);
810-
var delegate = delegateWithoutCast();
811-
Expression unboxedList =
812-
delegate.invoke(
809+
return delegateWithoutCast()
810+
.invoke(
813811
delegate.isNonSoyNullish()
814812
? MethodRefs.SOY_VALUE_AS_JAVA_LIST
815813
: MethodRefs.SOY_VALUE_AS_JAVA_LIST_OR_NULL);
816-
817-
ListType asListType;
818-
SoyRuntimeType nonNullRuntimeType =
819-
SoyRuntimeType.getBoxedType(SoyTypes.excludeNullish(soyType()));
820-
if (!SoyTypes.isNullOrUndefined(soyType()) && nonNullRuntimeType.isKnownIterable()) {
821-
asListType = nonNullRuntimeType.asListType();
822-
} else {
823-
if (soyType().isEffectivelyEqual(UnknownType.getInstance())
824-
|| SoyTypes.isNullOrUndefined(soyType())) {
825-
asListType = ListType.of(UnknownType.getInstance());
826-
} else {
827-
// The type checker should have already rejected all of these
828-
throw new UnsupportedOperationException("Can't convert " + soyRuntimeType + " to List");
829-
}
830-
}
831-
return forList(asListType, unboxedList);
832814
}
833815

834816
/** Unboxes a proto message. Throws an exception if the boxed value is nullish. */

0 commit comments

Comments
 (0)