-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Closure state is stack allocated #217
Comments
This is a known issue. Capture state is stack allocated. It should be heap allocated and it's something we plan to fix. |
This issue is observed here as well #111 |
With the magic %%python the output of this closure is ok: x = 7, y = 4, z = 2, return: 13 %%python
def outer_func(x):
y = 4
def inner_func(z):
print(f'x = {x}, y = {y}, z = {z}')
return x + y + z
return inner_func
closure = outer_func(7)
print(closure(2)) But without: def outer_func(x):
y = 4
def inner_func(z):
print("x = ", x, " y = ", y, " z = ", z)
return x + y + z
return inner_func
closure = outer_func(7)
print(closure(2)) _error: Expression [5]:10:14: no matching function in call to 'print': /.modular/Kernels/mojo/Stdlib/IO.mojo:244:1: candidate not viable: callee expects 0 arguments, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:249:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:258:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:268:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:278:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:287:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:296:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:305:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:314:1: candidate not viable: callee expects 2 input parameters but 0 were provided /.modular/Kernels/mojo/Stdlib/IO.mojo:339:1: candidate not viable: callee expects 1 input parameter but 0 were provided /.modular/Kernels/mojo/Stdlib/IO.mojo:352:1: candidate not viable: callee expects 1 input parameter but 0 were provided /.modular/Kernels/mojo/Stdlib/IO.mojo:378:1: candidate not viable: callee expects 1 argument, but 6 were specified /.modular/Kernels/mojo/Stdlib/IO.mojo:397:1: candidate not viable: argument #1 cannot be converted from 'object' to '_Printable' error: Expression [5]:12:12: 'fn(object) raises capturing -> object' value cannot be converted to 'object' in return value |
from time import time |
The following variant segfaults in the beta:
|
@Mogball Stdlib or lang for labels? |
lang. Closures are a language issue |
The following examples might be relevant. fn outer(number: Int = 1) -> fn () capturing -> None:
fn inner():
print("inner=", number) #prints inner=94467979384400 (varies)
print("outer=", number) #prints outer=1
return inner fn outer(number: Int = 1) -> fn () capturing -> None:
print("outer=", number) #prints inner=1
fn inner():
print("inner=", number) #prints inner=1
print("outer=", number) #prints outer=1
return inner fn outer(number: Int = 1) -> fn () capturing -> None:
print("outer=", number) #SEGFAULT
fn inner():
print("inner=", number) #SEGFAULT
return inner |
@emillma Reproducing this behavior in the playground requires using |
I started a discussion (#1690) on why stack allocated closures may not necessarily be a "bug" to "fix". Tell me what you guys think. |
Bug Description
crashes with
Expected behavior would be for it to not crash.
Note that making
func
a parameter instead does workSteps to Reproduce
Run the provided code.
Context
The text was updated successfully, but these errors were encountered: