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

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/AutoBoxingTest.java (+66 lines)
Lines 5102-5105 Link Here
5102
		"The expression of type int is boxed into Integer\n" + 
5102
		"The expression of type int is boxed into Integer\n" + 
5103
		"----------\n");
5103
		"----------\n");
5104
}
5104
}
5105
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=264843
5106
public void test169() {
5107
	this.runNegativeTest(
5108
		new String[] {
5109
			"X.java",
5110
			"public class X<T extends Integer> {\n" + 
5111
			"    T x = 12;\n" + 
5112
			"    Byte y = 12;\n" + 
5113
			"    	void x(T t) {\n" +
5114
			"    		t = 5;\n" +
5115
			"    		switch (t) {\n" +
5116
			"    		case 1:\n" +
5117
			"    			break;\n" +
5118
			"    		}\n" +
5119
			"    	}\n" +
5120
			"    	void y(Byte t) {\n" +
5121
			"    		t = 5;\n" +
5122
			"    		switch (t) {\n" +
5123
			"    		case 1:\n" +
5124
			"    			break;\n" +
5125
			"    		}\n" +
5126
			"    	}\n" +
5127
			"}\n",
5128
		},
5129
		"----------\n" + 
5130
		"1. WARNING in X.java (at line 1)\n" + 
5131
		"	public class X<T extends Integer> {\n" + 
5132
		"	                         ^^^^^^^\n" + 
5133
		"The type parameter T should not be bounded by the final type Integer. Final types cannot be further extended\n" + 
5134
		"----------\n" + 
5135
		"2. ERROR in X.java (at line 2)\n" + 
5136
		"	T x = 12;\n" + 
5137
		"	      ^^\n" + 
5138
		"Type mismatch: cannot convert from int to T\n" + 
5139
		"----------\n" + 
5140
		"3. WARNING in X.java (at line 3)\n" + 
5141
		"	Byte y = 12;\n" + 
5142
		"	         ^^\n" + 
5143
		"The expression of type int is boxed into Byte\n" + 
5144
		"----------\n" + 
5145
		"4. ERROR in X.java (at line 5)\n" + 
5146
		"	t = 5;\n" + 
5147
		"	    ^\n" + 
5148
		"Type mismatch: cannot convert from int to T\n" + 
5149
		"----------\n" + 
5150
		"5. WARNING in X.java (at line 6)\n" + 
5151
		"	switch (t) {\n" + 
5152
		"	        ^\n" + 
5153
		"The expression of type T is unboxed into int\n" + 
5154
		"----------\n" + 
5155
		"6. ERROR in X.java (at line 7)\n" + 
5156
		"	case 1:\n" + 
5157
		"	     ^\n" + 
5158
		"Type mismatch: cannot convert from int to T\n" + 
5159
		"----------\n" + 
5160
		"7. WARNING in X.java (at line 12)\n" + 
5161
		"	t = 5;\n" + 
5162
		"	    ^\n" + 
5163
		"The expression of type int is boxed into Byte\n" + 
5164
		"----------\n" + 
5165
		"8. WARNING in X.java (at line 13)\n" + 
5166
		"	switch (t) {\n" + 
5167
		"	        ^\n" + 
5168
		"The expression of type Byte is unboxed into int\n" + 
5169
		"----------\n");
5170
}
5105
}
5171
}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (-5 / +1 lines)
Lines 197-207 Link Here
197
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
197
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
198
							CastExpression.checkNeedForAssignedCast(scope, variableType, (CastExpression) this.initialization);
198
							CastExpression.checkNeedForAssignedCast(scope, variableType, (CastExpression) this.initialization);
199
						}
199
						}
200
					} else if (scope.isBoxingCompatibleWith(initializationType, variableType)
200
					} else if (isBoxingCompatible(initializationType, variableType, this.initialization, scope)) {
201
										|| (initializationType.isBaseType()  // narrowing then boxing ?
202
												&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
203
												&& !variableType.isBaseType()
204
												&& this.initialization.isConstantValueOfTypeAssignableToType(initializationType, scope.environment().computeBoxingType(variableType)))) {
205
						this.initialization.computeConversion(scope, variableType, initializationType);
201
						this.initialization.computeConversion(scope, variableType, initializationType);
206
						if (this.initialization instanceof CastExpression
202
						if (this.initialization instanceof CastExpression
207
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
203
								&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Statement.java (+11 lines)
Lines 114-119 Link Here
114
114
115
public abstract void generateCode(BlockScope currentScope, CodeStream codeStream);
115
public abstract void generateCode(BlockScope currentScope, CodeStream codeStream);
116
116
117
protected boolean isBoxingCompatible(TypeBinding expressionType, TypeBinding targetType, Expression expression, Scope scope) {
118
	if (scope.isBoxingCompatibleWith(expressionType, targetType))
119
		return true;
120
121
	return expressionType.isBaseType()  // narrowing then boxing ?
122
		&& !targetType.isBaseType()
123
		&& !targetType.isTypeVariable()
124
		&& scope.compilerOptions().sourceLevel >= org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.JDK1_5 // autoboxing
125
		&& expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType(targetType));
126
}
127
117
public boolean isEmptyBlock() {
128
public boolean isEmptyBlock() {
118
	return false;
129
	return false;
119
}
130
}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/FieldDeclaration.java (-5 / +1 lines)
Lines 227-237 Link Here
227
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
227
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
228
						CastExpression.checkNeedForAssignedCast(initializationScope, fieldType, (CastExpression) this.initialization);
228
						CastExpression.checkNeedForAssignedCast(initializationScope, fieldType, (CastExpression) this.initialization);
229
					}
229
					}
