View | Details | Raw Unified | Return to bug 155255 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (-38 / +28 lines)
Lines 89-137 Link Here
89
			return;
89
			return;
90
		}
90
		}
91
		int pc = codeStream.position;
91
		int pc = codeStream.position;
92
		Constant inlinedValue;
93
92
94
		// something to initialize?
93
		// something to initialize?
95
		if (initialization != null) {
94
		generateInit: {
96
			// initialize to constant value?
95
			if (this.initialization == null) 
97
			if ((inlinedValue = initialization.constant) != Constant.NotAConstant) {
96
				break generateInit;
98
				// forget initializing unused or final locals set to constant value (final ones are inlined)
97
			// forget initializing unused or final locals set to constant value (final ones are inlined)
99
				if (binding.resolvedPosition != -1) { // may need to preserve variable
98
			if (binding.resolvedPosition < 0) {
100
					int initPC = codeStream.position;
99
				if (initialization.constant != Constant.NotAConstant) 
101
					codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
100
					break generateInit;
102
					codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
103
					codeStream.store(binding, false);
104
					binding.recordInitializationStartPC(codeStream.position);
105
					//				codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
106
					//				codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index		
107
				}
108
			} else { // initializing to non-constant value
109
				initialization.generateCode(currentScope, codeStream, true);
110
				// if binding unused generate then discard the value
101
				// if binding unused generate then discard the value
111
				if (binding.resolvedPosition != -1) {
102
				// TODO why not simply discard value ?
112
					// 26903, need extra cast to store null in array local var	
103
				initialization.generateCode(currentScope, codeStream, true);
113
					if (binding.type.isArrayType() 
104
				if ((binding.type == TypeBinding.LONG) || (binding.type == TypeBinding.DOUBLE)) {
114
						&& (initialization.resolvedType == TypeBinding.NULL	// arrayLoc = null
105
					codeStream.pop2();
115
							|| ((initialization instanceof CastExpression)	// arrayLoc = (type[])null
116
								&& (((CastExpression)initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL)))){
117
						codeStream.checkcast(binding.type); 
118
					}					
119
					codeStream.store(binding, false);
120
					if (binding.initializationCount == 0) {
121
						/* Variable may have been initialized during the code initializing it
122
							e.g. int i = (i = 1);
123
						*/
124
						binding.recordInitializationStartPC(codeStream.position);
125
						//					codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
126
						//					codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index 
127
					}
128
				} else {
106
				} else {
129
					if ((binding.type == TypeBinding.LONG) || (binding.type == TypeBinding.DOUBLE)) {
107
					codeStream.pop();
130
						codeStream.pop2();
131
					} else {
132
						codeStream.pop();
133
					}
134
				}
108
				}
109
				break generateInit;
110
			}			
111
			initialization.generateCode(currentScope, codeStream, true);
112
			// 26903, need extra cast to store null in array local var	
113
			if (binding.type.isArrayType() 
114
				&& (initialization.resolvedType == TypeBinding.NULL	// arrayLoc = null
115
					|| ((initialization instanceof CastExpression)	// arrayLoc = (type[])null
116
						&& (((CastExpression)initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL)))){
117
				codeStream.checkcast(binding.type); 
118
			}					
119
			codeStream.store(binding, false);
120
			if (binding.initializationCount == 0) {
121
				/* Variable may have been initialized during the code initializing it
122
					e.g. int i = (i = 1);
123
				*/
124
				binding.recordInitializationStartPC(codeStream.position);
135
			}
125
			}
136
		}
126
		}
137
		codeStream.recordPositionsFrom(pc, this.sourceStart);
127
		codeStream.recordPositionsFrom(pc, this.sourceStart);
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java (-9 / +10 lines)
Lines 530-544 Link Here
530
				scope.problemReporter().autoboxing(this, compileTimeType, runtimeType);
530
				scope.problemReporter().autoboxing(this, compileTimeType, runtimeType);
531
				compileTimeType = unboxedType;
531
				compileTimeType = unboxedType;
532
			}
532
			}
