View | Details | Raw Unified | Return to bug 127244
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/flow/UnconditionalFlowInfo.java (+18 lines)
Lines 1510-1515 Link Here
1510
	return count;
1510
	return count;
1511
}
1511
}
1512
1512
1513
/**
1514
 * Erase definite inits and potential inits from this, then return this. 
1515
 * @return this flow info, minus definite inits and potential inits
1516
 */
1517
public UnconditionalFlowInfo nullInfo() {
1518
	if (this == DEAD_END) {
1519
		return this;
1520
	}
1521
	this.definiteInits =
1522
		this.potentialInits = 0;
1523
	if (this.extra != null) {
1524
		for (int i = 0, length = this.extra[0].length; i < length; i++) {
1525
			this.extra[0][i] = this.extra[1][i] = 0;
1526
		}
1527
	}
1528
	return this;
1529
}
1530
1513
public UnconditionalFlowInfo nullInfoLessUnconditionalCopy() {
1531
public UnconditionalFlowInfo nullInfoLessUnconditionalCopy() {
1514
	if (this == DEAD_END) {
1532
	if (this == DEAD_END) {
1515
		return this;
1533
		return this;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/AssertStatement.java (-3 / +10 lines)
Lines 54-62 Link Here
54
		boolean isOptimizedTrueAssertion = cst != Constant.NotAConstant && cst.booleanValue() == true;
54
		boolean isOptimizedTrueAssertion = cst != Constant.NotAConstant && cst.booleanValue() == true;
55
		boolean isOptimizedFalseAssertion = cst != Constant.NotAConstant && cst.booleanValue() == false;
55
		boolean isOptimizedFalseAssertion = cst != Constant.NotAConstant && cst.booleanValue() == false;
56
56
57
		UnconditionalFlowInfo assertInfo = assertExpression.
57
		FlowInfo assertRawInfo = assertExpression.
58
			analyseCode(currentScope, flowContext, flowInfo.copy()).
58
			analyseCode(currentScope, flowContext, flowInfo.copy());
59
		UnconditionalFlowInfo assertWhenTrueInfo = assertRawInfo.initsWhenTrue().
59
			unconditionalInits();
60
			unconditionalInits();
61
		UnconditionalFlowInfo assertInfo = assertRawInfo.unconditionalCopy();
60
		if (isOptimizedTrueAssertion) {
62
		if (isOptimizedTrueAssertion) {
61
			assertInfo.setReachMode(FlowInfo.UNREACHABLE);
63
			assertInfo.setReachMode(FlowInfo.UNREACHABLE);
62
		}
64
		}
Lines 80-87 Link Here
80
		}
82
		}
81
		if (isOptimizedFalseAssertion) {
83
		if (isOptimizedFalseAssertion) {
82
			return flowInfo; // if assertions are enabled, the following code will be unreachable
84
			return flowInfo; // if assertions are enabled, the following code will be unreachable
85
			// change this if we need to carry null analysis results of the assert
86
			// expression downstream
83
		} else {
87
		} else {
84
			return flowInfo.mergedWith(assertInfo); 
88
			return flowInfo.mergedWith(assertInfo.nullInfoLessUnconditionalCopy()).
89
				addInitializationsFrom(assertWhenTrueInfo.nullInfo());
90
			// keep the merge from the initial code for the definite assignment 
91
			// analysis, tweak the null part to influence nulls downstream
85
		}
92
		}
86
	}
93
	}
87
94
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (+157 lines)
Lines 5093-5098 Link Here
5093
		"----------\n");
5093
		"----------\n");
5094
}
5094
}
5095
5095
5096
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5097
// [compiler] Null reference analysis doesn't understand assertions
5098
public void test0950_assert() {
5099
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5100
		this.runConformTest(
5101
			new String[] {
5102
				"X.java",
5103
				"public class X {\n" + 
5104
				"  void foo(Object o) {\n" + 
5105
				"    boolean b = o != null;\n" + // shades doubts upon o 
5106
				"    assert(o != null);\n" + 	// protection
5107
				"    o.toString();\n" + 		// quiet
5108
				"  }\n" + 
5109
				"}\n"},
5110
			"");
5111
	}
5112
}
5113
5114
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5115
// [compiler] Null reference analysis doesn't understand assertions
5116
public void test0951_assert() {
5117
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5118
		this.runNegativeTest(
5119
			new String[] {
5120
				"X.java",
5121
				"public class X {\n" + 
5122
				"  void foo(Object o) {\n" + 
5123
				"    assert(o == null);\n" + 	// forces null
5124
				"    o.toString();\n" + 		// can only be null
5125
				"  }\n" + 
5126
				"}\n"},
5127
		"----------\n" + 
5128
		"1. ERROR in X.java (at line 4)\n" + 
5129
		"	o.toString();\n" + 
5130
		"	^\n" + 
5131
		"The variable o can only be null; it was either set to null or checked for null when last used\n" + 
5132
		"----------\n");
5133
	}
