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

Types cannot be unquoted to call static methods #6078

Closed
Thunkar opened this issue Sep 18, 2024 · 4 comments · Fixed by #6093
Closed

Types cannot be unquoted to call static methods #6078

Thunkar opened this issue Sep 18, 2024 · 4 comments · Fixed by #6093
Assignees
Labels
bug Something isn't working

Comments

@Thunkar
Copy link
Contributor

Thunkar commented Sep 18, 2024

Aim

Given

mod types {
    use std::meta::type_of;

    struct Foo {
        x: Field,
    }

    impl Foo {
        fn static() -> Field {
            3
        }
    }

    comptime fn add_static_call(m: Module) -> Quoted {
        // If I want this to be resolved in AMod, I need to use the type, since the struct name is just Foo and I need types::Foo
        let type_of_foo =quote { Foo }.as_type();
        quote {
            fn call_static() -> Field{
                $type_of_foo::static()
            }
        }
    }
}

use types::add_static_call;

#[add_static_call]
mod AMod {}

Expected Behavior

The code above should compile, so that Foo can be used in AMod even if it was resolved in its own mod.

Bug

The compiler fails to parse the macro token stream

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

Blocker

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@asterite
Copy link
Collaborator

asterite commented Sep 18, 2024

This can be solved with a trick Jake told me today (I needed to do something similar and I didn't want to use a fully qualified name):

mod types {
    struct Foo {
        x: Field,
    }

    impl Foo {
        fn static() -> Field {
            3
        }
    }

    pub comptime fn add_static_call(m: Module) -> Quoted {
        let static_call = Foo::static;
        quote {
            fn call_static() -> Field {
                $static_call()
            }
        }
    }
}

use types::add_static_call;

#[add_static_call]
mod AMod {}

@asterite
Copy link
Collaborator

I also tried doing this but it didn't work:

let foo = std::meta::type_of(Foo { x: 1 });
quote {
  fn call_static() -> Field {
    $foo::static()
  }
}

The reason is that $foo in that parsing position must be a Path, not a Type.

Unrelated, but I'm thinking it would be nice to be able to somehow convert a type to a Type. We can do that with std::meta::type_of, but we have to pass an actual instance.

But, we can also do this:

struct Foo {
    x: Field
}

comptime fn get_type<T>() -> Type {
    let exp: T = std::mem::zeroed();
    std::meta::type_of(exp)
}

fn main() {
    comptime
    {
        let foo = get_type::<Foo>();
        println(foo);
    }
}

@jfecher jfecher self-assigned this Sep 18, 2024
@jfecher
Copy link
Contributor

jfecher commented Sep 18, 2024

This can be solved with a trick Jake told me today (I needed to do something similar and I didn't want to use a fully qualified name):

This is true, I'm assuming @Thunkar's full example could be using a non-statically known type from user code somewhere so we'd still need this feature to do:

comptime fn add_static_call(m: Module, t: Type) -> Quoted {
    quote {
        fn call_static() -> Field {
            $t::static()
        }
    }
}

So I'm working on this issue since we should also support this usecase

@asterite
Copy link
Collaborator

I don't know how you are going to implement this, but some weeks ago I wanted to implement being able to do Field::method(), i32::method(), etc. I just pushed the branch I had. I changed the parsing a bit to allow type :: name ( .. ) to be parsed, but only for primitive types. I guess allowing that for interned types would be similar, and it might enable being able to call primitive "static" methods (though in my PR it didn't work because primitive methods are assumed to have a self argument, I think)

github-merge-queue bot pushed a commit that referenced this issue Sep 18, 2024
# Description

## Problem\*

Resolves #6078

## Summary\*

Adds the ability to do `Type::method` where `Type` is a primitive type
or interned type. Other named types should already be supported by
existing paths.

## Additional Context

I also made a small update to `lookup_method` to support looking up
methods without a `self` parameter to allow for us to possibly have
something like `Field::static_method()` in the future, although we have
no functions like this in the stdlib currently.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Sep 18, 2024
Thunkar added a commit to AztecProtocol/aztec-packages that referenced this issue Sep 24, 2024
Requires noir sync and noir-lang/noir#6078 to
be merged so `compute_note_hash_and_optionally_a_nullifier` can be
autogenerated.

Also, fixed a lot of explicit numeric generics, used arithmetics on them
(yay!!) and introduced a macro for partial notes

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: benesjan <janbenes1234@gmail.com>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
Co-authored-by: Ary Borenszweig <asterite@gmail.com>
github-merge-queue bot pushed a commit that referenced this issue Sep 24, 2024
# Description

Only blocked by #6078


![image](https://github.com/user-attachments/assets/b117f8da-f32a-45ec-b3f8-a6028e7e1d40)

Hope I didn't miss anything

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
AztecBot pushed a commit to AztecProtocol/aztec-nr that referenced this issue Sep 25, 2024
Requires noir sync and noir-lang/noir#6078 to
be merged so `compute_note_hash_and_optionally_a_nullifier` can be
autogenerated.

Also, fixed a lot of explicit numeric generics, used arithmetics on them
(yay!!) and introduced a macro for partial notes

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: benesjan <janbenes1234@gmail.com>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
Co-authored-by: Ary Borenszweig <asterite@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants