fixes 7980

This commit is contained in:
cooldome
2018-06-06 23:41:19 +01:00
parent bf394ed1a1
commit e03b3bdde7
2 changed files with 56 additions and 21 deletions

View File

@@ -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)

View 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)