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

(-)src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java (+316 lines)
Lines 42625-42628 Link Here
42625
			"The method combine(X.TT[], X.TO<? super E>[]) in the type X is not applicable for the arguments (X.TO<? super String>[], X.OO<String,Object>[])\n" + 
42625
			"The method combine(X.TT[], X.TO<? super E>[]) in the type X is not applicable for the arguments (X.TO<? super String>[], X.OO<String,Object>[])\n" + 
42626
			"----------\n");
42626
			"----------\n");
42627
}
42627
}
42628
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425
42629
public void test1284() {
42630
	this.runNegativeTest(
42631
			new String[] {
42632
					"X.java",
42633
					"public class X {\n" + 
42634
					"	public static void main(String[] args) {\n" + 
42635
					"		Thread th = Thread.currentThread();\n" + 
42636
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42637
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42638
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42639
					"		y.foo(z1).get().getThreadGroup();\n" + 
42640
					"		y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42641
					"		Zork z;\n" + 
42642
					"	}\n" + 
42643
					"}\n" + 
42644
					"abstract class Y<T, U> {\n" + 
42645
					"	I2<T, U> foo(I1<T> i) {\n" + 
42646
					"		return (I2<T, U>) i;\n" + 
42647
					"	}\n" + 
42648
					"}\n" + 
42649
					"interface I1<T> {\n" + 
42650
					"}\n" + 
42651
					"interface I2<T, U> extends I1<T> {\n" + 
42652
					"	U get();\n" + 
42653
					"}\n" + 
42654
					"class Z<V, W> implements I2<V, W> {\n" + 
42655
					"	W w;\n" + 
42656
					"	Z(W w) {\n" + 
42657
					"		this.w = w;\n" + 
42658
					"	}\n" + 
42659
					"	public W get() {\n" + 
42660
					"		return this.w;\n" + 
42661
					"	}\n" + 
42662
					"}\n", // =================
42663
			},
42664
			"----------\n" + 
42665
			"1. ERROR in X.java (at line 9)\n" + 
42666
			"	Zork z;\n" + 
42667
			"	^^^^\n" + 
42668
			"Zork cannot be resolved to a type\n" + 
42669
			"----------\n" + 
42670
			"2. WARNING in X.java (at line 14)\n" + 
42671
			"	return (I2<T, U>) i;\n" + 
42672
			"	       ^^^^^^^^^^^^\n" + 
42673
			"Type safety: Unchecked cast from I1<T> to I2<T,U>\n" + 
42674
			"----------\n");
42675
}
42676
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42677
public void test1285() {
42678
	this.runNegativeTest(
42679
			new String[] {
42680
					"X.java",
42681
					"public class X {\n" + 
42682
					"	public static void main(String[] args) {\n" + 
42683
					"		Thread th = Thread.currentThread();\n" + 
42684
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42685
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42686
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42687
					"		y.foo(z1).get().getThreadGroup();\n" + 
42688
					"		y.foo(z2).get().getThreadGroup();\n" + 
42689
					"	}\n" + 
42690
					"}\n" + 
42691
					"abstract class Y<T, U> {\n" + 
42692
					"	I2<T, U> foo(I1<T,U> i) {\n" + 
42693
					"		return (I2<T, U>) i;\n" + 
42694
					"	}\n" + 
42695
					"}\n" + 
42696
					"interface I1<T, U> {\n" + 
42697
					"}\n" + 
42698
					"interface I2<T, U> extends I1<T,U> {\n" + 
42699
					"	U get();\n" + 
42700
					"}\n" + 
42701
					"class Z<V, W> implements I2<V, W> {\n" + 
42702
					"	W w;\n" + 
42703
					"	Z(W w) {\n" + 
42704
					"		this.w = w;\n" + 
42705
					"	}\n" + 
42706
					"	public W get() {\n" + 
42707
					"		return this.w;\n" + 
42708
					"	}\n" + 
42709
					"}\n", // =================
42710
			},
42711
			"----------\n" + 
42712
			"1. ERROR in X.java (at line 8)\n" + 
42713
			"	y.foo(z2).get().getThreadGroup();\n" + 
42714
			"	  ^^^\n" + 
42715
			"The method foo(I1<String,Thread>) in the type Y<String,Thread> is not applicable for the arguments (Z<String,Exception>)\n" + 
42716
			"----------\n");
42717
}
42718
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42719
public void test1286() {
42720
	this.runNegativeTest(
42721
			new String[] {
42722
					"X.java",
42723
					"public class X {\n" + 
42724
					"	public static void main(String[] args) {\n" + 
42725
					"		Thread th = Thread.currentThread();\n" + 
42726
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42727
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42728
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42729
					"		y.foo(z1).get().getThreadGroup();\n" + 
42730
					"		y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42731
					"		Zork z;\n" + 
42732
					"	}\n" + 
42733
					"}\n" + 
42734
					"abstract class Y<T, U> {\n" + 
42735
					"	I2<U> foo(I1 i) {\n" + 
42736
					"		return (I2<U>) i;\n" + 
42737
					"	}\n" + 
42738
					"}\n" + 
42739
					"interface I1 {}\n" + 
42740
					"interface I2<U> extends I1 {\n" + 
42741
					"	U get();\n" + 
42742
					"}\n" + 
42743
					"class Z<V, W> implements I2<W> {\n" + 
42744
					"	W w;\n" + 
42745
					"	Z(W w) {\n" + 
42746
					"		this.w = w;\n" + 
42747
					"	}\n" + 
42748
					"	public W get() {\n" + 
42749
					"		return this.w;\n" + 
42750
					"	}\n" + 
42751
					"}\n", // =================
42752
			},
42753
			"----------\n" + 
42754
			"1. ERROR in X.java (at line 9)\n" + 
42755
			"	Zork z;\n" + 
42756
			"	^^^^\n" + 
42757
			"Zork cannot be resolved to a type\n" + 
42758
			"----------\n" + 
42759
			"2. WARNING in X.java (at line 14)\n" + 
42760
			"	return (I2<U>) i;\n" + 
42761
			"	       ^^^^^^^^^\n" + 
42762
			"Type safety: Unchecked cast from I1 to I2<U>\n" + 
42763
			"----------\n");
42764
}
42765
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42766
public void test1287() {
42767
	this.runNegativeTest(
42768
			new String[] {
42769
					"X.java",
42770
					"public class X {\n" + 
42771
					"	public static void main(String[] args) {\n" + 
42772
					"		Thread th = Thread.currentThread();\n" + 
42773
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42774
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42775
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42776
					"		y.foo(z1).get().getThreadGroup();\n" + 
42777
					"		y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42778
					"		Zork z;\n" + 
42779
					"	}\n" + 
42780
					"}\n" + 
42781
					"abstract class Y<T, U> {\n" + 
42782
					"	I2<T,U> foo(I1<T,T> i) {\n" + 
42783
					"		return (I2<T,U>) i;\n" + 
42784
					"	}\n" + 
42785
					"}\n" + 
42786
					"interface I1<D,E> {}\n" + 
42787
					"interface I2<F,G> extends I1<F,F> {\n" + 
42788
					"	G get();\n" + 
42789
					"}\n" + 
42790
					"class Z<V, W> implements I2<V,W> {\n" + 
42791
					"	W w;\n" + 
42792
					"	Z(W w) {\n" + 
42793
					"		this.w = w;\n" + 
42794
					"	}\n" + 
42795
					"	public W get() {\n" + 
42796
					"		return this.w;\n" + 
42797
					"	}\n" + 
42798
					"}\n", // =================
42799
			},
42800
			"----------\n" + 
42801
			"1. ERROR in X.java (at line 9)\n" + 
42802
			"	Zork z;\n" + 
42803
			"	^^^^\n" + 
42804
			"Zork cannot be resolved to a type\n" + 
42805
			"----------\n" + 
42806
			"2. WARNING in X.java (at line 14)\n" + 
42807
			"	return (I2<T,U>) i;\n" + 
42808
			"	       ^^^^^^^^^^^\n" + 
42809
			"Type safety: Unchecked cast from I1<T,T> to I2<T,U>\n" + 
42810
			"----------\n");
42811
}
42812
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42813
public void test1288() {
42814
	this.runNegativeTest(
42815
			new String[] {
42816
					"X.java",
42817
					"import java.util.Map;\n" + 
42818
					"public class X {\n" + 
42819
					"	public static void main(String[] args) {\n" + 
42820
					"		Thread th = Thread.currentThread();\n" + 
42821
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42822
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42823
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42824
					"		y.foo(z1).get().getThreadGroup();\n" + 
42825
					"		y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42826
					"		Zork z;\n" + 
42827
					"	}\n" + 
42828
					"}\n" + 
42829
					"abstract class Y<T, U> {\n" + 
42830
					"	I2<T,U> foo(I1<Map<T,T>> i) {\n" + 
42831
					"		return (I2<T,U>) i;\n" + 
42832
					"	}\n" + 
42833
					"}\n" + 
42834
					"interface I1<D> {}\n" + 
42835
					"interface I2<F,G> extends I1<Map<F,F>> {\n" + 
42836
					"	G get();\n" + 
42837
					"}\n" + 
42838
					"class Z<V, W> implements I2<V,W> {\n" + 
42839
					"	W w;\n" + 
42840
					"	Z(W w) {\n" + 
42841
					"		this.w = w;\n" + 
42842
					"	}\n" + 
42843
					"	public W get() {\n" + 
42844
					"		return this.w;\n" + 
42845
					"	}\n" + 
42846
					"}", // =================
42847
			},
42848
			"----------\n" + 
42849
			"1. ERROR in X.java (at line 10)\n" + 
42850
			"	Zork z;\n" + 
42851
			"	^^^^\n" + 
42852
			"Zork cannot be resolved to a type\n" + 
42853
			"----------\n" + 
42854
			"2. WARNING in X.java (at line 15)\n" + 
42855
			"	return (I2<T,U>) i;\n" + 
42856
			"	       ^^^^^^^^^^^\n" + 
42857
			"Type safety: Unchecked cast from I1<Map<T,T>> to I2<T,U>\n" + 
42858
			"----------\n");
42859
}
42860
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42861
public void test1289() {
42862
	this.runNegativeTest(
42863
			new String[] {
42864
					"X.java",
42865
					"import java.util.Map;\n" + 
42866
					"public class X {\n" + 
42867
					"	public static void main(String[] args) {\n" + 
42868
					"		Thread th = Thread.currentThread();\n" + 
42869
					"		Z<String, Thread> z1 = new Z<String, Thread>(th);\n" + 
42870
					"		Z<String, Exception> z2 = new Z<String, Exception>(new Exception());\n" + 
42871
					"		Y<String, Thread> y = new Y<String, Thread>() {};\n" + 
42872
					"		y.foo(z1).get().getThreadGroup();\n" + 
42873
					"		y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42874
					"		Zork z;\n" + 
42875
					"	}\n" + 
42876
					"}\n" + 
42877
					"abstract class Y<T, U> {\n" + 
42878
					"	I2<T,U> foo(I1<Map<T,T>> i) {\n" + 
42879
					"		return (I2<T,U>) i;\n" + 
42880
					"	}\n" + 
42881
					"}\n" + 
42882
					"interface I1<D> {}\n" + 
42883
					"interface I2<F,G> extends I1<Map<F,G>> {\n" + 
42884
					"	G get();\n" + 
42885
					"}\n" + 
42886
					"class Z<V, W> implements I2<V,W> {\n" + 
42887
					"	W w;\n" + 
42888
					"	Z(W w) {\n" + 
42889
					"		this.w = w;\n" + 
42890
					"	}\n" + 
42891
					"	public W get() {\n" + 
42892
					"		return this.w;\n" + 
42893
					"	}\n" + 
42894
					"}", // =================
42895
			},
42896
			"----------\n" + 
42897
			"1. ERROR in X.java (at line 8)\n" + 
42898
			"	y.foo(z1).get().getThreadGroup();\n" + 
42899
			"	  ^^^\n" + 
42900
			"The method foo(I1<Map<String,String>>) in the type Y<String,Thread> is not applicable for the arguments (Z<String,Thread>)\n" + 
42901
			"----------\n" + 
42902
			"2. ERROR in X.java (at line 9)\n" + 
42903
			"	y.foo(z2).get().getThreadGroup(); // heap pollution: we get a CCE because we return a U2\n" + 
42904
			"	  ^^^\n" + 
42905
			"The method foo(I1<Map<String,String>>) in the type Y<String,Thread> is not applicable for the arguments (Z<String,Exception>)\n" + 
42906
			"----------\n" + 
42907
			"3. ERROR in X.java (at line 10)\n" + 
42908
			"	Zork z;\n" + 
42909
			"	^^^^\n" + 
42910
			"Zork cannot be resolved to a type\n" + 
42911
			"----------\n" + 
42912
			"4. ERROR in X.java (at line 15)\n" + 
42913
			"	return (I2<T,U>) i;\n" + 
42914
			"	       ^^^^^^^^^^^\n" + 
42915
			"Cannot cast from I1<Map<T,T>> to I2<T,U>\n" + 
42916
			"----------\n");
42917
}
42918
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=210425 - variation
42919
public void test1290() {
42920
	this.runNegativeTest(
42921
			new String[] {
42922
					"X.java",
42923
					"public class X <T, U> {\n" + 
42924
					"  K<T> foo(I<T> i) {\n" + 
42925
					"        return (K<T>) i;\n" + 
42926
					"  }\n" + 
42927
					"  Zork z;\n" + 
42928
					"}\n" + 
42929
					"interface I<T> {\n" + 
42930
					"}\n" + 
42931
					"interface J<T, U> extends I<T> {\n" + 
42932
					"}\n" + 
42933
					"interface K<T> extends J<T, String> {\n" + 
42934
					"}", // =================
42935
			},
42936
			"----------\n" + 
42937
			"1. ERROR in X.java (at line 5)\n" + 
42938
			"	Zork z;\n" + 
42939
			"	^^^^\n" + 
42940
			"Zork cannot be resolved to a type\n" + 
42941
			"----------\n");
42942
}
42943
42628
}
42944
}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java (+31 lines)
Lines 305-310 Link Here
305
								TypeBinding genericMatch = genericCastType.findSuperTypeOriginatingFrom(expressionType);
