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

Cannot access trait type methods from built-in types. #4463

Closed
Thunkar opened this issue Mar 1, 2024 · 2 comments
Closed

Cannot access trait type methods from built-in types. #4463

Thunkar opened this issue Mar 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Thunkar
Copy link
Contributor

Thunkar commented Mar 1, 2024

Aim

Define a trait for all numeric types:

trait FromField {
    fn from_field(x: Field) -> Self;
}

impl FromField for u8 {
    fn from_field(x: Field) -> Self {
        x as u8
    }
}

fn main(x: Field) {
    u8::from_field(x);
}

Expected Behavior

It should compile

Bug

Compiler errors out:

error: Unexpected u8, expected one of for, }, expression, statement
┌─ /mnt/user-data/grego/repos/noir-experiments/traits_on_u/src/main.nr:12:5

12 │ u8::from_field(x);
│ --

To Reproduce

Try to compile the provided example

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@Thunkar Thunkar added the bug Something isn't working label Mar 1, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Mar 1, 2024
@jfecher
Copy link
Contributor

jfecher commented Mar 4, 2024

This is because u8 is a keyword where the Foo::method syntax currently expects an identifier for Foo.

If anyone reading is encountering this issue, the workaround is to use the static trait method syntax FromField::from_field(x). If type annotations are needed, you can add a let binding:

let y: u8 = FromField::from_field(x);

@asterite
Copy link
Collaborator

asterite commented Nov 4, 2024

It seems this already works since we implemented the TypePath syntax.

@asterite asterite closed this as completed Nov 4, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Nov 4, 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
Projects
Archived in project
Development

No branches or pull requests

3 participants