nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.43k stars 1.47k forks source link

Invalid codegen with default argument invalid in generic context #13270

Closed refi64 closed 3 hours ago

refi64 commented 4 years ago

I couldn't really think of a better title (why do I always run into the weirdest bugs), so here's an example:

Example

type
  A = object
  B = object

proc f(a: A) = discard

proc g[T](value: T, cb: (proc(a: T)) = f) =
  cb value

# This should fail because there is no f(a: B) overload available
g B()

Current Output

/usercode/nimcache/@min.nim.c: In function 'NimMainModule':
/usercode/nimcache/@min.nim.c:138:34: error: expected expression before ')' token
  g__blFRD9cre3iSc5obJINpxvg(T1_, );
                                  ^
Error: execution of an external program failed: 'gcc -c  -w  -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c'

Expected Output

A compiler error because g(a: B) does not exist.

Possible Solution

I'm honestly not sure why this occurs, but it's definitely related to the default value of cb. If I remove the default and pass f as a second argument to g at the call site, an error is properly displayed.

$ nim -v
Nim Compiler Version 1.0.6
metagn commented 1 week ago

Now gives

Error: type mismatch: got <proc (a: A){.noSideEffect, gcsafe.}> but expected 'proc (a: B){.closure.}'
  Calling convention mismatch: got '{.nimcall.}', but expected '{.closure.}'.

probably since #24006