Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.errorprone.refaster.UStatement.UnifierWithUnconsumedStatements;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
Expand All @@ -40,6 +42,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.jspecify.annotations.Nullable;

/// Template representing a sequence of consecutive statements.
///
Expand Down Expand Up @@ -110,7 +113,27 @@ public Iterable<BlockTemplateMatch> match(
// TODO(lowasser): consider nonconsecutive matches?
if (tree instanceof JCBlock block) {
ImmutableList<JCStatement> targetStatements = ImmutableList.copyOf(block.getStatements());
return matchesStartingAnywhere(block, 0, targetStatements, context, compilationUnit)
return matchesStartingAnywhere(
block, 0, targetStatements, context, compilationUnit, null, null)
.findFirst()
.orElse(List.nil());
}
return ImmutableList.of();
}

@Override
Iterable<BlockTemplateMatch> match(
JCTree tree, Unifier.MatchContext matchContext, @Nullable TreePath currentPath) {
if (tree instanceof JCBlock block) {
ImmutableList<JCStatement> targetStatements = ImmutableList.copyOf(block.getStatements());
return matchesStartingAnywhere(
block,
0,
targetStatements,
matchContext.context(),
matchContext.compilationUnit(),
matchContext,
currentPath)
.findFirst()
.orElse(List.nil());
}
Expand All @@ -122,16 +145,28 @@ private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(
int offset,
ImmutableList<? extends StatementTree> statements,
Context context,
JCCompilationUnit compilationUnit) {
JCCompilationUnit compilationUnit,
Unifier.@Nullable MatchContext matchContext,
@Nullable TreePath currentPath) {
if (statements.isEmpty()) {
return Choice.none();
}
JCStatement firstStatement = (JCStatement) statements.getFirst();
ImmutableList<UStatement> tmplStatements = templateStatements();
// NOMUTANTS -- Performance fast-path; avoids Unifier allocation when first statement differs.
if (!tmplStatements.isEmpty()) {
Tree.Kind requiredFirstKind = UTree.requiredStatementKind(tmplStatements.getFirst());
if (requiredFirstKind != null && requiredFirstKind != firstStatement.getKind()) {
return Choice.none();
}
}
Unifier initialUnifier =
matchContext != null
? new Unifier(matchContext, currentPath, expressionArgumentTypes(), autoboxing())
: new Unifier(context, compilationUnit, expressionArgumentTypes(), autoboxing());
Choice<UnifierWithUnconsumedStatements> choice =
Choice.of(
UnifierWithUnconsumedStatements.create(
new Unifier(context, compilationUnit), statements));
for (UStatement templateStatement : templateStatements()) {
Choice.of(UnifierWithUnconsumedStatements.create(initialUnifier, statements));
for (UStatement templateStatement : tmplStatements) {
choice = choice.flatMap(templateStatement);
}
return choice.flatMap((UnifierWithUnconsumedStatements state) -> {
Expand All @@ -153,7 +188,7 @@ private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(
boolean verified =
ExpressionTemplate.trueOrNull(
ExpressionTemplate.PLACEHOLDER_VERIFIER.scan(
templateStatements(), checkedUnifier.get()));
tmplStatements, checkedUnifier.get()));
if (!verified) {
return Choice.none();
}
Expand All @@ -162,7 +197,9 @@ private Choice<List<BlockTemplateMatch>> matchesStartingAtBeginning(
offset + consumedStatements,
statements.subList(consumedStatements, statements.size()),
context,
compilationUnit)
compilationUnit,
matchContext,
currentPath)
.map(list -> list.prepend(match));
}
} catch (CouldNotResolveImportException e) {
Expand All @@ -177,7 +214,9 @@ private Choice<List<BlockTemplateMatch>> matchesStartingAnywhere(
int offset,
ImmutableList<? extends StatementTree> statements,
Context context,
JCCompilationUnit compilationUnit) {
JCCompilationUnit compilationUnit,
Unifier.@Nullable MatchContext matchContext,
@Nullable TreePath currentPath) {
Choice<List<BlockTemplateMatch>> choice = Choice.none();
for (int i = 0; i < statements.size(); i++) {
choice =
Expand All @@ -187,7 +226,9 @@ private Choice<List<BlockTemplateMatch>> matchesStartingAnywhere(
offset + i,
statements.subList(i, statements.size()),
context,
compilationUnit));
compilationUnit,
matchContext,
currentPath));
}
return choice.concat(Choice.of(List.<BlockTemplateMatch>nil()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import com.sun.source.tree.ConditionalExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -138,7 +139,26 @@ public Iterable<ExpressionTemplateMatch> match(
JCTree target, Context context, JCCompilationUnit compilationUnit) {
if (target instanceof JCExpression targetExpr) {
Optional<Unifier> unifier =
unify(targetExpr, new Unifier(context, compilationUnit)).findFirst();
unify(
targetExpr,
new Unifier(context, compilationUnit, expressionArgumentTypes(), autoboxing()))
.findFirst();
if (unifier.isPresent()) {
return ImmutableList.of(new ExpressionTemplateMatch(targetExpr, unifier.get()));
}
}
return ImmutableList.of();
}

@Override
Iterable<ExpressionTemplateMatch> match(
JCTree target, Unifier.MatchContext matchContext, @Nullable TreePath currentPath) {
if (target instanceof JCExpression targetExpr) {
Optional<Unifier> unifier =
unify(
targetExpr,
new Unifier(matchContext, currentPath, expressionArgumentTypes(), autoboxing()))
.findFirst();
if (unifier.isPresent()) {
return ImmutableList.of(new ExpressionTemplateMatch(targetExpr, unifier.get()));
}
Expand Down
14 changes: 4 additions & 10 deletions core/src/main/java/com/google/errorprone/refaster/Inliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.errorprone.refaster.UTypeVar.TypeWithExpression;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -97,17 +96,12 @@ public void addStaticImport(String qualifiedImport) {

public ClassSymbol resolveClass(CharSequence qualifiedClass)
throws CouldNotResolveImportException {
try {
Symbol symbol =
JavaCompiler.instance(context).resolveBinaryNameOrIdent(qualifiedClass.toString());
if (symbol.equals(symtab().errSymbol) || !(symbol instanceof ClassSymbol classSymbol)) {
throw new CouldNotResolveImportException(qualifiedClass);
} else {
return classSymbol;
}
} catch (NullPointerException e) {
ClassSymbol classSymbol =
Unifier.resolveClassOrNull(JavaCompiler.instance(context), symtab(), qualifiedClass);
if (classSymbol == null) {
throw new CouldNotResolveImportException(qualifiedClass);
}
return classSymbol;
}

public Context getContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean allRequiredMatched() {
}

private boolean couldUnify(UExpression expr, Tree tree, Unifier unifier) {
return expr.unify(tree, unifier.fork()).findFirst().isPresent();
return expr.unify(tree, unifier.forkSkippingEarlyTypeCheck()).findFirst().isPresent();
}

@Override
Expand Down
Loading
Loading