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] Type checker is failing to compare two function types #3310

Open
sibarras opened this issue Jul 25, 2024 · 1 comment
Open

[BUG] Type checker is failing to compare two function types #3310

sibarras opened this issue Jul 25, 2024 · 1 comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@sibarras
Copy link

Bug description

A struct with a parametrized type, who is also the return type of a function parameter (or argument) is not making the compiler happy. No matter if the function signature is exactly what is needed.

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.
# Needs a function as a parameter
struct Foo[T: AnyType, f1: fn () -> T]:
    alias one_generator = f1

# Needs a function as an argument
struct Bar[T: AnyType]:
    var one_generator: fn () -> T

    fn __init__(inout self, f1: fn () -> T):
        self.one_generator = f1

# function to be passed
fn returns_one() -> Int:
    return 1


fn main() -> None:
    var foo = Foo[Int, returns_one]
    # error: 'Foo' parameter #1 has 'fn() -> Int' type, but value has type 'fn() -> Int'
    var bar = Bar[Int](returns_one)
    # error: invalid initialization: argument #1 cannot be converted from 'fn() -> Int' to 'fn() -> Int'

The problem goes away if I specify the return type to be a an Int instead of the type T. But this is not generic.

struct Foo[T: AnyType, f1: fn () -> Int]:
    alias one_generator = f1


struct Bar[T: AnyType]:
    var one_generator: fn () -> Int

    fn __init__(inout self, f1: fn () -> Int):
        self.one_generator = f1


fn returns_one() -> Int:
    return 1


fn main() -> None:
    alias foo = Foo[Int, returns_one]
    _ = Bar[Int](returns_one)

But this is not generic :(

System information

- What OS did you do install Mojo on ?
╭──────────────────┬─────────────────────────────────────╮
│ kernel-name      │ Linux                               │
│ kernel-release   │ 5.15.153.1-microsoft-standard-WSL2  │
│ kernel-version   │ #1 SMP Fri Mar 29 23:14:13 UTC 2024 │
│ machine          │ x86_64                              │
│ operating-system │ GNU/Linux                           │
╰──────────────────┴─────────────────────────────────────╯
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 2024.7.2505 (defedaa4)
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.8.0 (39a426b5)
@sibarras sibarras added bug Something isn't working mojo-repo Tag all issues with this label labels Jul 25, 2024
@soraros
Copy link
Contributor

soraros commented Jul 25, 2024

Duplicate of #1631.

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

2 participants