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.48k stars 1.47k forks source link

Nim Devel generates invalid C code #17397

Open HJarausch opened 3 years ago

HJarausch commented 3 years ago
proc FP[T](A: var openArray[T], myCmp : proc(x,y:T):T): int =
  1

proc FS[T](A: var openArray[T], myCmp : proc(x,y:T):T = system.cmp[T]): int =
  result= FP(A, myCmp)

const
  N= 6
  K= 3
var A :array[N,float]= [7.0,4,6,3,9,1]

func myCmp(x,y:float):float =
  (if x==y : 0 elif x<y : -1 else: 1)

echo FS(A)
# echo FS(A,myCmp)  # this compiles fine

#[
/tmp/jarausch/.cache/nim/BUG_d/@mBUG.nim.c: In function 'NimMainModule':
/tmp/jarausch/.cache/nim/BUG_d/@mBUG.nim.c:185:69: error: expected expression before ')' token
  185 |  T2_ = FS__M7cQ7xfR9acKDe9cYix8OwcA(A__a1TOBMZ6yaU79afZ08aXS9cg, 6, );
      |                                                                     ^
Error: execution of an external compiler program 'x86_64-pc-linux-gnu-gcc -c  -w -fmax-errors=3   -I/usr/lib/nim -I/home/jarausch/Nim_My/Math -o /tmp/jarausch/.cache/nim/BUG_d/@mBUG.nim.c.o /tmp/jarausch/.cache/nim/BUG_d/@mBUG.nim.c' failed with exit code: 1
]#

nim -v

Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-03-16
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 48eab5370a5e731059a7337f702b2bc42a3c4b79
active boot switches: -d:release
timotheecour commented 3 years ago

please check whether you can minimize this without using generics

HJarausch commented 3 years ago

My programme was in error (I wished I got a sensible error message). If the programme is corrected to

proc FP[T](A: var openArray[T], myCmp : proc(x,y:T):int {.nimcall}): int =
  1

proc FS[T](A: var openArray[T], myCmp : proc(x,y:T):int {.nimcall} = system.cmp[T]): int =
  result= FP(A, myCmp)

const
  N= 6
  K= 3
var A :array[N,float]= [7.0,4,6,3,9,1]

func myCmp(x,y:float):int =
  (if x==y : 0 elif x<y : -1 else: 1)

echo FS(A)

it compiles and runs just fine.

ghost commented 3 years ago

Even if it's a mistake in the code Nim should output an error instead of making invalid C code :)