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

Alternative solution for issue 3281 #154

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions checker/tests/regex/Issue3281.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test case for Issue 3281:
// https://github.com/typetools/checker-framework/issues/3281

import java.util.regex.Pattern;
import org.checkerframework.checker.regex.RegexUtil;

public class Issue3281 {

void bar(String s) {
RegexUtil.isRegex(s);
if (true) {
// :: error: (argument.type.incompatible)
Pattern.compile(s);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,9 @@ protected static class CFGTranslationPhaseOne extends TreePathScanner<Node, Void
*/
protected final List<LambdaExpressionTree> declaredLambdas;

/** Statements in a method that are followed by if-statement. */
protected final HashSet<StatementTree> statementBeforeIf;

/**
* @param treeBuilder builder for new AST nodes
* @param annotationProvider extracts annotations from AST nodes
Expand Down Expand Up @@ -1655,6 +1658,7 @@ public CFGTranslationPhaseOne(
returnNodes = new ArrayList<>();
declaredClasses = new ArrayList<>();
declaredLambdas = new ArrayList<>();
statementBeforeIf = new HashSet<>();
}

/**
Expand Down Expand Up @@ -3360,6 +3364,14 @@ public Node visitBinary(BinaryTree tree, Void p) {

@Override
public Node visitBlock(BlockTree tree, Void p) {
List<? extends StatementTree> statements = tree.getStatements();
// Collect all expression satatments that are followed by a if statment
for (int i = 0; i < statements.size() - 1; i++) {
if (statements.get(i).getKind() == Kind.EXPRESSION_STATEMENT
&& statements.get(i + 1).getKind() == Kind.IF) {
statementBeforeIf.add(statements.get(i));
}
}
for (StatementTree n : tree.getStatements()) {
scan(n, null);
}
Expand Down Expand Up @@ -3616,7 +3628,21 @@ public Node visitErroneous(ErroneousTree tree, Void p) {

@Override
public Node visitExpressionStatement(ExpressionStatementTree tree, Void p) {
return scan(tree.getExpression(), p);
if (statementBeforeIf.contains(tree)) {
// For an expression statment followed by an if-statment, create a local variable
// and assign the expression to that local variable, which would lead merging of the
// two store branches during dataflow analysis.
String name = uniqueName("mergeStoreBeforeIf");
Element owner = findOwner();
ExpressionTree initializer = tree.getExpression();
VariableTree auxillaryVariable =
treeBuilder.buildVariableDecl(
TreeUtils.typeOf(initializer), name, owner, initializer);
return scan(auxillaryVariable, p);

} else {
return scan(tree.getExpression(), p);
}
}

@Override
Expand Down