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

Traits should allow methods with generic return types #3474

Closed
Tracked by #2568
grasshopper47 opened this issue Nov 10, 2023 · 2 comments · Fixed by #4000
Closed
Tracked by #2568

Traits should allow methods with generic return types #3474

grasshopper47 opened this issue Nov 10, 2023 · 2 comments · Fixed by #4000
Assignees
Labels
bug Something isn't working

Comments

@grasshopper47
Copy link
Contributor

grasshopper47 commented Nov 10, 2023

Aim

Currently it is possible to use generics within a trait:

trait Extensions
{
    fn generic<N>(self : [u8], right : [u8; N]) -> bool;   // OK
}

Expected Behavior

A trait allows methods with a generic return type:

trait Extensions
{
    fn generic<N>(self) -> [u8; N];                    
}

Bug

When creating the implementation the compiler throws a confusing error:

impl Extensions for [u8]
{
    pub fn generic<N>(self) -> [u8; N] // error: Expected type [u8; N], found type [u8; N]
    {
        // .. //
    }
}

Linked issue: #3502

To Reproduce

Compile the following:

trait Extensions
{
    fn generic<N>(self) -> [u8; N];                    
}

impl Extensions for [u8]
{
    pub fn generic<N>(self) -> [u8; N] // error: Expected type [u8; N], found type [u8; N]
    {
        // .. //
    }
}

Installation Method

Binary

Nargo Version

0.19.2

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@grasshopper47 grasshopper47 added the bug Something isn't working label Nov 10, 2023
@grasshopper47 grasshopper47 changed the title Traits should allow generic return types to methods Traits should allow methods with generic return types Nov 10, 2023
@TomAFrench
Copy link
Member

Closing as duplicate of #3479

@TomAFrench TomAFrench closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2024
@TomAFrench TomAFrench reopened this Jan 2, 2024
@TomAFrench
Copy link
Member

Reopening as they're actually slightly different.

@jfecher jfecher self-assigned this Jan 2, 2024
@Savio-Sou Savio-Sou mentioned this issue Jan 5, 2024
46 tasks
github-merge-queue bot pushed a commit that referenced this issue Jan 16, 2024
# Description

## Problem\*

Resolves #3471
Resolves #3474

## Summary\*

Implements support for generics on the trait directly. E.g. `trait
Into<T> { ... }`

## Additional Context

The old `trait_generics` test has been renamed to `trait_impl_generics`
- I think this is more accurate. There is a new test in `trait_generics`
now which tests the generic traits added by this PR.

I've discovered two new bugs in writing this PR, which are commented in
the `trait_generics` test. I'll make issues for them now.

## Documentation\*

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

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
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

Successfully merging a pull request may close this issue.

3 participants