5134
}
5135
5136
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5137
// [compiler] Null reference analysis doesn't understand assertions
5138
public void test0952_assert() {
5139
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5140
		this.runNegativeTest(
5141
			new String[] {
5142
				"X.java",
5143
				"public class X {\n" + 
5144
				"  void foo(Object o, boolean b) {\n" + 
5145
				"    assert(o != null || b);\n" + // shade doubts
5146
				"    o.toString();\n" + 		// complain
5147
				"  }\n" + 
5148
				"}\n"},
5149
		"----------\n" + 
5150
		"1. ERROR in X.java (at line 4)\n" + 
5151
		"	o.toString();\n" + 
5152
		"	^\n" + 
5153
		"The variable o may be null\n" + 
5154
		"----------\n");
5155
	}
5156
}
5157
5158
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5159
// [compiler] Null reference analysis doesn't understand assertions
5160
public void test0953_assert_combined() {
5161
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5162
		this.runNegativeTest(
5163
			new String[] {
5164
				"X.java",
5165
				"public class X {\n" + 
5166
				"  void foo(Object o1, Object o2) {\n" + 
5167
				"    assert(o1 != null && o2 == null);\n" +
5168
				"    if (o1 == null) { };\n" + 		// complain
5169
				"    if (o2 == null) { };\n" + 		// complain
5170
				"  }\n" + 
5171
				"}\n"},
5172
		"----------\n" + 
5173
		"1. ERROR in X.java (at line 4)\n" + 
5174
		"	if (o1 == null) { };\n" + 
5175
		"	    ^^\n" + 
5176
		"The variable o1 cannot be null; it was either set to a non-null value or assumed to be non-null when last used\n" + 
5177
		"----------\n" + 
5178
		"2. ERROR in X.java (at line 5)\n" + 
5179
		"	if (o2 == null) { };\n" + 
5180
		"	    ^^\n" + 
5181
		"The variable o2 can only be null; it was either set to null or checked for null when last used\n" + 
5182
		"----------\n");
5183
	}
5184
}
5185
5186
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5187
// [compiler] Null reference analysis doesn't understand assertions
5188
public void test0954_assert_fake_reachable() {
5189
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5190
		this.runNegativeTest(
5191
			new String[] {
5192
				"X.java",
5193
				"public class X {\n" + 
5194
				"  void foo(Object o) {\n" + 
5195
				"    assert(false && o != null);\n" +
5196
				"    if (o == null) { };\n" + 		// quiet
5197
				"  }\n" + 
5198
				"}\n"},
5199
		"");
5200
	}
5201
}
5202
5203
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5204
// [compiler] Null reference analysis doesn't understand assertions
5205
public void test0955_assert_combined() {
5206
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5207
		this.runNegativeTest(
5208
			new String[] {
5209
				"X.java",
5210
				"public class X {\n" + 
5211
				"  void foo(Object o) {\n" + 
5212
				"    assert(false || o != null);\n" +
5213
				"    if (o == null) { };\n" + 		// complain
5214
				"  }\n" + 
5215
				"}\n"},
5216
		"----------\n" + 
5217
		"1. ERROR in X.java (at line 4)\n" + 
5218
		"	if (o == null) { };\n" + 
5219
		"	    ^\n" + 
5220
		"The variable o cannot be null; it was either set to a non-null value or assumed to be non-null when last used\n" + 
5221
		"----------\n");
5222
	}
5223
}
5224
5225
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
5226
// [compiler] Null reference analysis doesn't understand assertions
5227
public void test0956_assert_combined() {
5228
	if (COMPLIANCE_1_3.compareTo(this.complianceLevel) < 0) {
5229
		this.runNegativeTest(
5230
			new String[] {
5231
				"X.java",
5232
				"public class X {\n" + 
5233
				"  void foo() {\n" +
5234
				"    Object o = null;\n" + 
5235
				"    assert(o != null);\n" +    // complain
5236
				"    if (o == null) { };\n" +   // complain
5237
				"  }\n" + 
5238
				"}\n"},
5239
		"----------\n" + 
5240
		"1. ERROR in X.java (at line 4)\n" + 
5241
		"	assert(o != null);\n" + 
5242
		"	       ^\n" + 
5243
		"The variable o can only be null; it was either set to null or checked for null when last used\n" + 
5244
		"----------\n" + 
5245
		"2. ERROR in X.java (at line 5)\n" + 
5246
		"	if (o == null) { };\n" + 
5247
		"	    ^\n" + 
5248
		"The variable o cannot be null; it was either set to a non-null value or assumed to be non-null when last used\n" + 
5249
		"----------\n");
5250
	}
5251
}
5252
5096
// null analysis -- notNull protection tag
5253
// null analysis -- notNull protection tag
5097
public void _test0900_notNull_protection_tag() {
5254
public void _test0900_notNull_protection_tag() {
5098
	this.runNegativeTest(
5255
	this.runNegativeTest(

Return to bug 127244