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

Unexpected "User-defined operator must be declared static and public" for an operator in an interface #52202

Closed
AlekseyTs opened this issue Mar 28, 2021 · 1 comment · Fixed by #53119
Assignees
Milestone

Comments

@AlekseyTs
Copy link
Contributor

interface I2
{
    static I2 operator+ (I2 x)
    {throw null;}
}

Observed:

error CS0558: User-defined operator 'I2.operator +(I2)' must be declared static and public

Expected: No errors. members in interfaces are implicitly public.

There is also a question whether we should tie the fix to new language version or just fix it for C# 8.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 28, 2021
@jaredpar jaredpar removed the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 29, 2021
@jaredpar jaredpar added this to the 16.10 milestone Mar 29, 2021
@jaredpar
Copy link
Member

I think we should just fix it for C# 8.0.

AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue Mar 29, 2021
AlekseyTs added a commit that referenced this issue Apr 2, 2021
…events in interfaces. (#52228)

Spec: https://github.com/dotnet/csharplang/blob/9a0dddbf0643993db0da8f48436f2aac60bc20b1/proposals/statics-in-interfaces.md

Related to #52202.

Relevant quotes from the spec:

#### Abstract virtual members
Static interface members other than fields are allowed to also have the `abstract` modifier. Abstract static members are not allowed to have a body (or in the case of properties, the accessors are not allowed to have a body). 

``` c#
interface I<T> where T : I<T>
{
    static abstract void M();
    static abstract T P { get; set; }
    static abstract event Action E;
    static abstract T operator +(T l, T r);
}
```

#### Explicitly non-virtual static members
For symmetry with non-virtual instance members, static members should be allowed an optional `sealed` modifier, even though they are non-virtual by default:

``` c#
interface I0
{
    static sealed void M() => Console.WriteLine("Default behavior");
    
    static sealed int f = 0;
    
    static sealed int P1 { get; set; }
    static sealed int P2 { get => f; set => f = value; }
    
    static sealed event Action E1;
    static sealed event Action E2 { add => E1 += value; remove => E1 -= value; }
    
    static sealed I0 operator +(I0 l, I0 r) => l;
}
```
AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants