The problem comes from the fact that macroOrTmpl[...] is transformed by
semSubscript which is trying to evaluate macroOrTmpl identifier in place. This
is okay for non-generic macros or templates, but wrong for generic ones, that
do not have a chance to receive their generic arguments explicitly specified in
brackets.

Solution:

1. macroOrTmpl[...] where macroOrTmpl is non-generic macro or template, then
   macroOrTmpl is evaluated before applying brackets. (as before)

2. macroOrTmpl[...] where macroOrTmpl is generic macro or template, then if:

   a. It comes from macroOrTmpl[...](...) call expr (efInCall), then macroOrTmpl
      is turned into a symbol (efNoEvaluate) rather than evaluating it in place,
      then whole bracket expr is returned to semIndirectOp which transforms it
      to proper generic macro or template call with explicit generic arguments.

   b. macroOrTmpl[...] does not come from call expr, as above macroOrTmpl is
      transformed to symbol, then it is transformed into proper generic macro or
      template call with explicit generic arguments and no normal arguments.
This commit is contained in:
Adam Strzelecki 2015-10-29 22:10:09 +01:00
commit 47e45dee7e
3 changed files with 88 additions and 5 deletions

View file

@ -0,0 +1,31 @@
discard """
output: '''1
1
1
1
999
999
999
2'''
"""
# test if we can pass explicit generic arguments to generic templates
# based on bug report #3496
proc tproc[T](t: T = 999) = echo t
template ttmpl[T](t: T = 999) = echo t
tproc(1)
tproc[int](1)
ttmpl(1)
ttmpl[int](1) #<- crash case #1
tproc[int]()
discard tproc[int]
ttmpl[int]() #<- crash case #2
ttmpl[int] #<- crash case #3
# but still allow normal use of [] on non-generic templates
template tarr: expr = [1, 2, 3, 4]
echo tarr[1]