mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
fixes #4227
This commit is contained in:
@@ -59,7 +59,7 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
|
||||
evalTemplateAux(templ.sons[i], actual, c, 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 ``()``
|
||||
# `n` is then a nkSym or something similar
|
||||
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
|
||||
# wiser to just deprecate immediate templates and macros
|
||||
# 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
|
||||
expectedRegularParams = <s.typ.len
|
||||
givenRegularParams = totalParams - genericParams
|
||||
@@ -92,6 +92,7 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||
for i in givenRegularParams+1 .. expectedRegularParams:
|
||||
let default = s.typ.n.sons[i].sym.ast
|
||||
if default.isNil or default.kind == nkEmpty:
|
||||
echo "fuck you ", genericParams
|
||||
localError(n.info, errWrongNumberOfArguments)
|
||||
addSon(result, ast.emptyNode)
|
||||
else:
|
||||
@@ -104,14 +105,14 @@ proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||
var evalTemplateCounter* = 0
|
||||
# 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)
|
||||
if evalTemplateCounter > 100:
|
||||
globalError(n.info, errTemplateInstantiationTooNested)
|
||||
result = n
|
||||
|
||||
# replace each param by the corresponding node:
|
||||
var args = evalTemplateArgs(n, tmpl)
|
||||
var args = evalTemplateArgs(n, tmpl, fromHlo)
|
||||
var ctx: TemplCtx
|
||||
ctx.owner = tmpl
|
||||
ctx.genSymOwner = genSymOwner
|
||||
|
||||
@@ -24,7 +24,7 @@ proc evalPattern(c: PContext, n, orig: PNode): PNode =
|
||||
of skMacro:
|
||||
result = semMacroExpr(c, n, orig, s)
|
||||
of skTemplate:
|
||||
result = semTemplateExpr(c, n, s)
|
||||
result = semTemplateExpr(c, n, s, {efFromHlo})
|
||||
else:
|
||||
result = semDirectOp(c, n, {})
|
||||
if optHints in gOptions and hintPattern in gNotes:
|
||||
|
||||
@@ -47,7 +47,7 @@ type
|
||||
efLValue, efWantIterator, efInTypeof,
|
||||
efWantStmt, efAllowStmt, efDetermineType,
|
||||
efAllowDestructor, efWantValue, efOperand, efNoSemCheck,
|
||||
efNoProcvarCheck
|
||||
efNoProcvarCheck, efFromHlo
|
||||
TExprFlags* = set[TExprFlag]
|
||||
|
||||
TTypeAttachedOp* = enum
|
||||
|
||||
@@ -15,7 +15,7 @@ proc semTemplateExpr(c: PContext, n: PNode, s: PSym,
|
||||
markUsed(n.info, s)
|
||||
styleCheckUse(n.info, s)
|
||||
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)
|
||||
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()
|
||||
Reference in New Issue
Block a user