Skip to content

Commit

Permalink
Adjust Overloading (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis authored Jan 17, 2025
1 parent a78ebd9 commit 77a1ffe
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 120 deletions.
260 changes: 182 additions & 78 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2383,11 +2383,11 @@ public Tag getTag() {
/**
* Pattern matching forms.
*/
public abstract static class JCPattern extends JCTree
public sealed abstract static class JCPattern extends JCTree
implements PatternTree {
}

public static class JCAnyPattern extends JCPattern
public static final class JCAnyPattern extends JCPattern
implements AnyPatternTree {

protected JCAnyPattern() {
Expand Down Expand Up @@ -2415,7 +2415,7 @@ public Tag getTag() {
}
}

public static class JCBindingPattern extends JCPattern
public static final class JCBindingPattern extends JCPattern
implements BindingPatternTree {
public JCVariableDecl var;

Expand Down Expand Up @@ -2554,7 +2554,7 @@ public Tag getTag() {

}

public static class JCRecordPattern extends JCPattern
public static final class JCRecordPattern extends JCPattern
implements DeconstructionPatternTree {
public JCExpression deconstructor;
public List<JCPattern> nested;
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/jdk/classfile/PatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static helpers.TestUtil.assertEmpty;
import static org.junit.jupiter.api.Assertions.*;

import java.lang.classfile.components.ClassPrinter;
import jdk.internal.classfile.components.ClassPrinter;
import org.junit.jupiter.api.Test;

class PatternTest {
Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/T8305582.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @test
* @bug 8305582
* @summary Compiler crash when compiling record patterns with var
* @compile/fail/ref=T8305582.out -XDrawDiagnostics -XDshould-stop.at=FLOW T8305582.java
* @compile/fail/ref=T8305582.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDdev T8305582.java
*/

public class T8305582 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@
// options: --enable-preview -source ${jdk.version} -Xlint:preview

public class PatternDeclarationOverloadingAmbiguity {
private static int test(D o) {
if (o instanceof D(String data, Integer out)) {
return out;
interface I {}
static class I1 implements I {}
static class I2 implements I {}

private static void test(D o) {
if (o instanceof D(I data)) {
}
return -1;
}

public static class D {
public pattern D(Object v, Integer out) {
match D(1, 1);
public pattern D(I1 v) {
match D(new I1());
}

public pattern D(CharSequence v, Integer out) {
match D("2", 2);
public pattern D(I2 v) {
match D(new I2());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ DeconstructionPatternErrors.java:35:37: compiler.err.illegal.start.of.type
DeconstructionPatternErrors.java:37:28: compiler.err.illegal.start.of.type
DeconstructionPatternErrors.java:39:42: compiler.err.expected: ';'
DeconstructionPatternErrors.java:39:43: compiler.err.not.stmt
DeconstructionPatternErrors.java:15:29: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.util.List<java.lang.String>, java.util.ArrayList<java.lang.Integer>)
DeconstructionPatternErrors.java:15:26: compiler.err.no.compatible.matcher.found
DeconstructionPatternErrors.java:16:29: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, java.util.ArrayList<java.lang.Integer>
DeconstructionPatternErrors.java:17:29: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
DeconstructionPatternErrors.java:18:28: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)
DeconstructionPatternErrors.java:19:29: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, DeconstructionPatternErrors.P)
DeconstructionPatternErrors.java:17:26: compiler.err.no.compatible.matcher.found
DeconstructionPatternErrors.java:18:26: compiler.err.no.compatible.matcher.found
DeconstructionPatternErrors.java:19:26: compiler.err.no.compatible.matcher.found
DeconstructionPatternErrors.java:20:26: compiler.err.cant.apply.deconstruction.pattern: DeconstructionPatternErrors.P2
DeconstructionPatternErrors.java:21:26: compiler.err.cant.apply.deconstruction.pattern: DeconstructionPatternErrors.P2
DeconstructionPatternErrors.java:22:26: compiler.err.cant.apply.deconstruction.pattern: DeconstructionPatternErrors.P
Expand All @@ -15,7 +15,7 @@ DeconstructionPatternErrors.java:24:26: compiler.err.cant.apply.deconstruction.p
DeconstructionPatternErrors.java:24:36: compiler.err.cant.resolve.location: kindname.class, Unresolvable, , , (compiler.misc.location: kindname.class, DeconstructionPatternErrors, null)
DeconstructionPatternErrors.java:25:13: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, DeconstructionPatternErrors.GenRecord<java.lang.String>
DeconstructionPatternErrors.java:26:29: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, DeconstructionPatternErrors.GenRecord<java.lang.String>
DeconstructionPatternErrors.java:27:44: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, java.lang.Integer)
DeconstructionPatternErrors.java:27:26: compiler.err.no.compatible.matcher.found
DeconstructionPatternErrors.java:27:13: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, DeconstructionPatternErrors.GenRecord<java.lang.String>
DeconstructionPatternErrors.java:28:40: compiler.err.match.binding.exists
DeconstructionPatternErrors.java:29:56: compiler.err.already.defined: kindname.variable, v1, kindname.method, meth()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PrimitivePatternsSwitchErrors.java:15:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:24:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:31:24: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.Long)
PrimitivePatternsSwitchErrors.java:31:18: compiler.err.no.compatible.matcher.found
PrimitivePatternsSwitchErrors.java:70:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:78:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:84:18: compiler.err.prob.found.req: (compiler.misc.possible.loss.of.precision: long, byte)
Expand Down Expand Up @@ -29,7 +29,6 @@ PrimitivePatternsSwitchErrors.java:248:18: compiler.err.prob.found.req: (compile
PrimitivePatternsSwitchErrors.java:255:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.Long, int)
PrimitivePatternsSwitchErrors.java:261:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.Short, char)
PrimitivePatternsSwitchErrors.java:266:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: T, byte)
PrimitivePatternsSwitchErrors.java:30:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:37:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:44:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:52:16: compiler.err.not.exhaustive
Expand All @@ -43,4 +42,4 @@ PrimitivePatternsSwitchErrors.java:254:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:260:16: compiler.err.not.exhaustive
- compiler.note.preview.filename: PrimitivePatternsSwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
43 errors
42 errors
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
* @compile/fail/ref=OverloadedPatternDeclarationErrors.out -XDrawDiagnostics -XDdev OverloadedPatternDeclarationErrors.java
*/
public class OverloadedPatternDeclarationErrors {
interface I {}
static class I1 implements I {}
static class I2 implements I {}

private static int test(D o) {
if (o instanceof D(String data, Integer out)) { // no compatible matcher found
return out;
}
return -1;
}

private static int test2(D2 o) {
if (o instanceof D2(String data, Integer out)) { // ambiguous
return out;
private static void test2(D o) {
if (o instanceof D(I data)) { // ambiguity
}
return -1;
}

public static class D {
public pattern D(Object v1, Float out) {
match D(10.0f, 10.0f);
public pattern D(I1 v) {
match D(new I1());
}

public pattern D(Float out, Integer v1) {
match D(10.0f, 2);
public pattern D(I2 v) {
match D(new I2());
}
}

public static class D2 {
public pattern D2(Object v, Integer out) {
match D2("", 1);
public pattern D(Object v1, Float out) {
match D(10.0f, 10.0f);
}

public pattern D2(CharSequence v, Integer out) {
match D2("", 2);
public pattern D(Float out, Integer v1) {
match D(10.0f, 2);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OverloadedPatternDeclarationErrors.java:9:26: compiler.err.no.compatible.matcher.found
OverloadedPatternDeclarationErrors.java:16:26: compiler.err.matcher.overloading.ambiguity
OverloadedPatternDeclarationErrors.java:13:26: compiler.err.no.compatible.matcher.found
OverloadedPatternDeclarationErrors.java:20:26: compiler.err.matcher.overloading.ambiguity
- compiler.note.preview.filename: OverloadedPatternDeclarationErrors.java, DEFAULT
- compiler.note.preview.recompile
2 errors
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public void runTests() throws Exception {
public void testOne(Path base) throws Exception {
runSingle(base, "A", "B", "B", 2);
runSingle(base, "B", "C", "B", 1);
runSingle(base, "B", "C", "A", 0);
runSingle(base, "A", "B", "C", 0);
runSingle(base, "A", "C", "B", 0);
runSingle(base, "A", "D", "B", 0);
runSingle(base, "B", "C", "A", 1);
runSingle(base, "A", "B", "C", 2);
runSingle(base, "A", "C", "B", 2);
runSingle(base, "A", "D", "B", 2);
runSingle(base, "E", "F", "I", 0);

runSingle(base, "A", "B", "var", 1);
}

void runSingle(Path base, String bt_1, String bt_2, String t1, Integer selected) throws IOException {
Expand Down
Loading

0 comments on commit 77a1ffe

Please sign in to comment.