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

Possible compiler issue #2672

Open
Moosems opened this issue May 16, 2024 · 1 comment
Open

Possible compiler issue #2672

Moosems opened this issue May 16, 2024 · 1 comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@Moosems
Copy link

Moosems commented May 16, 2024

Bug description

I was trying to create a struct that can take a function as a parameter and after discussion with devs on the Discord, I believe there may be a compiler bug.

Steps to reproduce

Naive code (causes error in playground):

@always_inline("nodebug")
fn string_to_string(arg: String) -> String:
    return arg
 
struct ParseableArg[
    type: AnyType,
    type_converter: fn(arg: String) -> type = string_to_string,
    action: String = "",
    description: String = "",
    required: Bool = False,
]:
    ...
 
 
/source/prog.mojo:7:47: error: cannot implicitly convert 'fn(arg: String) -> String' value to 'fn(arg: String) -> type' in default parameter
    type_converter: fn(arg: String) -> type = string_to_string,
                                              ^~~~~~~~~~~~~~~~
/source/prog.mojo:15:5: error: use of unknown declaration 'arg1', 'fn' declarations require explicit variable declarations
    arg1 = ParseableArg[
    ^~~~
/source/prog.mojo:15:24: error: dynamic type values not permitted yet
    arg1 = ParseableArg[
           ~~~~~~~~~~~~^
mojo: error: failed to parse the provided Mojo source module

with trait:

trait FromString:
    fn __init__(inout self, s: String): ...

@always_inline("nodebug")
fn string_to_string[T: FromString](arg: String) -> T:
    return arg^ # returning T(arg) doesn’t work either


struct ParseableArg[
    type: FromString,
    type_converter: fn[type](arg: String) -> type = string_to_string,

With rebind:

type_converter: fn[rebind[type](FromString)](arg: String) -> rebind[type](FromString) = string_to_string,
 
/source/prog.mojo:11:93: error: cannot implicitly convert 'fn[FromString](arg: String) -> $0' value to 'fn[rebind[AnyRegType,AnyRegType](FromString)](arg: String) -> rebind[AnyRegType,AnyRegType](FromString)' in default parameter
    type_converter: fn[rebind[type](FromString)](arg: String) -> rebind[type](FromString) = string_to_string,
                                                                                            ^~~~~~~~~~~~~~~~
mojo: error: failed to parse the provided Mojo source module

System information

Mojo Playground
@Moosems Moosems added bug Something isn't working mojo-repo Tag all issues with this label labels May 16, 2024
@Moosems
Copy link
Author

Moosems commented May 16, 2024

Discord conversation starts here

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-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant