Skip to content

Commit 7ac2079

Browse files
biboudislahodaj
authored andcommitted
8301025: ClassCastException in switch with generic record
Reviewed-by: jlahoda
1 parent 371a0c4 commit 7ac2079

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private UnrolledRecordPattern unrollRecordPattern(JCRecordPattern recordPattern)
334334
}
335335
JCMethodInvocation componentAccessor =
336336
make.App(make.Select(convert(make.Ident(recordBinding), recordBinding.type), //TODO - cast needed????
337-
component.accessor));
337+
component.accessor)).setType(types.erasure(component.accessor.getReturnType()));
338338
if (deconstructorCalls == null) {
339339
deconstructorCalls = Collections.newSetFromMap(new IdentityHashMap<>());
340340
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8301025
26+
* @enablePreview
27+
* @compile T8301025.java
28+
* @summary ClassCastException in switch with generic record
29+
* @modules jdk.compiler
30+
*/
31+
public class T8301025 {
32+
record TestRecord<T extends String>(T t) {}
33+
34+
public static void main(String argv[]) {
35+
TestRecord r = new TestRecord("a");
36+
switch (r) {
37+
case TestRecord(String cS)-> {
38+
System.out.println("String");
39+
}
40+
case TestRecord(Object cO)->{
41+
System.out.println("Object");
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)