Skip to content
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] Cannot materialize a capturing closure #3075

Open
ehein6 opened this issue Jun 18, 2024 · 0 comments
Open

[BUG] Cannot materialize a capturing closure #3075

ehein6 opened this issue Jun 18, 2024 · 0 comments
Labels
bug Something isn't working mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label

Comments

@ehein6
Copy link

ehein6 commented Jun 18, 2024

Bug description

I get errors when trying to use Optional to wrap a capturing closure in an argument or a parameter. See example code below.

  • Optional capturing closure as parameter leads to compiler crash
  • OptionalReg capturing closure leads to error in pass manager
  • Optional capturing closure as argument leads to compile-time error message

The error is marked "TODO" but I didn't see an issue filed to fix this anywhere.

Blocking KERN-542. Tracing.mojo is using a workaround to avoid accepting a capturing closure as a parameter.

Steps to reproduce

Test program:

# Change `OptionalReg` to `Optional` for a compiler crash
struct StructWithFuncParam[func: OptionalReg[fn () capturing -> String] = None]:
    var s: String
 
    fn __init__(inout self):
        self.s = func.value()()
 
    fn print_me(inout self):
        print(self.s)
 
 
struct StructWithFuncArg:
    var s: String
 
    fn __init__(
        inout self, func: OptionalReg[fn () capturing -> String] = None
    ):
        self.s = func.value()()
 
    fn print_me(inout self):
        print(self.s)
 
 
fn main():
    var f = "foo"
 
    @parameter
    fn foo() -> String:
        return f
 
    # error: 'kgen.param.constant' op cannot be used to materialize capturing closures; use `kgen.create_closure` instead
    var x = StructWithFuncParam[foo]()
    x.print_me()
    # CHECK: "foo"
 
    # error: TODO: capturing closures cannot be materialized as runtime values
    # var z = StructWithFuncArg(foo)
    # z.print_me()

System information

mojo 0.0.0 (4bb704c4)
modular 0.0.0 (4bb704c4)
macOS Sonoma 14.5 (M3 Pro)
@ehein6 ehein6 added bug Something isn't working mojo-repo Tag all issues with this label labels Jun 18, 2024
@linear linear bot added the mojo-lang Tag for all issues related to language. label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant