fixes #4227
This commit is contained in:
parent
6ff66bfd51
commit
6d76df8454
5 changed files with 32 additions and 7 deletions
|
|
@ -59,7 +59,7 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
|
||||||
evalTemplateAux(templ.sons[i], actual, c, res)
|
evalTemplateAux(templ.sons[i], actual, c, res)
|
||||||
result.add res
|
result.add res
|
||||||
|
|
||||||
proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
proc evalTemplateArgs(n: PNode, s: PSym; fromHlo: bool): PNode =
|
||||||
# if the template has zero arguments, it can be called without ``()``
|
# if the template has zero arguments, it can be called without ``()``
|
||||||
# `n` is then a nkSym or something similar
|
# `n` is then a nkSym or something similar
|
||||||
var totalParams = case n.kind
|
var totalParams = case n.kind
|
||||||
|
|
@ -75,7 +75,7 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||||
# their bodies. We could try to fix this, but it may be
|
# their bodies. We could try to fix this, but it may be
|
||||||
# wiser to just deprecate immediate templates and macros
|
# wiser to just deprecate immediate templates and macros
|
||||||
# now that we have working untyped parameters.
|
# now that we have working untyped parameters.
|
||||||
genericParams = if sfImmediate in s.flags: 0
|
genericParams = if sfImmediate in s.flags or fromHlo: 0
|
||||||
else: s.ast[genericParamsPos].len
|
else: s.ast[genericParamsPos].len
|
||||||
expectedRegularParams = <s.typ.len
|
expectedRegularParams = <s.typ.len
|
||||||
givenRegularParams = totalParams - genericParams
|
givenRegularParams = totalParams - genericParams
|
||||||
|
|
@ -92,6 +92,7 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||||
for i in givenRegularParams+1 .. expectedRegularParams:
|
for i in givenRegularParams+1 .. expectedRegularParams:
|
||||||
let default = s.typ.n.sons[i].sym.ast
|
let default = s.typ.n.sons[i].sym.ast
|
||||||
if default.isNil or default.kind == nkEmpty:
|
if default.isNil or default.kind == nkEmpty:
|
||||||
|
echo "fuck you ", genericParams
|
||||||
localError(n.info, errWrongNumberOfArguments)
|
localError(n.info, errWrongNumberOfArguments)
|
||||||
addSon(result, ast.emptyNode)
|
addSon(result, ast.emptyNode)
|
||||||
else:
|
else:
|
||||||
|
|
@ -104,14 +105,14 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||||
var evalTemplateCounter* = 0
|
var evalTemplateCounter* = 0
|
||||||
# to prevent endless recursion in templates instantiation
|
# to prevent endless recursion in templates instantiation
|
||||||
|
|
||||||
proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym): PNode =
|
proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym; fromHlo=false): PNode =
|
||||||
inc(evalTemplateCounter)
|
inc(evalTemplateCounter)
|
||||||
if evalTemplateCounter > 100:
|
if evalTemplateCounter > 100:
|
||||||
globalError(n.info, errTemplateInstantiationTooNested)
|
globalError(n.info, errTemplateInstantiationTooNested)
|
||||||
result = n
|
result = n
|
||||||
|
|
||||||
# replace each param by the corresponding node:
|
# replace each param by the corresponding node:
|
||||||
var args = evalTemplateArgs(n, tmpl)
|
var args = evalTemplateArgs(n, tmpl, fromHlo)
|
||||||
var ctx: TemplCtx
|
var ctx: TemplCtx
|
||||||
ctx.owner = tmpl
|
ctx.owner = tmpl
|
||||||
ctx.genSymOwner = genSymOwner
|
ctx.genSymOwner = genSymOwner
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ proc evalPattern(c: PContext, n, orig: PNode): PNode =
|
||||||
of skMacro:
|
of skMacro:
|
||||||
result = semMacroExpr(c, n, orig, s)
|
result = semMacroExpr(c, n, orig, s)
|
||||||
of skTemplate:
|
of skTemplate:
|
||||||
result = semTemplateExpr(c, n, s)
|
result = semTemplateExpr(c, n, s, {efFromHlo})
|
||||||
else:
|
else:
|
||||||
result = semDirectOp(c, n, {})
|
result = semDirectOp(c, n, {})
|
||||||
if optHints in gOptions and hintPattern in gNotes:
|
if optHints in gOptions and hintPattern in gNotes:
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ type
|
||||||
efLValue, efWantIterator, efInTypeof,
|
efLValue, efWantIterator, efInTypeof,
|
||||||
efWantStmt, efAllowStmt, efDetermineType,
|
efWantStmt, efAllowStmt, efDetermineType,
|
||||||
efAllowDestructor, efWantValue, efOperand, efNoSemCheck,
|
efAllowDestructor, efWantValue, efOperand, efNoSemCheck,
|
||||||
efNoProcvarCheck
|
efNoProcvarCheck, efFromHlo
|
||||||
TExprFlags* = set[TExprFlag]
|
TExprFlags* = set[TExprFlag]
|
||||||
|
|
||||||
TTypeAttachedOp* = enum
|
TTypeAttachedOp* = enum
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ proc semTemplateExpr(c: PContext, n: PNode, s: PSym,
|
||||||
markUsed(n.info, s)
|
markUsed(n.info, s)
|
||||||
styleCheckUse(n.info, s)
|
styleCheckUse(n.info, s)
|
||||||
pushInfoContext(n.info)
|
pushInfoContext(n.info)
|
||||||
result = evalTemplate(n, s, getCurrOwner())
|
result = evalTemplate(n, s, getCurrOwner(), efFromHlo in flags)
|
||||||
if efNoSemCheck notin flags: result = semAfterMacroCall(c, result, s, flags)
|
if efNoSemCheck notin flags: result = semAfterMacroCall(c, result, s, flags)
|
||||||
popInfoContext()
|
popInfoContext()
|
||||||
|
|
||||||
|
|
|
||||||
24
tests/trmacros/tstatic_t_bug.nim
Normal file
24
tests/trmacros/tstatic_t_bug.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
discard """
|
||||||
|
output: "optimized"
|
||||||
|
"""
|
||||||
|
# bug #4227
|
||||||
|
type Vector64[N: static[int]] = array[N, int]
|
||||||
|
|
||||||
|
proc `*`*[N: static[int]](a: Vector64[N]; b: float64): Vector64[N] =
|
||||||
|
result = a
|
||||||
|
|
||||||
|
proc `+=`*[N: static[int]](a: var Vector64[N]; b: Vector64[N]) =
|
||||||
|
echo "regular"
|
||||||
|
|
||||||
|
proc linearCombinationMut[N: static[int]](a: float64, v: var Vector64[N], w: Vector64[N]) {. inline .} =
|
||||||
|
echo "optimized"
|
||||||
|
|
||||||
|
template rewriteLinearCombinationMut*{v += `*`(w, a)}(a: float64, v: var Vector64, w: Vector64): auto =
|
||||||
|
linearCombinationMut(a, v, w)
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
const scaleVal = 9.0
|
||||||
|
var a, b: Vector64[7]
|
||||||
|
a += b * scaleval
|
||||||
|
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue