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

(-)compiler/org/eclipse/jdt/internal/compiler/ast/Expression.java (-2 / +4 lines)
Lines 287-293 Link Here
287
						// ( TYPE_PARAMETER ) ARRAY
287
						// ( TYPE_PARAMETER ) ARRAY
288
						TypeBinding match = expressionType.findSuperTypeWithSameErasure(castType);
288
						TypeBinding match = expressionType.findSuperTypeWithSameErasure(castType);
289
						if (match == null) {
289
						if (match == null) {
290
							checkUnsafeCast(scope, castType, expressionType, match, true);
290
							checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
291
						}
291
						}
292
						// recurse on the type variable upper bound
292
						// recurse on the type variable upper bound
293
						return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);
293
						return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);
Lines 341-347 Link Here
341
							// ( INTERFACE ) TYPE_PARAMETER
341
							// ( INTERFACE ) TYPE_PARAMETER
342
							match = expressionType.findSuperTypeWithSameErasure(castType);
342
							match = expressionType.findSuperTypeWithSameErasure(castType);
343
							if (match == null) {
343
							if (match == null) {
344
								checkUnsafeCast(scope, castType, expressionType, match, true);
344
								checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
345
							}
345
							}
346
							// recurse on the type variable upper bound
346
							// recurse on the type variable upper bound
347
							return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);
347
							return checkCastTypesCompatibility(scope, ((TypeVariableBinding)castType).upperBound(), expressionType, expression);
Lines 360-365 Link Here
360
									return checkUnsafeCast(scope, castType, interfaceType, match, true);
360
									return checkUnsafeCast(scope, castType, interfaceType, match, true);
361
								}
361
								}
362
								if (use15specifics) {
362
								if (use15specifics) {
363
									checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
363
									// ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
364
									// ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
364
									if (interfaceType.hasIncompatibleSuperType((ReferenceBinding)castType))
365
									if (interfaceType.hasIncompatibleSuperType((ReferenceBinding)castType))
365
										return false;
366
										return false;
Lines 445-450 Link Here
445
									return checkUnsafeCast(scope, castType, expressionType, match, true);
446
									return checkUnsafeCast(scope, castType, expressionType, match, true);
446
								}
447
								}
447
								if (use15specifics) {
448
								if (use15specifics) {
449
									checkUnsafeCast(scope, castType, expressionType, null /*no match*/, true);
448
									// ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
450
									// ensure there is no collision between both interfaces: i.e. I1 extends List<String>, I2 extends List<Object>
449
									if (refExprType.hasIncompatibleSuperType((ReferenceBinding) castType))
451
									if (refExprType.hasIncompatibleSuperType((ReferenceBinding) castType))
450
										return false;
452
										return false;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java (-1 / +5 lines)
Lines 272-278 Link Here
272
	}
272
	}
273
	
273
	
274
	public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding expressionType, TypeBinding match, boolean isNarrowing) {
274
	public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding expressionType, TypeBinding match, boolean isNarrowing) {
275
		if (match == castType) {
275
 		if (match == castType) {
276
			if (!isNarrowing && match == this.resolvedType.leafComponentType()) { // do not tag as unnecessary when recursing through upper bounds
276
			if (!isNarrowing && match == this.resolvedType.leafComponentType()) { // do not tag as unnecessary when recursing through upper bounds
277
				tagAsUnnecessaryCast(scope, castType);
277
				tagAsUnnecessaryCast(scope, castType);
278
			}
278
			}
Lines 301-306 Link Here
301
				this.bits |= UnsafeCast;
301
				this.bits |= UnsafeCast;
302
				return true;
302
				return true;
303
			}
303
			}
304
			if (match == null && castType.isBoundParameterizedType()) { // cast between unrelated types
305
				this.bits |= UnsafeCast;
306
				return true;
307
			}
304
			if (leafType.isTypeVariable()) {
308
			if (leafType.isTypeVariable()) {
305
				this.bits |= UnsafeCast;
309
				this.bits |= UnsafeCast;
306
				return true;
310
				return true;
(-)src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java (-1 / +103 lines)
Lines 7357-7362 Link Here
7357
			"	Set<X> channel = channels.get(0);\n" + 
7357
			"	Set<X> channel = channels.get(0);\n" + 
7358
			"	                 ^^^^^^^^\n" + 
7358
			"	                 ^^^^^^^^\n" + 
7359
			"channels cannot be resolved\n" + 
7359
			"channels cannot be resolved\n" + 
7360
			"----------\n" + 
7361
			"2. WARNING in X.java (at line 7)\n" + 
7362
			"	element = (Set<X>) iter.next();\n" + 
7363
			"	          ^^^^^^^^^^^^^^^^^^^^\n" + 
7364
			"Type safety: The cast from X to Set<X> is actually checking against the erased type Set\n" + 
7360
			"----------\n");
7365
			"----------\n");
7361
	}			
7366
	}			
7362
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=70243 unsafe cast when wildcards
7367
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=70243 unsafe cast when wildcards
Lines 26562-26570 Link Here
26562
		"1. WARNING in X.java (at line 5)\n" + 
26567
		"1. WARNING in X.java (at line 5)\n" + 
26563
		"	return (Bar<Object>)f;\n" + 
26568
		"	return (Bar<Object>)f;\n" + 
26564
		"	       ^^^^^^^^^^^^^^\n" + 
26569
		"	       ^^^^^^^^^^^^^^\n" + 
26570
		"Type safety: The cast from Foo to Bar<Object> is actually checking against the erased type Bar\n" + 
26571
		"----------\n" + 
26572
		"2. WARNING in X.java (at line 5)\n" + 
26573
		"	return (Bar<Object>)f;\n" + 
26574
		"	       ^^^^^^^^^^^^^^\n" + 
26565
		"Unnecessary cast from Foo to Bar<Object>\n" + 
26575
		"Unnecessary cast from Foo to Bar<Object>\n" + 
26566
		"----------\n" + 
26576
		"----------\n" + 
26567
		"2. ERROR in X.java (at line 7)\n" + 
26577
		"3. ERROR in X.java (at line 7)\n" + 
26568
		"	Zork z;\n" + 
26578
		"	Zork z;\n" + 
26569
		"	^^^^\n" + 
26579
		"	^^^^\n" + 
26570
		"Zork cannot be resolved to a type\n" + 
26580
		"Zork cannot be resolved to a type\n" + 
Lines 34170-34173 Link Here
34170
		},
34180
		},
34171
		"");
34181
		"");
34172
}
34182
}
34183
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041
34184
public void test1062() {
34185
	this.runNegativeTest(
34186
		new String[] {
34187
			"X.java", // =================
34188
			"import java.util.HashSet;\n" + 
34189
			"import java.util.Iterator;\n" + 
34190
			"import java.util.Set;\n" + 
34191
			"\n" + 
34192
			"public class X {\n" + 
34193
			"	public static void main(String[] args) {\n" + 
34194
			"		Set<X> set = new HashSet<X>();\n" + 
34195
			"		for (Iterator<X> iterator = set.iterator(); iterator.hasNext();) {\n" + 
34196
			"			Set<X> element1 = iterator.next();\n" + 
34197
			"			Set<X> element2 = (Set<X>) iterator.next(); // warning\n" + 
34198
			"		}\n" + 
34199
			"	}\n" + 
34200
			"}", // =================
34201
		},
34202
		"----------\n" + 
34203
		"1. ERROR in X.java (at line 9)\n" + 
34204
		"	Set<X> element1 = iterator.next();\n" + 
34205
		"	                  ^^^^^^^^^^^^^^^\n" + 
34206
		"Type mismatch: cannot convert from X to Set<X>\n" + 
34207
		"----------\n" + 
34208
		"2. WARNING in X.java (at line 10)\n" + 
34209
		"	Set<X> element2 = (Set<X>) iterator.next(); // warning\n" + 
34210
		"	                  ^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
34211
		"Type safety: The cast from X to Set<X> is actually checking against the erased type Set\n" + 
34212
		"----------\n");
34213
}
34214
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041 - variation
34215
public void test1063() {
34216
	this.runNegativeTest(
34217
		new String[] {
34218
			"X.java", // =================
34219
			"import java.util.HashSet;\n" + 
34220
			"import java.util.Iterator;\n" + 
34221
			"import java.util.Set;\n" + 
34222
			"\n" + 
34223
			"public class X {\n" + 
34224
			"	public static void main(String[] args) {\n" + 
34225
			"		Set<Cloneable> set = new HashSet<Cloneable>();\n" + 
34226
			"		for (Iterator<Cloneable> iterator = set.iterator(); iterator.hasNext();) {\n" + 
34227
			"			Set<Cloneable> element1 = iterator.next();\n" + 
34228
			"			Set<Cloneable> element2 = (Set<Cloneable>) iterator.next(); // warning\n" + 
34229
			"		}\n" + 
34230
			"	}\n" + 
34231
			"}", // =================
34232
		},
34233
		"----------\n" + 
34234
		"1. ERROR in X.java (at line 9)\n" + 
34235
		"	Set<Cloneable> element1 = iterator.next();\n" + 
34236
		"	                          ^^^^^^^^^^^^^^^\n" + 
34237
		"Type mismatch: cannot convert from Cloneable to Set<Cloneable>\n" + 
34238
		"----------\n" + 
34239
		"2. WARNING in X.java (at line 10)\n" + 
34240
		"	Set<Cloneable> element2 = (Set<Cloneable>) iterator.next(); // warning\n" + 
34241
		"	                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
34242
		"Type safety: The cast from Cloneable to Set<Cloneable> is actually checking against the erased type Set\n" + 
34243
		"----------\n");
34244
}
34245
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=148041 - variation
34246
public void test1064() {
34247
	this.runNegativeTest(
34248
		new String[] {
34249
			"X.java", // =================
34250
			"import java.util.HashSet;\n" + 
34251
			"import java.util.Iterator;\n" + 
34252
			"\n" + 
34253
			"public class X {\n" + 
34254
			"	public static void main(String[] args) {\n" + 
34255
			"		HashSet<X> set = new HashSet<X>();\n" + 
34256
			"		for (Iterator<X> iterator = set.iterator(); iterator.hasNext();) {\n" + 
34257
			"			HashSet<X> element1 = iterator.next();\n" + 
34258
			"			HashSet<X> element2 = (HashSet<X>) iterator.next();\n" + 
34259
			"		}\n" + 
34260
			"	}\n" + 
34261
			"}", // =================
34262
		},
34263
		"----------\n" + 
34264
		"1. ERROR in X.java (at line 8)\n" + 
34265
		"	HashSet<X> element1 = iterator.next();\n" + 
34266
		"	                      ^^^^^^^^^^^^^^^\n" + 
34267
		"Type mismatch: cannot convert from X to HashSet<X>\n" + 
34268
		"----------\n" + 
34269
		"2. ERROR in X.java (at line 9)\n" + 
34270
		"	HashSet<X> element2 = (HashSet<X>) iterator.next();\n" + 
34271
		"	                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
34272
		"Cannot cast from X to HashSet<X>\n" + 
34273
		"----------\n");
34274
}
34173
}
34275
}

Return to bug 148041