diff --git a/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java b/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java index 09194021..8c8dce1e 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java @@ -2153,7 +2153,7 @@ public abstract class TypeNode extends PklNode { @Override public boolean isEquivalentTo(TypeNode other) { - return other instanceof UIntTypeAliasTypeNode; + return other instanceof UIntTypeAliasTypeNode aliasTypeNode && mask == aliasTypeNode.mask; } @Override diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/as2.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/as2.pkl index 2c44d663..bdaf46e8 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/as2.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/as2.pkl @@ -22,6 +22,18 @@ examples { new Listing { 1; 2; 3 } as Listing module.catch(() -> (new Listing { 1; 2; 3 } as Listing)[0]) } + + ["cast Listing to other integer types"] { + local l = new Listing { 9999999 } + (l as Listing)[0] + (l as Listing)[0] + (l as Listing)[0] + (l as Listing)[0] + module.catch(() -> (l as Listing)[0]) + module.catch(() -> (l as Listing)[0]) + module.catch(() -> (l as Listing)[0]) + module.catch(() -> (l as Listing)[0]) + } ["mapping"] { module.catch(() -> new Listing { 1; 2; 3 } as Mapping) @@ -29,6 +41,18 @@ examples { module.catch(() -> new Mapping { ["Pigeon"] = 42; ["Barn Owl"] = 21 } as Mapping) } + ["cast Mapping<, UInt> to other integer types"] { + local m = new Mapping { ["x"] = 9999 } + (m as Mapping)["x"] + (m as Mapping)["x"] + (m as Mapping)["x"] + (m as Mapping)["x"] + (m as Mapping)["x"] + (m as Mapping)["x"] + module.catch(() -> (m as Mapping)["x"]) + module.catch(() -> (m as Mapping)["x"]) + } + ["union type"] { 42 as Int|String 42 as String|Int diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/as2.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/as2.pcf index 5ee246a6..64c13fb2 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/as2.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/as2.pcf @@ -21,6 +21,16 @@ examples { } "Expected value of type `String`, but got type `Int`. Value: 1" } + ["cast Listing to other integer types"] { + 9999999 + 9999999 + 9999999 + 9999999 + "Type constraint `isBetween(-32768, 32767)` violated. Value: 9999999" + "Type constraint `isBetween(0, 65535)` violated. Value: 9999999" + "Type constraint `isBetween(-128, 127)` violated. Value: 9999999" + "Type constraint `isBetween(0, 255)` violated. Value: 9999999" + } ["mapping"] { "Expected value of type `Mapping`, but got type `Listing`. Value: new Listing { ?; ?; ? }" new { @@ -29,6 +39,16 @@ examples { } "Expected value of type `Int`, but got type `String`. Value: \"Pigeon\"" } + ["cast Mapping<, UInt> to other integer types"] { + 9999 + 9999 + 9999 + 9999 + 9999 + 9999 + "Type constraint `isBetween(-128, 127)` violated. Value: 9999" + "Type constraint `isBetween(0, 255)` violated. Value: 9999" + } ["union type"] { 42 42