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

Support synthesizing generic adaptor methods for interface requirements. #5035

Closed
tunabrain opened this issue Sep 6, 2024 · 2 comments · Fixed by #5111
Closed

Support synthesizing generic adaptor methods for interface requirements. #5035

tunabrain opened this issue Sep 6, 2024 · 2 comments · Fixed by #5111
Assignees
Labels
goal:client support Feature or fix needed for a current slang user.

Comments

@tunabrain
Copy link

There seems to be a corner case issue with generic return values in interfaces.

Let's pretend I had an interface like this:

interface IStack<let D : int>
{
    IStack<D - 1> pop();
}
struct StackImpl<let D : int> : IStack<D>
{
    StackImpl<D - 1> pop() { /* ... */ }
}

This works. However, this does not:

interface IStack<let D : int>
{
    __generic<let N : int>
    IStack<D - N> popN();
}
struct StackImpl<let D : int> : IStack<D>
{
    // member 'popN' does not match interface requirement.
    __generic<let N : int>
    StackImpl<D - N> popN() { /* *** */ }
}

Is it intended to begin with that a pop return type of StackImpl<D-1> satisfies the interface requirement of a IStack<D-1> return type?
If so, is it possible to support the IStack<D-N> case as well?

@csyonghe
Copy link
Collaborator

csyonghe commented Sep 7, 2024

Seems like a bug that needs to be fixed.

@csyonghe csyonghe added the goal:client support Feature or fix needed for a current slang user. label Sep 7, 2024
@csyonghe csyonghe self-assigned this Sep 7, 2024
@csyonghe csyonghe added this to the Q3 2024 (Summer) milestone Sep 7, 2024
@csyonghe
Copy link
Collaborator

csyonghe commented Sep 9, 2024

The problem here is that the implementation method

__generic<let N : int>
    StackImpl<D - N> popN() { /* *** */ }

does not match exactly the requirement:

__generic<let N : int>
    IStack<D - N> popN();

Because StackImpl<D-N> is not the same type as IStack<D-N>.
Normally if this is not a generic method, slang can synthesize a wrapper to satisfy the requirement. However since popN is a generic method, we currently don't have the logic to synthesize the wrapper and you will need to manually provide one, or simply modify the implementation method to return IStack<D-N>.

Doing that revealed another issue that the compiler cannot properly specailize functiosn that returns an interface type to functions that return a concrete type even if the return type is known at compile time.
PR #5044 addresses this issue and can produce correctly specialized code.

@csyonghe csyonghe changed the title Interface requirements with generic return values are not matched Support synthesizing interface requirement adaptors for generic methods. Sep 10, 2024
@csyonghe csyonghe changed the title Support synthesizing interface requirement adaptors for generic methods. Support synthesizing generic adaptor methods for interface requirements. Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:client support Feature or fix needed for a current slang user.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants