Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Hole in auto allows to compile incorrect code

Open jmgomez opened this issue 2 years ago • 1 comments

What happened?

Probably this is already reported, but couldnt find it anywhere. When using auto, and it seems sugar uses it, the compiler allows you to write incorrect code.

Notice in the code below, that if you specify the type for at least one parameter, it works as expected by dont compiling the code.


import sugar

proc example(fn : int -> int)  =
    discard $fn(2)

#The code below is incorrect and compiles
example((n)=>"test")
example((n:auto)=>"test")
example(proc(n:auto):auto = "test")
example(proc(n:auto):auto = discard)

#Works: Doesnt compile
example((n:int)=>"test") 
example(proc(n:auto):int = discard) 
example(proc(n:int):auto = discard) 

Nim Version

Nim Compiler Version 1.7.1 [MacOSX: amd64]
Compiled at 2022-07-01
Copyright (c) 2006-2022 by Andreas Rumpf

Current Standard Output Logs

nim c --run main.nim
Hint: used config file '/Volumes/Store/Nim/config/nim.cfg' [Conf]
Hint: used config file '/Volumes/Store/Nim/config/config.nims' [Conf]
Hint: used config file '/Volumes/Store/Projects/nimlab/playground/config.nims' [Conf]
.............................................................
CC: main.nim
Hint:  [Link]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
38687 lines; 0.385s; 39.43MiB peakmem; proj: /Volumes/Store/Projects/nimlab/playground/main.nim; out: /Volumes/Store/Projects/nimlab/playground/main [SuccessX]
Hint: /Volumes/Store/Projects/nimlab/playground/main [Exec]

Expected Standard Output Logs

/Volumes/Store/Projects/nimlab/playground/main.nim(11, 8) Error: type mismatch: got <proc (n: int): string{.noSideEffect, gcsafe, locks: 0.}>
but expected one of:
proc example(fn: int -> int)
  first type mismatch at position: 1
  required type for fn: proc (i0: int): int{.closure.}
  but expression 'proc (n: int): auto = result = "test"' is of type: proc (n: int): string{.noSideEffect, gcsafe, locks: 0.}

expression: example(proc (n: int): auto = result = "test")

Possible Solution

No response

Additional Information

No response

jmgomez avatar Aug 06 '22 13:08 jmgomez

related: https://github.com/nim-lang/Nim/issues/15836

ringabout avatar Aug 06 '22 14:08 ringabout

after https://github.com/nim-lang/Nim/pull/21065

t20171.nim(7, 14) Error: type mismatch: got <string> but expected 'int'
t20171.nim(8, 19) Error: type mismatch: got <string> but expected 'int'
t20171.nim(9, 29) Error: type mismatch: got <string> but expected 'int'
t20171.nim(12, 8) Error: type mismatch: got <proc (n: int): string{.noSideEffect, gcsafe.}>
but expected one of:
proc example(fn: int -> int)
  first type mismatch at position: 1
  required type for fn: proc (i0: int): int{.closure.}
  but expression 'proc (n: int): auto = result = "test"' is of type: proc (n: int): string{.noSideEffect, gcsafe.}
  Calling convention mismatch: got '{.nimcall.}', but expected '{.closure.}'.

expression: example(proc (n: int): auto = result = "test")
t20171.nim(14, 8) Error: type mismatch: got <proc (n: int){.noSideEffect, gcsafe.}>
but expected one of:
proc example(fn: int -> int)
  first type mismatch at position: 1
  required type for fn: proc (i0: int): int{.closure.}
  but expression 'proc (n: int): auto = discard' is of type: proc (n: int){.noSideEffect, gcsafe.}
  Calling convention mismatch: got '{.nimcall.}', but expected '{.closure.}'.

expression: example(proc (n: int): auto = discard )

bung87 avatar Dec 11 '22 16:12 bung87