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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (-5 / +1 lines)
Lines 198-208 Link Here
198
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
198
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
199
							CastExpression.checkNeedForAssignedCast(scope, variableType, (CastExpression) this.initialization);
199
							CastExpression.checkNeedForAssignedCast(scope, variableType, (CastExpression) this.initialization);
200
						}	
200
						}	
201
					} else if (scope.isBoxingCompatibleWith(initializationType, variableType) 
201
					} else if (isBoxingCompatible(initializationType, variableType, this.initialization, scope)) {
202
										|| (initializationType.isBaseType()  // narrowing then boxing ?
203
												&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
204
												&& !variableType.isBaseType()
205
												&& initialization.isConstantValueOfTypeAssignableToType(initializationType, scope.environment().computeBoxingType(variableType)))) {
206
						this.initialization.computeConversion(scope, variableType, initializationType);
202
						this.initialization.computeConversion(scope, variableType, initializationType);
207
						if (this.initialization instanceof CastExpression 
203
						if (this.initialization instanceof CastExpression 
208
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
204
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java (-1 / +12 lines)
Lines 104-110 Link Here
104
	}
104
	}
105
105
106
	public abstract void generateCode(BlockScope currentScope, CodeStream codeStream);
106
	public abstract void generateCode(BlockScope currentScope, CodeStream codeStream);
107
	
107
108
	protected boolean isBoxingCompatible(TypeBinding expressionType, TypeBinding targetType, Expression expression, Scope scope) {
109
		if (scope.isBoxingCompatibleWith(expressionType, targetType))
110
			return true;
111
112
		return expressionType.isBaseType() // narrowing then boxing ?
113
				&& !targetType.isBaseType()
114
				&& !targetType.isTypeVariable()
115
				&& scope.compilerOptions().sourceLevel >= org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.JDK1_5 // autoboxing
116
				&& expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType( targetType));
117
	}
118
108
	public boolean isEmptyBlock() {
119
	public boolean isEmptyBlock() {
109
		return false;
120
		return false;
110
	}
121
	}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/FieldDeclaration.java (-5 / +1 lines)
Lines 230-240 Link Here
230
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
230
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
231
						CastExpression.checkNeedForAssignedCast(initializationScope, fieldType, (CastExpression) this.initialization);
231
						CastExpression.checkNeedForAssignedCast(initializationScope, fieldType, (CastExpression) this.initialization);
232
					}								
232
					}								
233
				} else if (initializationScope.isBoxingCompatibleWith(initializationType, fieldType) 
233
				} else if (isBoxingCompatible(initializationType, fieldType, this.initialization, initializationScope)) {
234
									|| (initializationType.isBaseType()  // narrowing then boxing ?
235
											&& initializationScope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
236
											&& !fieldType.isBaseType()
237
											&& initialization.isConstantValueOfTypeAssignableToType(initializationType, initializationScope.environment().computeBoxingType(fieldType)))) {
238
					this.initialization.computeConversion(initializationScope, fieldType, initializationType);
234
					this.initialization.computeConversion(initializationScope, fieldType, initializationType);
239
					if (this.initialization instanceof CastExpression 
235
					if (this.initialization instanceof CastExpression 
240
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
236
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java (-5 / +1 lines)
Lines 127-137 Link Here
127
		} else {
127
		} else {
128
			return this.constantExpression.constant;
128
			return this.constantExpression.constant;
129
		}
129
		}
130
	} else if (scope.isBoxingCompatibleWith(caseType, switchExpressionType)
130
	} else if (isBoxingCompatible(caseType, switchExpressionType, this.constantExpression, scope)) {
131
					|| (caseType.isBaseType()  // narrowing then boxing ?
132
							&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
133
							&& !switchExpressionType.isBaseType()
134
							&& this.constantExpression.isConstantValueOfTypeAssignableToType(caseType, scope.environment().computeBoxingType(switchExpressionType)))) {
135
		// constantExpression.computeConversion(scope, caseType, switchExpressionType); - do not report boxing/unboxing conversion
131
		// constantExpression.computeConversion(scope, caseType, switchExpressionType); - do not report boxing/unboxing conversion
136
		return this.constantExpression.constant;
132
		return this.constantExpression.constant;
137
	}
