Skip to content

Commit 1236018

Browse files
authored
Warns for structural equality on abstract types (#1972)
This is achieved by making promotion local to finding the lub of the arguments. I've changed the error message slightly, because "incompatible" seems like the wrong description now.
1 parent 808009d commit 1236018

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/mo_frontend/typing.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,9 @@ and infer_exp'' env exp : T.typ =
770770
end;
771771
t
772772
| RelE (ot, exp1, op, exp2) ->
773-
let t1 = infer_exp_promote env exp1 in
774-
let t2 = infer_exp_promote env exp2 in
775-
let t = Operator.type_relop op (T.lub t1 t2) in
773+
let t1 = T.normalize (infer_exp env exp1) in
774+
let t2 = T.normalize (infer_exp env exp2) in
775+
let t = Operator.type_relop op (T.lub (T.promote t1) (T.promote t2)) in
776776
if not env.pre then begin
777777
assert (!ot = Type.Pre);
778778
if not (Operator.has_relop op t) then
@@ -781,11 +781,17 @@ and infer_exp'' env exp : T.typ =
781781
(T.string_of_typ_expand t1)
782782
(T.string_of_typ_expand t2);
783783
if not (T.eq t t1 || T.eq t t2) then
784-
warn env exp.at
785-
"comparing incompatible types\n %s\nand\n %s\nat common supertype\n %s"
786-
(T.string_of_typ_expand t1)
787-
(T.string_of_typ_expand t2)
788-
(T.string_of_typ_expand t);
784+
if T.eq t1 t2 then
785+
warn env exp.at
786+
"comparing abstract type\n %s\nto itself at supertype\n %s"
787+
(T.string_of_typ_expand t1)
788+
(T.string_of_typ_expand t)
789+
else
790+
warn env exp.at
791+
"comparing incompatible types\n %s\nand\n %s\nat common supertype\n %s"
792+
(T.string_of_typ_expand t1)
793+
(T.string_of_typ_expand t2)
794+
(T.string_of_typ_expand t);
789795
ot := t;
790796
end;
791797
T.bool
@@ -2277,4 +2283,3 @@ let check_lib scope lib : Scope.t Diag.result =
22772283
Scope.lib lib.note imp_typ
22782284
) lib
22792285
)
2280-

test/fail/ok/structural_equality.tc.ok

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ structural_equality.mo:10.8-10.18: warning, comparing incompatible types
2020
and
2121
Text
2222
Any
23+
structural_equality.mo:12.37-12.43: warning, comparing abstract type
24+
A/10
25+
to itself at supertype
26+
Any
27+
structural_equality.mo:13.41-13.47: warning, comparing incompatible types
28+
A/11
29+
and
30+
B
31+
Any

test/fail/structural_equality.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ A() == A();
88
ignore ({ x = 10 } == { var x = 10 });
99

1010
assert(10 == "hi");
11+
12+
func myEq<A>(x : A, y : A) : Bool = x == y;
13+
func myEq2<A, B>(x : A, y : B) : Bool = x == y;

0 commit comments

Comments
 (0)