533
		} else {
533
		} else if (compileTimeType != TypeBinding.NULL && compileTimeType.isBaseType()) {
534
			if (compileTimeType != TypeBinding.NULL && compileTimeType.isBaseType()) {
534
			TypeBinding boxedType = scope.environment().computeBoxingType(runtimeType);
535
				TypeBinding boxedType = scope.environment().computeBoxingType(runtimeType);
535
			if (boxedType == runtimeType) // Object o = 12;
536
				if (boxedType == runtimeType) // Object o = 12;
536
				boxedType = compileTimeType; 
537
					boxedType = compileTimeType; 
537
			this.implicitConversion = BOXING | (boxedType.id << 4) + compileTimeType.id;
538
				this.implicitConversion = BOXING | (boxedType.id << 4) + compileTimeType.id;
538
			scope.problemReporter().autoboxing(this, compileTimeType, scope.environment().computeBoxingType(boxedType));
539
				scope.problemReporter().autoboxing(this, compileTimeType, scope.environment().computeBoxingType(boxedType));
539
			return;
540
				return;
540
		} else if (this.constant != Constant.NotAConstant && this.constant.typeID() != T_JavaLangString) {
541
			}
541
			this.implicitConversion = BOXING;
542
			return;
542
		}
543
		}
543
		int compileTimeTypeID, runtimeTypeID;
544
		int compileTimeTypeID, runtimeTypeID;
