From 818c5004b852ecd1bf7e91a9b45b04d61d586879 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Tue, 13 Feb 2024 15:00:50 +0100 Subject: [PATCH 1/3] added proper alias checks to the match implementations of parametrized types This fixes usethesource/rascal#1812 --- .../java/io/usethesource/vallang/type/AbstractDataType.java | 2 +- src/main/java/io/usethesource/vallang/type/ListType.java | 5 ++--- src/main/java/io/usethesource/vallang/type/MapType.java | 2 +- src/main/java/io/usethesource/vallang/type/SetType.java | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/usethesource/vallang/type/AbstractDataType.java b/src/main/java/io/usethesource/vallang/type/AbstractDataType.java index 874e9781..774b3241 100644 --- a/src/main/java/io/usethesource/vallang/type/AbstractDataType.java +++ b/src/main/java/io/usethesource/vallang/type/AbstractDataType.java @@ -428,7 +428,7 @@ public boolean match(Type matched, Map bindings) throws FactTypeUseE return false; } - if (matched.isAbstractData() || matched.isBottom()) { + if (matched.isAbstractData() || (matched.isAliased() && matched.getAliased().isAbstractData()) || matched.isBottom()) { return getTypeParameters().match(matched.getTypeParameters(), bindings); } diff --git a/src/main/java/io/usethesource/vallang/type/ListType.java b/src/main/java/io/usethesource/vallang/type/ListType.java index 3d4d00d2..97938f0c 100644 --- a/src/main/java/io/usethesource/vallang/type/ListType.java +++ b/src/main/java/io/usethesource/vallang/type/ListType.java @@ -286,12 +286,11 @@ protected Type glbWithList(Type type) { } @Override - public boolean match(Type matched, Map bindings) - throws FactTypeUseException { + public boolean match(Type matched, Map bindings) throws FactTypeUseException { if (!super.match(matched, bindings)) { return false; } - else if (matched.isList() || matched.isBottom()) { + else if (matched.isList() || (matched.isAliased() && matched.getAliased().isList()) || matched.isBottom()) { return getElementType().match(matched.getElementType(), bindings); } diff --git a/src/main/java/io/usethesource/vallang/type/MapType.java b/src/main/java/io/usethesource/vallang/type/MapType.java index 3252391b..8d66c071 100644 --- a/src/main/java/io/usethesource/vallang/type/MapType.java +++ b/src/main/java/io/usethesource/vallang/type/MapType.java @@ -244,7 +244,7 @@ public boolean match(Type matched, Map bindings)throws FactTypeUseEx return false; } - if (matched.isMap() || matched.isBottom()) { + if (matched.isMap() || (matched.isAliased() && matched.getAliased().isMap()) || matched.isBottom()) { return getKeyType().match(matched.getKeyType(), bindings) && getValueType().match(matched.getValueType(), bindings); } diff --git a/src/main/java/io/usethesource/vallang/type/SetType.java b/src/main/java/io/usethesource/vallang/type/SetType.java index 44f3ca0f..7276ab9b 100644 --- a/src/main/java/io/usethesource/vallang/type/SetType.java +++ b/src/main/java/io/usethesource/vallang/type/SetType.java @@ -289,7 +289,7 @@ public boolean match(Type matched, Map bindings) throws FactTypeUseE if (!super.match(matched, bindings)) { return false; } - else if (matched.isSet() || matched.isBottom()) { + else if (matched.isSet() || (matched.isAliased() && matched.getAliased().isSet()) || matched.isBottom()) { return getElementType().match(matched.getElementType(), bindings); } From de61c18acb15a712642a717857d58cd593812318 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Tue, 13 Feb 2024 17:17:52 +0100 Subject: [PATCH 2/3] added failing test for #1812 which does not fail anymore --- .../vallang/specification/TypeTest.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/usethesource/vallang/specification/TypeTest.java b/src/test/java/io/usethesource/vallang/specification/TypeTest.java index f56a0645..0ba79af3 100644 --- a/src/test/java/io/usethesource/vallang/specification/TypeTest.java +++ b/src/test/java/io/usethesource/vallang/specification/TypeTest.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -168,13 +169,27 @@ public void testParameterizedAlias(TypeFactory ft, TypeStore ts) { Type T = ft.parameterType("T"); // DiGraph[&T] = rel[&T from ,&T to] @SuppressWarnings("deprecation") - Type DiGraph = ft.aliasType(ts, "DiGraph", ft.relType(T, "from", T, "to"), T); + Type relType = ft.relType(T, "from", T, "to"); + Type DiGraph = ft.aliasType(ts, "DiGraph", relType, T); Type IntInstance = ft.relType(ft.integerType(), ft.integerType()); Type ValueInstance = ft.relType(ft.valueType(), ft.valueType()); - // after instantiation rel[int,int] is a sub-type of rel[&T, &T] + // after instantiation rel[int,int] is a sub-type of rel[&T, &T], and vice versa, and also reflectively assertTrue(IntInstance.isSubtypeOf(DiGraph)); - + assertTrue(DiGraph.isSubtypeOf(IntInstance)); + assertTrue(DiGraph.isSubtypeOf(DiGraph)); + + // vice versa: the aliased type can be matched using the alias type + Map bindings = new HashMap<>(); + assertTrue(DiGraph.match(IntInstance, bindings)); + assertTrue(bindings.get(T) == ft.integerType()); + + // and the alias type can be matched by the aliased type (not we reuse the bindings from the previous assert) + Type instanceDiGraph = DiGraph.instantiate(bindings); + bindings = new HashMap<>(); // reset the bindings + assertTrue(relType.match(instanceDiGraph, bindings)); + assertTrue(bindings.get(T) == ft.integerType()); + // before instantiation, the parameterized type rel[&T, &T] // could be instantiated later by rel[int, int] assertTrue(DiGraph.isSubtypeOf(IntInstance)); @@ -182,7 +197,7 @@ public void testParameterizedAlias(TypeFactory ft, TypeStore ts) { // the generic graph is also always a sub-type of the most general instantiation assertTrue(DiGraph.isSubtypeOf(ValueInstance)); - Map bindings = new HashMap<>(); + bindings = new HashMap<>(); DiGraph.match(IntInstance, bindings); assertTrue(bindings.get(T) == ft.integerType()); From ad23ea6727fc1db59815a11f136379a2de5e2d5b Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Tue, 13 Feb 2024 17:22:16 +0100 Subject: [PATCH 3/3] added missing tuple case --- src/main/java/io/usethesource/vallang/type/TupleType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/usethesource/vallang/type/TupleType.java b/src/main/java/io/usethesource/vallang/type/TupleType.java index ed5c8d52..b0a46a8a 100644 --- a/src/main/java/io/usethesource/vallang/type/TupleType.java +++ b/src/main/java/io/usethesource/vallang/type/TupleType.java @@ -419,7 +419,7 @@ public boolean match(Type matched, Map bindings) return false; } - if (matched.isTuple() || matched.isBottom()) { + if (matched.isTuple() || (matched.isAliased() && matched.getAliased().isTuple()) || matched.isBottom()) { for (int i = getArity() - 1; i >= 0; i--) { if (!getFieldType(i).match(matched.getFieldType(i), bindings)) { return false;