Support for user-defined Eq impls (#1730)

Related to #1549; user-defined equality is a prerequisite for user-defined comparison.
This commit is contained in:
Thejaswi Kadur
2022-07-30 11:51:39 -07:00
committed by GitHub
parent b988d5353c
commit 5665f32bf7
18 changed files with 237 additions and 23 deletions
-3
View File
@@ -14,9 +14,6 @@ package ExplorerTest api;
fn Main() -> i32 {
var empty: {} = {};
empty = {};
if (not (empty == {})) {
return 1;
}
match (empty) {
case {} => {
return 0;
+7
View File
@@ -11,6 +11,13 @@
package ExplorerTest api;
// TODO: Implement this with some kind of reflection?
external impl {.x: i32, .y: i32} as EqWith(Self) {
fn Equal[me: Self](other: Self) -> Bool {
return me.x == other.x and me.y == other.y;
}
}
fn Main() -> i32 {
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
var t2: {.x: i32, .y: i32} = {.x = 5, .y = 2};
+7
View File
@@ -11,6 +11,13 @@
package ExplorerTest api;
// TODO: Implement this with some kind of reflection?
external impl {.x: i32, .y: i32} as EqWith(Self) {
fn Equal[me: Self](other: Self) -> Bool {
return me.x == other.x and me.y == other.y;
}
}
fn Main() -> i32 {
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
var t2: {.x: i32, .y: i32} = {.x = 5, .y = 4};
+8 -3
View File
@@ -7,12 +7,17 @@
package ExplorerTest api;
// TODO: Implement this with some kind of reflection?
external impl {.x: i32, .y: i32} as EqWith(Self) {
fn Equal[me: Self](other: Self) -> Bool {
return me.x == other.x and me.y == other.y;
}
}
fn Main() -> i32 {
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
var t2: {.x: i32,} = {.x = 5,};
// CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/struct/fail_equality_type.carbon:[[@LINE+3]]: type error in ==
// CHECK: expected: {.x: i32, .y: i32}
// CHECK: actual: {.x: i32}
// CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/struct/fail_equality_type.carbon:[[@LINE+1]]: {.x: i32, .y: i32} is not equality comparable with {.x: i32} (could not find implementation of interface EqWith(U = {.x: i32}) for {.x: i32, .y: i32})
if (t1 == t2) {
return 1;
} else {