133
	}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java (-6 / +1 lines)
Lines 11-17 Link Here
11
package org.eclipse.jdt.internal.compiler.ast;
11
package org.eclipse.jdt.internal.compiler.ast;
12
12
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
14
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
15
import org.eclipse.jdt.internal.compiler.codegen.*;
14
import org.eclipse.jdt.internal.compiler.codegen.*;
16
import org.eclipse.jdt.internal.compiler.flow.*;
15
import org.eclipse.jdt.internal.compiler.flow.*;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
16
import org.eclipse.jdt.internal.compiler.impl.Constant;
Lines 239-249 Link Here
239
			CastExpression.checkNeedForAssignedCast(scope, methodType, (CastExpression) this.expression);
238
			CastExpression.checkNeedForAssignedCast(scope, methodType, (CastExpression) this.expression);
240
		}			
239
		}			
241
		return;
240
		return;
242
	} else if (scope.isBoxingCompatibleWith(expressionType, methodType)
241
	} else if (isBoxingCompatible(expressionType, methodType, this.expression, scope)) {
243
						|| (expressionType.isBaseType()  // narrowing then boxing ?
244
								&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
245
								&& !methodType.isBaseType()
246
								&& this.expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType(methodType)))) {
247
		this.expression.computeConversion(scope, methodType, expressionType);
242
		this.expression.computeConversion(scope, methodType, expressionType);
248
		if (this.expression instanceof CastExpression 
243
		if (this.expression instanceof CastExpression 
249
				&& (this.expression.bits & (ASTNode.UnnecessaryCast|ASTNode.DisableUnnecessaryCastCheck)) == 0) {
244
				&& (this.expression.bits & (ASTNode.UnnecessaryCast|ASTNode.DisableUnnecessaryCastCheck)) == 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java (-6 / +1 lines)
Lines 12-18 Link Here
12
package org.eclipse.jdt.internal.compiler.ast;
12
package org.eclipse.jdt.internal.compiler.ast;
13
13
14
import org.eclipse.jdt.internal.compiler.ASTVisitor;
14
import org.eclipse.jdt.internal.compiler.ASTVisitor;
15
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
16
import org.eclipse.jdt.internal.compiler.codegen.*;
15
import org.eclipse.jdt.internal.compiler.codegen.*;
17
import org.eclipse.jdt.internal.compiler.flow.*;
16
import org.eclipse.jdt.internal.compiler.flow.*;
18
import org.eclipse.jdt.internal.compiler.impl.Constant;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
Lines 214-224 Link Here
214
			CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
213
			CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
215
		}			
214
		}			
216
		return this.resolvedType;
215
		return this.resolvedType;
217
	} else if (scope.isBoxingCompatibleWith(rhsType, lhsType) 
216
	} else if (isBoxingCompatible(rhsType, lhsType, this.expression, scope)) {
218
						|| (rhsType.isBaseType()  // narrowing then boxing ?
219
								&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
220
								&& !lhsType.isBaseType()
221
								&& this.expression.isConstantValueOfTypeAssignableToType(rhsType, scope.environment().computeBoxingType(lhsType)))) {
222
		this.expression.computeConversion(scope, lhsType, rhsType);
217
		this.expression.computeConversion(scope, lhsType, rhsType);
223
		if (this.expression instanceof CastExpression 
218
		if (this.expression instanceof CastExpression 
224
				&& (this.expression.bits & ASTNode.UnnecessaryCast) == 0) {
219
				&& (this.expression.bits & ASTNode.UnnecessaryCast) == 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ArrayInitializer.java (-6 / +1 lines)
Lines 11-17 Link Here
11
package org.eclipse.jdt.internal.compiler.ast;
11
package org.eclipse.jdt.internal.compiler.ast;
12
12
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
14
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
15
import org.eclipse.jdt.internal.compiler.codegen.*;
14
import org.eclipse.jdt.internal.compiler.codegen.*;
16
import org.eclipse.jdt.internal.compiler.flow.*;
15
import org.eclipse.jdt.internal.compiler.flow.*;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
16
import org.eclipse.jdt.internal.compiler.impl.Constant;
Lines 169-179 Link Here
169
						|| (elementType.isBaseType() && BaseTypeBinding.isWidening(elementType.id, expressionType.id)))
168
						|| (elementType.isBaseType() && BaseTypeBinding.isWidening(elementType.id, expressionType.id)))
170
						|| expressionType.isCompatibleWith(elementType)) {
169
						|| expressionType.isCompatibleWith(elementType)) {
171
					expression.computeConversion(scope, elementType, expressionType);
170
					expression.computeConversion(scope, elementType, expressionType);
172
				} else if (scope.isBoxingCompatibleWith(expressionType, elementType) 
171
				} else if (isBoxingCompatible(expressionType, elementType, expression, scope)) {
173
									|| (expressionType.isBaseType()  // narrowing then boxing ?
174
											&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
175
											&& !elementType.isBaseType()
176
											&& expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType(elementType)))) {
177
					expression.computeConversion(scope, elementType, expressionType);
172
					expression.computeConversion(scope, elementType, expressionType);
178
				} else {
173
				} else {
179
					scope.problemReporter().typeMismatchError(expressionType, elementType, expression, null);
174
					scope.problemReporter().typeMismatchError(expressionType, elementType, expression, null);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/AutoBoxingTest.java (+125 lines)
Lines 4847-4850 Link Here
4847
		},
