changeset 56796:e6e27550e2d5 patterns-deconstruction

Using subtyping to find patterns.
author jlahoda
date Mon, 17 Jun 2019 17:14:55 +0200
parents 424c4d694771
children 321d2ca49a87
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java test/langtools/tools/javac/patterns/SimpleDeconstructionPattern.java
diffstat 2 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jun 14 13:35:47 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jun 17 17:14:55 2019 +0200
@@ -3882,7 +3882,7 @@
             while (sig[idx[0]] != ')') {//TODO: handle errors
                 patternComponents.append(reader.decodeType(env.toplevel.modle, sig, idx));
             }
-            if (isSameTypesIgnoreNone(components.toList(), patternComponents.toList())) {
+            if (isSubTypesIgnoreNone(components.toList(), patternComponents.toList())) {
                 //found a pattern:
                 foundPatterns = foundPatterns.prepend(Pair.of((MethodSymbol) pattern, patternComponents.toList()));
             }
@@ -3905,9 +3905,9 @@
         result = tree.type;
     }
 
-    private boolean isSameTypesIgnoreNone(List<Type> ts, List<Type> ss) {
+    private boolean isSubTypesIgnoreNone(List<Type> ts, List<Type> ss) {
         while (ts.tail != null && ss.tail != null &&
-               (ts.head == Type.noType || types.isSameType(ts.head, ss.head))) {
+               (ts.head == Type.noType || types.isSubtype(ts.head, ss.head))) {
             ts = ts.tail;
             ss = ss.tail;
         }
--- a/test/langtools/tools/javac/patterns/SimpleDeconstructionPattern.java	Fri Jun 14 13:35:47 2019 +0200
+++ b/test/langtools/tools/javac/patterns/SimpleDeconstructionPattern.java	Mon Jun 17 17:14:55 2019 +0200
@@ -49,6 +49,9 @@
         if (!test6(new P(41))) {
             throw new IllegalStateException();
         }
+        if (!((new BaseUse(new BaseSubclass(0))) instanceof BaseUse(BaseSubclass(0)))) {
+            throw new IllegalStateException();
+        }
     }
 
     private static boolean test1(Object o) throws Throwable {
@@ -79,5 +82,9 @@
     }
 
     public record P2(P p, String s) {
-    }    
+    }
+
+    public interface Base {}
+    public record BaseUse(Base b) {}
+    public record BaseSubclass(int i) implements Base {}
 }
\ No newline at end of file