From 8cc268876a214d164899b0d0a0ccdcb98a861b1b Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 3 Nov 2017 18:05:14 +0100 Subject: [PATCH] bugfix: detect captures in non-closure inner procs --- compiler/lambdalifting.nim | 5 ++++- tests/closure/tinvalidclosure3.nim | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/closure/tinvalidclosure3.nim diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index e64e0a898..f8d107c84 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -764,7 +764,10 @@ proc semCaptureSym*(s, owner: PSym) = var o = owner.skipGenericOwner while o.kind != skModule and o != nil: if s.owner == o: - owner.typ.callConv = ccClosure + if owner.typ.callConv in {ccClosure, ccDefault} or owner.kind == skIterator: + owner.typ.callConv = ccClosure + else: + discard "do not produce an error here, but later" #echo "computing .closure for ", owner.name.s, " ", owner.info, " because of ", s.name.s o = o.skipGenericOwner # since the analysis is not entirely correct, we don't set 'tfCapturesEnv' diff --git a/tests/closure/tinvalidclosure3.nim b/tests/closure/tinvalidclosure3.nim new file mode 100644 index 000000000..31c4976f8 --- /dev/null +++ b/tests/closure/tinvalidclosure3.nim @@ -0,0 +1,12 @@ +discard """ + line: 9 + errormsg: "illegal capture 'x'" +""" + +proc outer(arg: string) = + var x = 0 + proc inner {.inline.} = + echo "inner", x + inner() + +outer("abc") \ No newline at end of file