230
				} else if (initializationScope.isBoxingCompatibleWith(initializationType, fieldType)
230
				} else if (isBoxingCompatible(initializationType, fieldType, this.initialization, initializationScope)) {
231
									|| (initializationType.isBaseType()  // narrowing then boxing ?
232
											&& initializationScope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
233
											&& !fieldType.isBaseType()
234
											&& this.initialization.isConstantValueOfTypeAssignableToType(initializationType, initializationScope.environment().computeBoxingType(fieldType)))) {
235
					this.initialization.computeConversion(initializationScope, fieldType, initializationType);
231
					this.initialization.computeConversion(initializationScope, fieldType, initializationType);
236
					if (this.initialization instanceof CastExpression
232
					if (this.initialization instanceof CastExpression
237
							&& (this.initialization.bits & ASTNode.UnnecessaryCast) == 0) {
233
							&& (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 (-7 / +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 241-252 Link Here
241
			CastExpression.checkNeedForAssignedCast(scope, methodType, (CastExpression) this.expression);
240
			CastExpression.checkNeedForAssignedCast(scope, methodType, (CastExpression) this.expression);
242
		}
241
		}
243
		return;
242
		return;
244
	} else if (scope.isBoxingCompatibleWith(expressionType, methodType)
243
	} else if (isBoxingCompatible(expressionType, methodType, this.expression, scope)) {
245
						|| (expressionType.isBaseType()  // narrowing then boxing ?
246
								&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
247
								&& !methodType.isBaseType()
248
								&& !methodType.isTypeVariable()
249
								&& this.expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType(methodType)))) {
250
		this.expression.computeConversion(scope, methodType, expressionType);
244
		this.expression.computeConversion(scope, methodType, expressionType);
251
		if (this.expression instanceof CastExpression
245
		if (this.expression instanceof CastExpression
252
				&& (this.expression.bits & (ASTNode.UnnecessaryCast|ASTNode.DisableUnnecessaryCastCheck)) == 0) {
246
				&& (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 179-189 Link Here
179
			CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
178
			CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
180
		}
179
		}
181
		return this.resolvedType;
180
		return this.resolvedType;
182
	} else if (scope.isBoxingCompatibleWith(rhsType, lhsType)
181
	} else if (isBoxingCompatible(rhsType, lhsType, this.expression, scope)) {
183
						|| (rhsType.isBaseType()  // narrowing then boxing ?
184
								&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
185
								&& !lhsType.isBaseType()
186
								&& this.expression.isConstantValueOfTypeAssignableToType(rhsType, scope.environment().computeBoxingType(lhsType)))) {
187
		this.expression.computeConversion(scope, lhsType, rhsType);
182
		this.expression.computeConversion(scope, lhsType, rhsType);
188
		if (this.expression instanceof CastExpression
183
		if (this.expression instanceof CastExpression
189
				&& (this.expression.bits & ASTNode.UnnecessaryCast) == 0) {
184
				&& (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 168-178 Link Here
168
				if (expression.isConstantValueOfTypeAssignableToType(expressionType, elementType) 
167
				if (expression.isConstantValueOfTypeAssignableToType(expressionType, elementType) 
169
						|| expressionType.isCompatibleWith(elementType)) {
168
						|| expressionType.isCompatibleWith(elementType)) {
170
					expression.computeConversion(scope, elementType, expressionType);
169
					expression.computeConversion(scope, elementType, expressionType);
171
				} else if (scope.isBoxingCompatibleWith(expressionType, elementType)
170
				} else if (isBoxingCompatible(expressionType, elementType, expression, scope)) {
172
									|| (expressionType.isBaseType()  // narrowing then boxing ?
173
											&& scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
174
											&& !elementType.isBaseType()
175
											&& expression.isConstantValueOfTypeAssignableToType(expressionType, scope.environment().computeBoxingType(elementType)))) {
176
					expression.computeConversion(scope, elementType, expressionType);
171
					expression.computeConversion(scope, elementType, expressionType);
177
				} else {
172
				} else {
178
					scope.problemReporter().typeMismatchError(expressionType, elementType, expression, null);
173
					scope.problemReporter().typeMismatchError(expressionType, elementType, expression, null);

Return to bug 264843