mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-03 18:34:43 +00:00
fixes 7980
This commit is contained in:
@@ -36,27 +36,35 @@ proc applyPatterns(c: PContext, n: PNode): PNode =
|
||||
# we apply the last pattern first, so that pattern overriding is possible;
|
||||
# however the resulting AST would better not trigger the old rule then
|
||||
# anymore ;-)
|
||||
for i in countdown(c.patterns.len-1, 0):
|
||||
let pattern = c.patterns[i]
|
||||
if not isNil(pattern):
|
||||
let x = applyRule(c, pattern, result)
|
||||
if not isNil(x):
|
||||
assert x.kind in {nkStmtList, nkCall}
|
||||
# better be safe than sorry, so check evalTemplateCounter too:
|
||||
inc(evalTemplateCounter)
|
||||
if evalTemplateCounter > evalTemplateLimit:
|
||||
globalError(c.config, n.info, "template instantiation too nested")
|
||||
# deactivate this pattern:
|
||||
c.patterns[i] = nil
|
||||
if x.kind == nkStmtList:
|
||||
assert x.len == 3
|
||||
x.sons[1] = evalPattern(c, x.sons[1], result)
|
||||
result = flattenStmts(x)
|
||||
else:
|
||||
result = evalPattern(c, x, result)
|
||||
dec(evalTemplateCounter)
|
||||
# activate this pattern again:
|
||||
c.patterns[i] = pattern
|
||||
if c.patterns.len > 0:
|
||||
|
||||
# temporary disable converters
|
||||
var ctx_converters: TSymSeq
|
||||
shallowCopy(ctx_converters, c.converters)
|
||||
c.converters = @[]
|
||||
defer: shallowCopy(c.converters, ctx_converters)
|
||||
|
||||
for i in countdown(c.patterns.len-1, 0):
|
||||
let pattern = c.patterns[i]
|
||||
if not isNil(pattern):
|
||||
let x = applyRule(c, pattern, result)
|
||||
if not isNil(x):
|
||||
assert x.kind in {nkStmtList, nkCall}
|
||||
# better be safe than sorry, so check evalTemplateCounter too:
|
||||
inc(evalTemplateCounter)
|
||||
if evalTemplateCounter > evalTemplateLimit:
|
||||
globalError(c.config, n.info, "template instantiation too nested")
|
||||
# deactivate this pattern:
|
||||
c.patterns[i] = nil
|
||||
if x.kind == nkStmtList:
|
||||
assert x.len == 3
|
||||
x.sons[1] = evalPattern(c, x.sons[1], result)
|
||||
result = flattenStmts(x)
|
||||
else:
|
||||
result = evalPattern(c, x, result)
|
||||
dec(evalTemplateCounter)
|
||||
# activate this pattern again:
|
||||
c.patterns[i] = pattern
|
||||
|
||||
proc hlo(c: PContext, n: PNode): PNode =
|
||||
inc(c.hloLoopDetector)
|
||||
|
||||
27
tests/template/tpattern_with_converter.nim
Normal file
27
tests/template/tpattern_with_converter.nim
Normal file
@@ -0,0 +1,27 @@
|
||||
discard """
|
||||
output: 10.0
|
||||
"""
|
||||
|
||||
type
|
||||
MyFloat = object
|
||||
val: float
|
||||
|
||||
converter to_myfloat*(x: float): MyFloat {.inline.} =
|
||||
MyFloat(val: x)
|
||||
|
||||
proc `+`(x1, x2: MyFloat): MyFloat =
|
||||
MyFloat(val: x1.val + x2.val)
|
||||
|
||||
proc `*`(x1, x2: MyFloat): MyFloat =
|
||||
MyFloat(val: x1.val * x2.val)
|
||||
|
||||
template optMul{`*`(a, 2.0)}(a: MyFloat): MyFloat =
|
||||
a + a
|
||||
|
||||
func floatMyFloat(x: MyFloat): MyFloat =
|
||||
result = x * 2.0
|
||||
|
||||
func floatDouble(x: float): float =
|
||||
result = x * 2.0
|
||||
|
||||
echo floatDouble(5)
|
||||
Reference in New Issue
Block a user