Closed emilwestergren closed 9 years ago
Things messed up when variable appears.
line no.104,105 cause this problem. Here we are generating unnecessary context __vvv_ctx5
.
101 __vvv_ctx5 = lang_Memory__gc_malloc(((lang_types__Class*)__vvv_vvv_closure4_ctx_class())->size);
102 #line 1 "/home/housezet/.ooc_libs/mcs/test/rock/vvv.ooc"
103 (*(__vvv_ctx5)) = (__vvv_vvv_closure4_ctx) {
104 a,
105 b
106 };
107 __vvv_closure6 = (lang_types__Closure) {
108 vvv____vvv_vvv_closure4_thunk,
109 __vvv_ctx5
110 };
It seems to be a bug in VariableDecl -> captureInUpstreamClosures -> intermediateScopeIndex W should also check if var is defined in parent closure.
A related problem(UNRESOLVED):
main: func{
ret := 1
1 times(|| 2 times(|| ret += 1))
ret toString() println()
}
Expect: 3 Got: 1
because rock generate &(&ret) in nested closure...
My example (for the original issue, not #882)
func1 := func {
a := 1
b := 2
func2 := func {
"#{a + b}" println()
}
func2()
}
func1()
Note that if you change func1 :=
to func1:
it doesn't happen. Looks like isDefined
doesn't do the right thing..
Should it be possible to create a closure inside another closure? Because it is in that case not possible to use variables created in the parent closure in the child closure.
Example code:
fails with: