23
23
24
24
/**
25
25
* @test
26
- * @bug 8263642
26
+ * @bug 8263642 8268885
27
27
* @summary javac should not emit duplicate checkcast for first bound of intersection type in cast
28
+ * duplicate checkcast when destination type is not first type of intersection type
28
29
* @library /tools/lib
29
30
* @modules jdk.compiler/com.sun.tools.javac.api
30
31
* jdk.compiler/com.sun.tools.javac.main
@@ -61,19 +62,39 @@ public static void main(String[] args) throws Exception {
61
62
t .runTests ();
62
63
}
63
64
65
+ static final String Code1 =
66
+ """
67
+ class IntersectionTypeTest {
68
+ interface I1 { }
69
+ static class C1 { }
70
+ static Object test(Object o) {
71
+ return (C1 & I1) o;
72
+ }
73
+ }
74
+ """ ;
75
+
76
+ static final String Code2 =
77
+ """
78
+ class IntersectionTypeTest {
79
+ interface I1 {}
80
+ interface I2 {}
81
+ static class C {}
82
+ void test() {
83
+ I2 i = (I1 & I2) new C();
84
+ }
85
+ }
86
+ """ ;
87
+
64
88
@ Test
65
89
public void testDuplicatedCheckcast () throws Exception {
66
- String code = """
67
- class IntersectionTypeTest {
68
- interface I1 { }
69
- static class C1 { }
70
- static Object test(Object o) {
71
- return (C1 & I1) o;
72
- }
73
- }""" ;
90
+ duplicateCheckCastHelper (Code1 , "IntersectionTypeTest$I1" , "IntersectionTypeTest$C1" );
91
+ duplicateCheckCastHelper (Code2 , "IntersectionTypeTest$I1" , "IntersectionTypeTest$I2" );
92
+ }
93
+
94
+ private void duplicateCheckCastHelper (String source , String expected1 , String expected2 ) throws Exception {
74
95
Path curPath = Path .of ("." );
75
96
new JavacTask (tb )
76
- .sources (code )
97
+ .sources (source )
77
98
.outdir (curPath )
78
99
.run ();
79
100
cf = ClassFile .read (curPath .resolve ("IntersectionTypeTest.class" ));
@@ -92,8 +113,8 @@ static Object test(Object o) {
92
113
throw new AssertionError ("The number of the instruction 'checkcast' is not right. " +
93
114
"Expected number: 2, actual number: " + checkCastList .size ());
94
115
}
95
- checkClassName (checkCastList .get (0 ), "IntersectionTypeTest$I1" );
96
- checkClassName (checkCastList .get (1 ), "IntersectionTypeTest$C1" );
116
+ checkClassName (checkCastList .get (0 ), expected1 );
117
+ checkClassName (checkCastList .get (1 ), expected2 );
97
118
}
98
119
99
120
public void checkClassName (Instruction ins , String expected ) throws Exception {
0 commit comments