Skip to content

Commit d96152d

Browse files
committed
Fix separate compilation issue for generic patterns in ClassReader and mangled name calculation
1 parent 7e0ea40 commit d96152d

File tree

4 files changed

+139
-74
lines changed

4 files changed

+139
-74
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,9 +2090,13 @@ public Name externalName(Types types) {
20902090
}
20912091

20922092
private Name mangledBytecodePatternName(Types types) {
2093-
List<Type> erasedBindingTypes = ((PatternType) this.type).erasedBindingTypes;
2093+
List<Type> bindingTypes = ((PatternType) this.type).erasedBindingTypes;
20942094

2095-
List<String> parts = erasedBindingTypes.map(type -> {
2095+
if (bindingTypes == null) {
2096+
bindingTypes = ((PatternType) this.type).getBindingTypes();
2097+
}
2098+
2099+
List<String> parts = bindingTypes.map(type -> {
20962100
var g = new UnSharedSignatureGenerator(types);
20972101
g.assembleSig(type);
20982102
String mangled = name.table.names.fromString(BytecodeName.toBytecodeName(g.toName(name.table.names).toString())).toString();

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,13 @@ protected void read(Symbol sym, int attrLen) {
10681068
}
10691069
} else if (sym.type.hasTag(TypeTag.PATTERN)){
10701070
Type mtype = poolReader.getType(nextChar());
1071-
PatternType patternType = new PatternType(mtype.getParameterTypes(), mtype.getParameterTypes(), syms.voidType, syms.methodClass);
1071+
1072+
List<Type> erasedBindingTypes = mtype.getParameterTypes()
1073+
.stream()
1074+
.map(rc -> types.erasure(rc))
1075+
.collect(List.collector());
1076+
1077+
PatternType patternType = new PatternType(mtype.getParameterTypes(), erasedBindingTypes, syms.voidType, syms.methodClass);
10721078

10731079
sym.type = patternType;
10741080
//TODO: no thrown types for PatternType
@@ -1382,7 +1388,7 @@ protected void read(Symbol sym, int attrLen) {
13821388
parameterAccessFlags = null;
13831389

13841390
MethodSymbol msym = (MethodSymbol) sym;
1385-
msym.type = new PatternType(patternType.getParameterTypes(), patternType.getParameterTypes(), syms.voidType, syms.methodClass);
1391+
msym.type = new PatternType(patternType.getParameterTypes(), null, syms.voidType, syms.methodClass);
13861392

13871393
readMemberAttrs(sym);
13881394

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2024, 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+
/**
25+
* @test
26+
* @enablePreview
27+
* @compile -parameters GenericClassPatternDeclarations.java
28+
* @run main GenericClassPatternDeclarations
29+
*/
30+
import java.util.Objects;
31+
import java.util.List;
32+
33+
public class GenericClassPatternDeclarations {
34+
public static void main(String... args) {
35+
Box<Integer> l = new Box<>();
36+
37+
switch (l) {
38+
case Box(GBox<Integer> ll) -> { }
39+
default -> {}
40+
}
41+
}
42+
43+
public static class Box<T extends Integer> {
44+
public pattern Box(GBox<T> o) {
45+
match Box(new GBox<T>());
46+
}
47+
}
48+
49+
static class GBox<T> {}
50+
private static <T> void assertEquals(T expected, T actual) {
51+
if (!Objects.equals(expected, actual)) {
52+
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
53+
}
54+
}
55+
}

test/langtools/tools/javac/patterns/declarations/SeparateCompilation.java

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -207,76 +207,76 @@ public pattern Lib(int a) {
207207
.writeAll()
208208
.getOutput(Task.OutputKind.STDOUT);
209209
}
210-
//
211-
// @Test
212-
// public void testGenericClass(Path base) throws Exception { // TODO: inference issue
213-
// Path current = base.resolve(".");
214-
// Path lib = current.resolve("lib");
215-
// Path libSrc = lib.resolve("src");
216-
// Path libClasses = lib.resolve("classes");
217-
//
218-
// tb.writeJavaFiles(libSrc,
219-
// """
220-
// package lib;
221-
// import java.util.List;
222-
// public class Box<T extends Integer> {
223-
// public pattern Box(List<T> o) {
224-
// match Box(List.of(1, 2));
225-
// }
226-
// }
227-
// """);
228-
//
229-
// Files.createDirectories(libClasses);
230-
//
231-
// new JavacTask(tb)
232-
// .options("-XDrawDiagnostics", "--enable-preview", "--release", SOURCE_VERSION)
233-
// .outdir(libClasses)
234-
// .files(tb.findJavaFiles(libSrc))
235-
// .run()
236-
// .writeAll()
237-
// .getOutput(Task.OutputKind.DIRECT);
238-
//
239-
// Path test = current.resolve("test");
240-
// Path testSrc = test.resolve("src");
241-
// Path testClasses = test.resolve("classes");
242-
//
243-
// tb.writeJavaFiles(testSrc,
244-
// """
245-
// package test;
246-
// import java.util.List;
247-
// import lib.Box;
248-
//
249-
// public class Test {
250-
// public static void main(String... args) {
251-
// Box<Integer> l = new Box<>();
252-
//
253-
// switch (l) {
254-
// case Box<Integer>(List<Integer> l) -> { System.out.println(l.get(0)); }
255-
// default -> {}
256-
// }
257-
// }
258-
// }
259-
// """);
260-
//
261-
// Files.createDirectories(testClasses);
262-
//
263-
// new JavacTask(tb)
264-
// .options("-XDrawDiagnostics", "--enable-preview", "--release", SOURCE_VERSION)
265-
// .classpath(libClasses)
266-
// .outdir(testClasses)
267-
// .files(tb.findJavaFiles(testSrc))
268-
// .run()
269-
// .writeAll()
270-
// .getOutput(Task.OutputKind.DIRECT);
271-
//
272-
// String javaOut = new JavaTask(tb)
273-
// .vmOptions("--enable-preview")
274-
// .classpath(testClasses.toString() + System.getProperty("path.separator") + libClasses.toString())
275-
// .className("test.Test")
276-
// .run()
277-
// .writeAll()
278-
// .getOutput(Task.OutputKind.STDOUT);
279-
// }
210+
211+
@Test
212+
public void testGenericClass(Path base) throws Exception {
213+
Path current = base.resolve(".");
214+
Path lib = current.resolve("lib");
215+
Path libSrc = lib.resolve("src");
216+
Path libClasses = lib.resolve("classes");
217+
218+
tb.writeJavaFiles(libSrc,
219+
"""
220+
package lib;
221+
public class Box<T extends Integer> {
222+
public pattern Box(GBox<T> o) {
223+
match Box(new GBox<T>());
224+
}
225+
226+
public static class GBox<T> {}
227+
}
228+
""");
229+
230+
Files.createDirectories(libClasses);
231+
232+
new JavacTask(tb)
233+
.options("-XDrawDiagnostics", "--enable-preview", "--release", SOURCE_VERSION)
234+
.outdir(libClasses)
235+
.files(tb.findJavaFiles(libSrc))
236+
.run()
237+
.writeAll()
238+
.getOutput(Task.OutputKind.DIRECT);
239+
240+
Path test = current.resolve("test");
241+
Path testSrc = test.resolve("src");
242+
Path testClasses = test.resolve("classes");
243+
244+
tb.writeJavaFiles(testSrc,
245+
"""
246+
package test;
247+
import java.util.List;
248+
import lib.Box;
249+
public class Test {
250+
public static void main(String... args) {
251+
Box<Integer> l = new Box<>();
252+
253+
switch (l) {
254+
case Box(Box.GBox<Integer> ll) -> { }
255+
default -> {}
256+
}
257+
}
258+
}
259+
""");
260+
261+
Files.createDirectories(testClasses);
262+
263+
new JavacTask(tb)
264+
.options("-XDrawDiagnostics", "--enable-preview", "--release", SOURCE_VERSION)
265+
.classpath(libClasses)
266+
.outdir(testClasses)
267+
.files(tb.findJavaFiles(testSrc))
268+
.run()
269+
.writeAll()
270+
.getOutput(Task.OutputKind.DIRECT);
271+
272+
String javaOut = new JavaTask(tb)
273+
.vmOptions("--enable-preview")
274+
.classpath(testClasses.toString() + System.getProperty("path.separator") + libClasses.toString())
275+
.className("test.Test")
276+
.run()
277+
.writeAll()
278+
.getOutput(Task.OutputKind.STDOUT);
279+
}
280280

281281
@Test
282282
public void testGenericRecord(Path base) throws Exception {

0 commit comments

Comments
 (0)