Skip to content

Commit 0bf8a69

Browse files
Jesse-Goodcopybara-github
authored andcommitted
TemplateLiteralNode calculates its SoyType rather than requiring state be set on it.
PiperOrigin-RevId: 910452857
1 parent ba4a805 commit 0bf8a69

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

java/src/com/google/template/soy/exprtree/TemplateLiteralNode.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public final class TemplateLiteralNode extends AbstractParentExprNode {
3232

3333
private TriState isStaticCall = TriState.UNSET;
3434
private String templateFqn;
35-
private SoyType type;
3635

3736
public static TemplateLiteralNode forVarRef(VarRefNode varRef) {
3837
return forVarRef(varRef, varRef.getSourceLocation());
@@ -55,7 +54,6 @@ private TemplateLiteralNode(TemplateLiteralNode orig, CopyState copyState) {
5554
super(orig, copyState);
5655
this.isStaticCall = orig.isStaticCall;
5756
this.templateFqn = orig.templateFqn;
58-
this.type = orig.type;
5957
}
6058

6159
/** Returns whether this node is the root expression of a CallNode. */
@@ -74,10 +72,8 @@ public void resolveTemplateName() {
7472
SoyType type = getChild(0).getType();
7573
if (type instanceof TemplateImportType) {
7674
templateFqn = ((TemplateImportType) type).getName();
77-
this.type = type;
7875
} else {
7976
templateFqn = "";
80-
this.type = UnknownType.getInstance();
8177
}
8278
}
8379

@@ -93,12 +89,22 @@ public String getResolvedName() {
9389

9490
@Override
9591
public SoyType getType() {
96-
return type;
92+
if (getChild(0) instanceof VarRefNode varRef) {
93+
if (varRef.getDefnDecl() != null
94+
&& varRef.getDefnDecl().hasType()
95+
&& varRef.getDefnDecl().type() instanceof TemplateImportType templateType) {
96+
SoyType type = templateType.getBasicTemplateType();
97+
if (type != null) {
98+
return type;
99+
}
100+
}
101+
}
102+
return UnknownType.getInstance();
97103
}
98104

99105
@Override
100106
public void setType(SoyType type) {
101-
this.type = Preconditions.checkNotNull(type);
107+
throw new UnsupportedOperationException();
102108
}
103109

104110
@Override

java/src/com/google/template/soy/passes/ResolveExpressionTypesPass.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,21 +2738,18 @@ protected void visitTemplateLiteralNode(TemplateLiteralNode node) {
27382738
errorReporter.report(
27392739
node.getSourceLocation(), TEMPLATE_TYPE_PARAMETERS_CANNOT_USE_INFERRED_TYPES);
27402740
// Placeholder type to prevent further confusing errors.
2741-
node.setType(UnknownType.getInstance());
27422741
return;
27432742
}
27442743

27452744
visitChildren(node);
27462745

27472746
SoyType existingType = node.getType();
2748-
if (existingType instanceof TemplateImportType templateImportType) {
2749-
TemplateType basicType = templateImportType.getBasicTemplateType();
2750-
node.setType(
2751-
Preconditions.checkNotNull(
2752-
basicType,
2753-
"No type for %s (%s)",
2754-
node.getResolvedName(),
2755-
node.getSourceLocation()));
2747+
if (existingType instanceof TemplateImportType) {
2748+
Preconditions.checkNotNull(
2749+
node.getType(),
2750+
"No type for %s (%s)",
2751+
node.getResolvedName(),
2752+
node.getSourceLocation());
27562753
}
27572754
}
27582755

0 commit comments

Comments
 (0)