544
		if ((compileTimeTypeID = compileTimeType.id) == NoId) { // e.g. ? extends String  ==> String (103227)
545
		if ((compileTimeTypeID = compileTimeType.id) == NoId) { // e.g. ? extends String  ==> String (103227)
(-)compiler/org/eclipse/jdt/internal/compiler/codegen/CodeStream.java (-31 / +28 lines)
Lines 1727-1763 Link Here
1727
}
1727
}
1728
public void generateConstant(Constant constant, int implicitConversionCode) {
1728
public void generateConstant(Constant constant, int implicitConversionCode) {
1729
	int targetTypeID = (implicitConversionCode & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4;
1729
	int targetTypeID = (implicitConversionCode & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4;
1730
	if (targetTypeID != 0) {
1730
	if (targetTypeID == 0) targetTypeID = constant.typeID(); // use default constant type
1731
		switch (targetTypeID) {
1731
	switch (targetTypeID) {
1732
			case TypeIds.T_boolean :
1732
		case TypeIds.T_boolean :
1733
				generateInlinedValue(constant.booleanValue());
1733
			generateInlinedValue(constant.booleanValue());
1734
				break;
1734
			break;
1735
			case TypeIds.T_char :
1735
		case TypeIds.T_char :
1736
				generateInlinedValue(constant.charValue());
1736
			generateInlinedValue(constant.charValue());
1737
				break;
1737
			break;
1738
			case TypeIds.T_byte :
1738
		case TypeIds.T_byte :
1739
				generateInlinedValue(constant.byteValue());
1739
			generateInlinedValue(constant.byteValue());
1740
				break;
1740
			break;
1741
			case TypeIds.T_short :
1741
		case TypeIds.T_short :
1742
				generateInlinedValue(constant.shortValue());
1742
			generateInlinedValue(constant.shortValue());
1743
				break;
1743
			break;
1744
			case TypeIds.T_int :
1744
		case TypeIds.T_int :
1745
				generateInlinedValue(constant.intValue());
1745
			generateInlinedValue(constant.intValue());
1746
				break;
1746
			break;
1747
			case TypeIds.T_long :
1747
		case TypeIds.T_long :
1748
				generateInlinedValue(constant.longValue());
1748
			generateInlinedValue(constant.longValue());
1749
				break;
1749
			break;
1750
			case TypeIds.T_float :
1750
		case TypeIds.T_float :
1751
				generateInlinedValue(constant.floatValue());
1751
			generateInlinedValue(constant.floatValue());
1752
				break;
1752
			break;
1753
			case TypeIds.T_double :
1753
		case TypeIds.T_double :
1754
				generateInlinedValue(constant.doubleValue());
1754
			generateInlinedValue(constant.doubleValue());
1755
				break;
1755
			break;
1756
			case TypeIds.T_JavaLangString :
1756
		case TypeIds.T_JavaLangString :
1757
				ldc(constant.stringValue());
1757
			ldc(constant.stringValue());
1758
		}
1759
	} else {
1760
		ldc(constant.stringValue());
1761
	}
1758
	}
1762
	if ((implicitConversionCode & TypeIds.BOXING) != 0) {
1759
	if ((implicitConversionCode & TypeIds.BOXING) != 0) {
1763
		// need boxing
1760
		// need boxing
(-)src/org/eclipse/jdt/core/tests/compiler/regression/AutoBoxingTest.java (+62 lines)
Lines 3648-3651 Link Here
3648
		"Type mismatch: cannot convert from int to Byte\n" + 
3648
		"Type mismatch: cannot convert from int to Byte\n" + 
3649
		"----------\n");
3649
		"----------\n");
3650
}
3650
}
3651
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=155255
3652
public void test123() {
3653
	this.runConformTest(
3654
		new String[] {
3655
			"X.java",
3656
			"public class X {\n" + 
3657
			"	public static void main(String[] args) {\n" + 
3658
			"		foo1();\n" + 
3659
			"		foo2();\n" + 
3660
			"		foo3();\n" + 
3661
			"		foo4();\n" + 
3662
			"		System.out.println(\"[done]\");\n" + 
3663
			"	}\n" + 
3664
			"	static void foo1() {\n" + 
3665
			"		Object x = true ? true : \"\";\n" + 
3666
			"		System.out.print(\"[1:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3667
			"	}\n" + 
3668
			"	static void foo2() {\n" + 
3669
			"		Object x = Boolean.TRUE != null ? true : \"\";\n" + 
3670
			"		System.out.print(\"[2:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3671
			"	}\n" + 
3672
			"	static void foo3() {\n" + 
3673
			"		Object x = false ? \"\" : false;\n" + 
3674
			"		System.out.print(\"[3:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3675
			"	}\n" + 
3676
			"	static void foo4() {\n" + 
3677
			"		Object x = Boolean.TRUE == null ? \"\" : false;\n" + 
3678
			"		System.out.print(\"[4:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3679
			"	}\n" + 
3680
			"}", // =================
3681
		},
3682
		"[1:true,java.lang.Boolean][2:true,java.lang.Boolean][3:false,java.lang.Boolean][4:false,java.lang.Boolean][done]");
3683
}
3684
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=155255 - variation
3685
public void test124() {
3686
	this.runNegativeTest(
3687
		new String[] {
3688
			"X.java",
3689
			"public class X {\n" + 
3690
			"	static void foo5() {\n" + 
3691
			"		boolean x = false ? \"\" : false;\n" + 
3692
			"		System.out.print(\"[4:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3693
			"	}	\n" + 
3694
			"}", // =================
3695
		},
3696
		"----------\n" + 
3697
		"1. ERROR in X.java (at line 3)\n" + 
3698
		"	boolean x = false ? \"\" : false;\n" + 
3699
		"	            ^^^^^^^^^^^^^^^^^^\n" + 
3700
		"Type mismatch: cannot convert from Object&Serializable&Comparable<?> to boolean\n" + 
3701
		"----------\n" + 
3702
		"2. WARNING in X.java (at line 3)\n" + 
3703
		"	boolean x = false ? \"\" : false;\n" + 
3704
		"	                         ^^^^^\n" + 
3705
		"The expression of type boolean is boxed into Boolean\n" + 
3706
		"----------\n" + 
3707
		"3. ERROR in X.java (at line 4)\n" + 
3708
		"	System.out.print(\"[4:\"+ x + \",\" + x.getClass().getCanonicalName() + \"]\");\n" + 
3709
		"	                                  ^^^^^^^^^^^^\n" + 
3710
		"Cannot invoke getClass() on the primitive type boolean\n" + 
3711
		"----------\n");
3712
	}
3651
}
3713
}

Return to bug 155255