Nim: passing empty set/seq to template loses type information and errors

Created on 25 Jun 2019  路  1Comment  路  Source: nim-lang/Nim

passing {} to a template loses type information, giving an error

Example

type Foo = enum k1, k2
proc main()=
  # macro fun(opts: static set[Foo]) = # works
  # proc fun(opts: set[Foo]) = # works
  template fun(opts: set[Foo]) =
    discard opts + {k2}
  fun({})
main()

Current Output

Error: type mismatch: got <set[empty], set[Foo]>

Expected Output

should compile, as it does with macro or proc (see example)

Example2

Similar issue with empty seq:

  proc main()=
    template fun(opts: seq[int]) =
      discard opts
    fun(@[])

which gives:
Error: type mismatch: got <seq[empty], string, string, string>

Possible Solution

propagate type information when template argument provides a type; the template already does type checking when passing arguments to it and during overload resolution, but the type information gets lost after that

Additional Information

  • Your Nim version (output of nim -v).
Nim Compiler Version 0.20.99 [MacOSX: amd64]
Compiled at 2019-06-25
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 0d50b0c8a766a80c357edfcdda563f239caaf9e6
active boot switches: -d:release

potentially related (but different issue): https://github.com/nim-lang/Nim/issues/7117

  • Was it working in the previous Nim releases?
    no

EDIT

Semcheck Templates

Most helpful comment

Yay, @timotheecour, welcome back!

>All comments

Yay, @timotheecour, welcome back!

Was this page helpful?
0 / 5 - 0 ratings