motoko icon indicating copy to clipboard operation
motoko copied to clipboard

Improve type argument inference error message

Open crusso opened this issue 5 years ago • 1 comments

import L "mo:stdlib/list";
var list = L.nil<Nat>();
list := L.push(5, list);
foo(L.last(list));

produces confusing error:

cannot implicitly instantiate function of type
  <T>List/211<T> -> ?T
to argument of type
  ?(Nat, List/211<Nat>)
to produce result of type
  Nat
no instantiation of T makes ?(Nat, List<Nat>) <: List<T> and ?T <: Nat

It looks like the compiler is just giving too much information here; not too little. The compiler should just be complaining that the return type will never match Nat, regardless of being able to take the argument type.

By comparison:

import L "mo:stdlib/list";
var list = L.nil<Nat>();
list := L.push(5, list);
let num = L.last(list);
foo(num);

gives a better error:

expression of type
  ?Nat
cannot produce expected type
  Nat

https://dfinity.slack.com/archives/CPVUFG8UV/p1587382898017100

crusso avatar Apr 21 '20 11:04 crusso

Could we amend the example here to include the definition of foo, as I think it's required to produce the problematic error here?

christoph-dfinity avatar Apr 15 '25 07:04 christoph-dfinity