From ee5adb87050ae7c0b96056a67dddc5d48174e695 Mon Sep 17 00:00:00 2001 From: Ronan Date: Tue, 30 Sep 2025 08:22:50 +0200 Subject: canonical type equality constraint (#8445) Fixes #8439 When checked, generic type equality constraints types are now in a canonical order, allowing for a commutative type equality operator. --------- Co-authored-by: Mukund Keshava --- .../generics/type-equality-cononical.slang | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/language-feature/generics/type-equality-cononical.slang (limited to 'tests') diff --git a/tests/language-feature/generics/type-equality-cononical.slang b/tests/language-feature/generics/type-equality-cononical.slang new file mode 100644 index 000000000..4eb682b10 --- /dev/null +++ b/tests/language-feature/generics/type-equality-cononical.slang @@ -0,0 +1,87 @@ +//TEST:SIMPLE(filecheck=CHECK): +//TEST:INTERPRET(filecheck=ICHECK): + +interface MyInterface +{ + associatedtype CompatibilityClass; + + __generic + This f(Other other) where Other::CompatibilityClass == This::CompatibilityClass; + + __generic + This g(Other other) where This::CompatibilityClass == Other::CompatibilityClass; + + CompatibilityClass toCompat(); +}; + +struct MyCompatibilityClass {}; + +__generic +struct MyStruct : MyInterface +{ + typealias CompatibilityClass = MyCompatibilityClass; + + __generic + This f(Other other) where CompatibilityClass == Other::CompatibilityClass + { + return this; + } + + __generic + This g(Other other) where MyCompatibilityClass == Other::CompatibilityClass + { + return this; + } + + CompatibilityClass toCompat() + { + return MyCompatibilityClass(); + } +}; + +struct TestInt : MyInterface +{ + typealias CompatibilityClass = int; + int value; + + __init(int v) + { + value = v; + } + + __generic + This f(Other other) where CompatibilityClass == Other::CompatibilityClass + { + return this; + } + + __generic + This g(Other other) where int == Other::CompatibilityClass + { + return this; + } + + int toCompat() + { + return value; + } +} + +__generic +void test(T t) + where int == T::CompatibilityClass +{ + printf("Success x %d!", t.toCompat()); +} + +void main() +{ + TestInt t = TestInt(12); + test(t); +} + +// CHECK-NOT: 30402 +// CHECK-NOT: 30404 +// CHECK-NOT: 30405 + +// ICHECK: Success x 12! \ No newline at end of file -- cgit v1.2.3