Fix capture for object types (#13315)

* Fix capture for object|tuple|... types

* Add test case
This commit is contained in:
Clyybber 2020-02-02 13:52:21 +01:00 • committed by GitHub
commit d43e5bef39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View file

@ -184,7 +184,7 @@ macro capture*(locals: openArray[typed], body: untyped): untyped {.since: (1, 1)
## echo r[0] & ", or " & r[1] # output: to be, or not to be ## echo r[0] & ", or " & r[1] # output: to be, or not to be
var params = @[newIdentNode("auto")] var params = @[newIdentNode("auto")]
for arg in locals: for arg in locals:
params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeImpl arg)) params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeInst arg))
result = newNimNode(nnkCall) result = newNimNode(nnkCall)
result.add(newProc(newEmptyNode(), params, body, nnkProcDef)) result.add(newProc(newEmptyNode(), params, body, nnkProcDef))
for arg in locals: result.add(arg) for arg in locals: result.add(arg)

View file

@ -1,6 +1,9 @@
discard """ discard """
output: ''' output: '''
to be, or not to be''' to be, or not to be
(v: 1)
(v: 1)
'''
joinable: false joinable: false
""" """
@ -10,3 +13,14 @@ let m = @[proc (s: string): string = "to " & s, proc (s: string): string = "not
var l = m.mapIt(capture([it], proc (s: string): string = it(s))) var l = m.mapIt(capture([it], proc (s: string): string = it(s)))
let r = l.mapIt(it("be")) let r = l.mapIt(it("be"))
echo r[0] & ", or " & r[1] echo r[0] & ", or " & r[1]
type O = object
v: int
var o = O(v: 1)
var execute: proc()
capture [o]:
execute = proc() =
echo o
execute()
o.v = -1
execute()