Skip to content

Commit

Permalink
Fix for problem with conditionals and not operation reported by Manoj.
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-sankaran committed Feb 22, 2024
1 parent a9de560 commit 3a809f1
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;

import java.util.stream.Stream;

import org.eclipse.jdt.internal.compiler.ASTVisitor;
import org.eclipse.jdt.internal.compiler.ast.TypeReference.AnnotationPosition;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
Expand Down Expand Up @@ -120,9 +122,7 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean
so, we need to manage the live variables manually. Pattern bindings are not definitely
assigned here as we are in instanceof false region.
*/
for (LocalVariableBinding binding : this.pattern.bindingsWhenTrue()) {
binding.recordInitializationEndPC(codeStream.position);
}
Stream.of(bindingsWhenTrue()).forEach(v->v.recordInitializationEndPC(codeStream.position));

if (valueRequired)
codeStream.iconst_0();
Expand Down Expand Up @@ -187,7 +187,8 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
if (valueRequired) {
if (falseLabel == null) {
if (trueLabel != null) {
// Implicit falling through the FALSE case
// Implicit falling through the FALSE case, any bindings defined when true cease to be live
Stream.of(bindingsWhenTrue()).forEach(v->v.recordInitializationEndPC(codeStream.position));
codeStream.goto_(trueLabel);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class UnaryExpression extends OperatorExpression {

public Expression expression;
public Constant optimizedBooleanConstant;
private int trueInitStateIndex = -1;

public UnaryExpression(Expression expression, int operator) {
this.expression = expression;
Expand All @@ -44,6 +45,7 @@ public FlowInfo analyseCode(
analyseCode(currentScope, flowContext, flowInfo).
asNegatedCondition();
flowContext.tagBits ^= FlowContext.INSIDE_NEGATION;
this.trueInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo.initsWhenTrue());
} else {
flowInfo = this.expression.
analyseCode(currentScope, flowContext, flowInfo);
Expand Down Expand Up @@ -102,6 +104,9 @@ public void generateCode(
(falseLabel = new BranchLabel(codeStream)),
valueRequired);
if (valueRequired) {
if (this.trueInitStateIndex != -1) {
codeStream.removeNotDefinitelyAssignedVariables(currentScope, this.trueInitStateIndex);
}
codeStream.iconst_0();
if (falseLabel.forwardReferenceCount() > 0) {
codeStream.goto_(endifLabel = new BranchLabel(codeStream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4577,4 +4577,193 @@ public static void main(String argv[]) {
},
"OK!");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_2() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String) ? (o instanceof String a) : (! (o instanceof String a));
System.out.println(b);
}
}
""",
},
"true\n"
+ "true");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_3() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String) ? !(o instanceof String a) : (o instanceof String a);
System.out.println(b);
}
}
""",
},
"false\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_4() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String) ? !!(o instanceof String a) : (o instanceof String c);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_5() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String) ? (o instanceof String a) : (o instanceof String c);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_6() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String) ? (o instanceof String a) : !!(o instanceof String c);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_7() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = (o instanceof String s1) ? (o instanceof String s2) : (o instanceof String s3);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_8() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = !(o instanceof String s1) ? (o instanceof String s2) : (o instanceof String s3);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_9() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = !!(o instanceof String s1) ? (o instanceof String s2) : (o instanceof String s3);
System.out.println(b);
}
}
""",
},
"true\n"
+ "false");
}
// Test for regression caused by fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1889
public void testIssue1889_10() {
runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] o) {
foo("one");
foo(new X());
}
public static void foo(Object o) {
boolean b = !!(o instanceof String s1) ? !!!!(o instanceof String s2) : !!!!!!!(o instanceof String s3);
System.out.println(b);
}
}
""",
},
"true\n"
+ "true");
}
}

0 comments on commit 3a809f1

Please sign in to comment.