4847
		},
4848
		"IJdone");
4848
		"IJdone");
4849
}
4849
}
4850
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=264843
4851
public void test168() {
4852
	this.runNegativeTest(
4853
		new String[] {
4854
			"X.java",
4855
			"public class X {\n" + 
4856
			"    <T extends Integer> T a() { return 35; }\n" + 
4857
			"    <T extends Integer> T[] b() { return new int[]{35}; }\n" + 
4858
			"    <T extends Integer> T c() { return new Integer(35); }\n" +
4859
			"    <T extends Integer> T[] d() { return new Integer[]{35}; }\n" + 
4860
			"}\n",
4861
		},
4862
		"----------\n" + 
4863
		"1. WARNING in X.java (at line 2)\n" + 
4864
		"	<T extends Integer> T a() { return 35; }\n" + 
4865
		"	           ^^^^^^^\n" + 
4866
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
4867
		"----------\n" + 
4868
		"2. ERROR in X.java (at line 2)\n" + 
4869
		"	<T extends Integer> T a() { return 35; }\n" + 
4870
		"	                                   ^^\n" + 
4871
		"Type mismatch: cannot convert from int to T\n" + 
4872
		"----------\n" + 
4873
		"3. WARNING in X.java (at line 3)\n" + 
4874
		"	<T extends Integer> T[] b() { return new int[]{35}; }\n" + 
4875
		"	           ^^^^^^^\n" + 
4876
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
4877
		"----------\n" + 
4878
		"4. ERROR in X.java (at line 3)\n" + 
4879
		"	<T extends Integer> T[] b() { return new int[]{35}; }\n" + 
4880
		"	                                     ^^^^^^^^^^^^^\n" + 
4881
		"Type mismatch: cannot convert from int[] to T[]\n" + 
4882
		"----------\n" + 
4883
		"5. WARNING in X.java (at line 4)\n" + 
4884
		"	<T extends Integer> T c() { return new Integer(35); }\n" + 
4885
		"	           ^^^^^^^\n" + 
4886
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
4887
		"----------\n" + 
4888
		"6. ERROR in X.java (at line 4)\n" + 
4889
		"	<T extends Integer> T c() { return new Integer(35); }\n" + 
4890
		"	                                   ^^^^^^^^^^^^^^^\n" + 
4891
		"Type mismatch: cannot convert from Integer to T\n" + 
4892
		"----------\n" + 
4893
		"7. WARNING in X.java (at line 5)\n" + 
4894
		"	<T extends Integer> T[] d() { return new Integer[]{35}; }\n" + 
4895
		"	           ^^^^^^^\n" + 
4896
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
4897
		"----------\n" + 
4898
		"8. ERROR in X.java (at line 5)\n" + 
4899
		"	<T extends Integer> T[] d() { return new Integer[]{35}; }\n" + 
4900
		"	                                     ^^^^^^^^^^^^^^^^^\n" + 
4901
		"Type mismatch: cannot convert from Integer[] to T[]\n" + 
4902
		"----------\n" + 
4903
		"9. WARNING in X.java (at line 5)\n" + 
4904
		"	<T extends Integer> T[] d() { return new Integer[]{35}; }\n" + 
4905
		"	                                                   ^^\n" + 
4906
		"The expression of type int is boxed into Integer\n" + 
4907
		"----------\n");
4908
}
4909
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=264843
4910
public void test169() {
4911
	this.runNegativeTest(
4912
		new String[] {
4913
			"X.java",
4914
			"public class X<T extends Integer> {\n" + 
4915
			"    T x = 12;\n" + 
4916
			"    Byte y = 12;\n" + 
4917
			"    	void x(T t) {\n" +
4918
			"    		t = 5;\n" +
4919
			"    		switch (t) {\n" +
4920
			"    		case 1:\n" +
4921
			"    			break;\n" +
4922
			"    		}\n" +
4923
			"    	}\n" +
4924
			"    	void y(Byte t) {\n" +
4925
			"    		t = 5;\n" +
4926
			"    		switch (t) {\n" +
4927
			"    		case 1:\n" +
4928
			"    			break;\n" +
4929
			"    		}\n" +
4930
			"    	}\n" +
4931
			"}\n",
4932
		},
4933
		"----------\n" + 
4934
		"1. WARNING in X.java (at line 1)\n" + 
4935
		"	public class X<T extends Integer> {\n" + 
4936
		"	                         ^^^^^^^\n" + 
4937
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
4938
		"----------\n" + 
4939
		"2. ERROR in X.java (at line 2)\n" + 
4940
		"	T x = 12;\n" + 
4941
		"	      ^^\n" + 
4942
		"Type mismatch: cannot convert from int to T\n" + 
4943
		"----------\n" + 
4944
		"3. WARNING in X.java (at line 3)\n" + 
4945
		"	Byte y = 12;\n" + 
4946
		"	         ^^\n" + 
4947
		"The expression of type int is boxed into Byte\n" + 
4948
		"----------\n" + 
4949
		"4. ERROR in X.java (at line 5)\n" + 
4950
		"	t = 5;\n" + 
4951
		"	    ^\n" + 
4952
		"Type mismatch: cannot convert from int to T\n" + 
4953
		"----------\n" + 
4954
		"5. WARNING in X.java (at line 6)\n" + 
4955
		"	switch (t) {\n" + 
4956
		"	        ^\n" + 
4957
		"The expression of type T is unboxed into int\n" + 
4958
		"----------\n" + 
4959
		"6. ERROR in X.java (at line 7)\n" + 
4960
		"	case 1:\n" + 
4961
		"	     ^\n" + 
4962
		"Type mismatch: cannot convert from int to T\n" + 
4963
		"----------\n" + 
4964
		"7. WARNING in X.java (at line 12)\n" + 
4965
		"	t = 5;\n" + 
4966
		"	    ^\n" + 
4967
		"The expression of type int is boxed into Byte\n" + 
4968
		"----------\n" + 
4969
		"8. WARNING in X.java (at line 13)\n" + 
4970
		"	switch (t) {\n" + 
4971
		"	        ^\n" + 
4972
		"The expression of type Byte is unboxed into int\n" + 
4973
		"----------\n");
4974
}
4850
}
4975
}

Return to bug 264843