Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for loops and collection access in TemplateAnalysis #1999

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions java/src/com/google/template/soy/jbcsrc/ExpressionCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
import com.google.template.soy.jbcsrc.shared.ExtraConstantBootstraps;
import com.google.template.soy.jbcsrc.shared.Names;
import com.google.template.soy.plugin.java.internal.PluginAnalyzer;
import com.google.template.soy.plugin.java.restricted.MethodSignature;
import com.google.template.soy.plugin.java.restricted.SoyJavaSourceFunction;
import com.google.template.soy.shared.internal.BuiltinFunction;
import com.google.template.soy.shared.internal.BuiltinMethod;
Expand All @@ -139,7 +138,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Future;
import javax.annotation.Nullable;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
Expand Down Expand Up @@ -2031,9 +2029,6 @@ protected Boolean visitVarRefNode(VarRefNode node) {
switch (node.getDefnDecl().kind()) {
case COMPREHENSION_VAR:
return true;
case PARAM:
case LOCAL_VAR:
return false;
case CONST:
// For consts we could allow references if they are in the same file and they themselves
// are constants. However, this would require changing the calling convention so that
Expand All @@ -2046,6 +2041,8 @@ protected Boolean visitVarRefNode(VarRefNode node) {
// For things like proto extensions and constructors we could allow references. But it
// isn't clear that that is very useful. For cross file `const`s this isn't possible
return false;
case PARAM:
case LOCAL_VAR:
case EXTERN:
return false;
case TEMPLATE:
Expand Down Expand Up @@ -2285,15 +2282,9 @@ protected Boolean visitTemplateLiteralNode(TemplateLiteralNode node) {
@Override
protected Boolean visitPluginFunction(FunctionNode node) {
Object fn = node.getSoyFunction();
if (fn instanceof SoyJavaSourceFunction) {
PluginAnalyzer.PluginMetadata metadata = PluginAnalyzer.analyze((SoyJavaSourceFunction) fn);
for (MethodSignature methodSignature :
Iterables.concat(
metadata.instanceMethodSignatures(), metadata.staticMethodSignatures())) {
if (Future.class.isAssignableFrom(methodSignature.returnType())) {
return true;
}
}
if (fn instanceof SoyJavaSourceFunction
&& JavaSourceFunctionCompiler.doesPluginReturnFuture((SoyJavaSourceFunction) fn)) {
return true;
}
return node.getParams().stream().anyMatch(this::visit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.template.soy.jbcsrc;

import com.google.common.collect.Iterables;
import com.google.template.soy.error.ErrorReporter;
import com.google.template.soy.exprtree.FieldAccessNode;
import com.google.template.soy.exprtree.FunctionNode;
Expand All @@ -23,10 +24,13 @@
import com.google.template.soy.jbcsrc.restricted.JbcSrcPluginContext;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.plugin.internal.JavaPluginExecContext;
import com.google.template.soy.plugin.java.internal.PluginAnalyzer;
import com.google.template.soy.plugin.java.restricted.MethodSignature;
import com.google.template.soy.plugin.java.restricted.SoyJavaSourceFunction;
import com.google.template.soy.shared.restricted.SoySourceFunctionMethod;
import com.google.template.soy.types.SoyTypeRegistry;
import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.Nullable;

/** Compiles method and function calls. */
Expand Down Expand Up @@ -136,4 +140,15 @@ public Expression getULocale() {
detacher)
.computeForJavaSource(args);
}

static boolean doesPluginReturnFuture(SoyJavaSourceFunction function) {
PluginAnalyzer.PluginMetadata metadata = PluginAnalyzer.analyze(function);
for (MethodSignature methodSignature :
Iterables.concat(metadata.instanceMethodSignatures(), metadata.staticMethodSignatures())) {
if (Future.class.isAssignableFrom(methodSignature.returnType())) {
return true;
}
}
return false;
}
}
Loading
Loading