305
								TypeBinding genericMatch = genericCastType.findSuperTypeOriginatingFrom(expressionType);
306
								if (genericMatch == match) {
306
								if (genericMatch == match) {
307
									this.bits |= UnsafeCast;
307
									this.bits |= UnsafeCast;
308
								} else {
309
									// if I2<T,U> extends I1<T>, then cast from I1<T> to I2<T,U> is unchecked
310
									ParameterizedTypeBinding paramCastType = (ParameterizedTypeBinding) castType;
311
									ParameterizedTypeBinding paramMatch = (ParameterizedTypeBinding) match;
312
									// easy case if less parameters on match
313
									TypeBinding[] castArguments = paramCastType.arguments;
314
									int length = castArguments.length;
315
									if (length > paramMatch.arguments.length) {
316
										this.bits |= UnsafeCast;
317
									} else if ((paramCastType.tagBits & (TagBits.HasDirectWildcard|TagBits.HasTypeVariable)) != 0) {
318
										// verify alternate cast type, substituting different type arguments
319
										LookupEnvironment environment = scope.environment();
320
										nextAlternateArgument: for (int i = 0; i < length; i++) {
321
											switch (castArguments[i].kind()) {
322
												case Binding.WILDCARD_TYPE :
323
												case Binding.TYPE_PARAMETER :
324
													break; // check substituting with other
325
												default:
326
													continue nextAlternateArgument; // no alternative possible
327
											}
328
											TypeBinding[] alternateArguments;
329
											// need to clone for each iteration to avoid env paramtype cache interference
330
											System.arraycopy(paramCastType.arguments, 0, alternateArguments = new TypeBinding[length], 0, length);
331
											alternateArguments[i] = scope.getJavaLangObject();
332
											ParameterizedTypeBinding alternateCastType = environment.createParameterizedType((ReferenceBinding)genericCastType, alternateArguments, castType.enclosingType());
333
											if (alternateCastType.findSuperTypeOriginatingFrom(expressionType) == match) {
334
												this.bits |= UnsafeCast;
335
												break;
336
											}
337
										}
338
									}
308
								}
339
								}
309
								return true;
340
								return true;
310
							} else {
341
							} else {

Return to bug 210425