motoko icon indicating copy to clipboard operation
motoko copied to clipboard

fix: change `M0061` and `M0062` from warnings to errors

Open rvanasa opened this issue 10 months ago • 2 comments

This PR promotes M0061 ("comparing abstract type to itself at supertype") and M0062 ("comparing incompatible types at common supertype") from warnings to errors.

This is motivated by the unintuitive behavior of comparing at supertype Any, which always returns true. For instance, comparing 5 and ?4 returns true and therefore can result in buggy behavior or false positive test cases.

rvanasa avatar Feb 27 '25 19:02 rvanasa

I think this need some more thought. I think some use case might still be sensible but not sure.

crusso avatar Feb 27 '25 19:02 crusso

Here is a counterexample where a developer might want to be able to compare at a supertype:

type A = { x : Int; y : Int};
type B = { x : Int; z : Int};

let a : A = { x = 0; y = 1 };
let b : B = { x = 1; z = 2 };

a == b // => false, supertype warning

Output:

false : Bool
mo:7.1-7.7: warning [M0062], comparing incompatible types
  {x : Int; y : Int}
and
  {x : Int; z : Int}
at common supertype
  {x : Int}

Maybe we could remove the warning or at least have the same behavior as comparing with a subtype. Here is the current behavior for a similar case:

type A = { x : Int};
type B = { x : Int; z : Int};

let a : A = { x = 0 };
let b : B = { x = 1; z = 2 };

a == b // => false, no warning

Another option could be to error for types such as Any, (), {}, etc.

Motivating example cases:

{ a = 1 } == { a = -1 }; // => true, warning
{ a = 1 } == { b = 2 }; // => true, warning

rvanasa avatar Feb 27 '25 20:02 rvanasa