mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-25 08:43:58 +00:00
The first commit reverts the revert of #23787. The second fixes lambdalifting in convolutedly nested closures/closureiters. This is considered to be the reason of #24094, though I can't tell for sure, as I was not able to reproduce #24094 for complicated but irrelevant reasons. Therefore I ask @jmgomez, @metagn or anyone who could reproduce it to try it again with this PR. I would suggest this PR to not be squashed if possible, as the history is already messy enough. Some theory behind the lambdalifting fix: - A closureiter that captures anything outside its body will always have `:up` in its env. This property is now used as a trigger to lift any proc that captures such a closureiter. - Instantiating a closureiter involves filling out its `:up`, which was previously done incorrectly. The fixed algorithm is to use "current" env if it is the owner of the iter declaration, or traverse through `:up`s of env param until the common ancestor is found. --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
58 lines
1.2 KiB
Nim
58 lines
1.2 KiB
Nim
discard """
|
|
cmd: '''nim c --newruntime $file'''
|
|
errormsg: "'=copy' is not available for type <owned Button>; requires a copy because it's not the last read of ':envAlt.b0'; routine: main"
|
|
line: 48
|
|
"""
|
|
|
|
import system / ansi_c
|
|
|
|
type
|
|
Widget* = ref object of RootObj
|
|
drawImpl: owned(proc (self: Widget))
|
|
|
|
Button* = ref object of Widget
|
|
caption: string
|
|
onclick: owned(proc())
|
|
|
|
Window* = ref object of Widget
|
|
elements: seq[owned Widget]
|
|
|
|
|
|
proc newButton(caption: string; onclick: owned(proc())): owned Button =
|
|
proc draw(self: Widget) =
|
|
let b = Button(self)
|
|
echo b.caption
|
|
|
|
result = Button(drawImpl: draw, caption: caption, onclick: onclick)
|
|
|
|
proc newWindow(): owned Window =
|
|
proc draw(self: Widget) =
|
|
let w = Window(self)
|
|
for e in w.elements:
|
|
if not e.drawImpl.isNil: e.drawImpl(e)
|
|
|
|
result = Window(drawImpl: draw, elements: @[])
|
|
|
|
proc draw(w: Widget) =
|
|
if not w.drawImpl.isNil: w.drawImpl(w)
|
|
|
|
proc add*(w: Window; elem: owned Widget) =
|
|
w.elements.add elem
|
|
|
|
proc main =
|
|
var w = newWindow()
|
|
|
|
var b = newButton("button", nil)
|
|
b.onclick = proc () =
|
|
b.caption = "clicked!"
|
|
w.add b
|
|
|
|
w.draw()
|
|
# simulate button click:
|
|
b.onclick()
|
|
|
|
w.draw()
|
|
|
|
main()
|
|
|