Java control flow: Added conditional branch that bypasses assert statement because assertions can be disabled (IDEA-71526)

This commit is contained in:
Pavel Dolgov
2016-07-22 14:58:06 +03:00
parent 5437898e57
commit 2be74e8c45
5 changed files with 37 additions and 0 deletions

View File

@@ -986,6 +986,12 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
public void visitAssertStatement(PsiAssertStatement statement) {
startElement(statement);
myStartStatementStack.pushStatement(statement, false);
myEndStatementStack.pushStatement(statement, false);
Instruction passByWhenAssertionsDisabled = new ConditionalGoToInstruction(0, BranchingInstruction.Role.END, null);
myCurrentFlow.addInstruction(passByWhenAssertionsDisabled);
addElementOffsetLater(statement, false);
// should not try to compute constant expression within assert
// since assertions can be disabled/enabled at any moment via JVM flags
@@ -1014,6 +1020,9 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
myCurrentFlow.addInstruction(instruction);
addElementOffsetLater(myCodeFragment, false);
myStartStatementStack.popStatement();
myEndStatementStack.popStatement();
finishElement(statement);
}

View File

@@ -274,4 +274,12 @@ class InitializedInClassInitializerUsedInTheFollowingFieldInitializer {
{
k = 1;
}
}
class AssignInAssert {
<error descr="Variable 'b' might not have been initialized">private final boolean b</error>;
AssignInAssert() {
assert b = true;
}
}

View File

@@ -435,4 +435,13 @@ class ClassInitializerConstantEval {
<error descr="Variable 'x' might already have been assigned to">x</error> = 1;
}
}
}
class AssignInAssert {
void f() {
boolean a;
assert a = true;
if(<error descr="Variable 'a' might not have been initialized">a</error>)
System.out.println();
}
}

View File

@@ -0,0 +1,7 @@
// LocalsOrMyInstanceFieldsControlFlowPolicy
class A {
void f(boolean b) {<caret>
assert b;
System.out.println();
}
}

View File

@@ -0,0 +1,4 @@
0: COND_GOTO [END] 3
1: READ b
2: COND_THROW_TO 4
3: EMPTY