From 6543040d40b063ce2c872f582c153c6cff9ef0aa Mon Sep 17 00:00:00 2001 From: Peter Munch-Ellingsen Date: Fri, 21 Nov 2025 21:26:43 +0100 Subject: [PATCH] Fixes #25304 proper test for hlo recursion limit (#25305) The `warnUser` message kind is probably not the right one, but I left it as a placeholder. It should probably at least warn if not just straight up throw an error, was very hard to figure out what went wrong without any indication. The hard coded 300 should possibly also be changed to `evalTemplateLimit` or the VM call recursion limit or something. --- compiler/hlo.nim | 19 +++++++++---------- compiler/semdata.nim | 1 - 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/compiler/hlo.nim b/compiler/hlo.nim index 9fdec38c0e..94ee96f541 100644 --- a/compiler/hlo.nim +++ b/compiler/hlo.nim @@ -10,7 +10,7 @@ # This include implements the high level optimization pass. # included from sem.nim -proc hlo(c: PContext, n: PNode): PNode +proc hlo(c: PContext, n: PNode, loopDetector: int): PNode proc evalPattern(c: PContext, n, orig: PNode): PNode = internalAssert c.config, n.kind == nkCall and n[0].kind == nkSym @@ -61,10 +61,11 @@ proc applyPatterns(c: PContext, n: PNode): PNode = # activate this pattern again: c.patterns[i] = pattern -proc hlo(c: PContext, n: PNode): PNode = - inc(c.hloLoopDetector) +proc hlo(c: PContext, n: PNode, loopDetector: int): PNode = # simply stop and do not perform any further transformations: - if c.hloLoopDetector > 300: return n + if loopDetector > 300: + message(c.config, n.info, warnUser, "term rewrite macro instantiation too nested") + return n case n.kind of nkMacroDef, nkTemplateDef, procDefs: # already processed (special cases in semstmts.nim) @@ -80,7 +81,7 @@ proc hlo(c: PContext, n: PNode): PNode = # no optimization applied, try subtrees: for i in 0..