dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

Errors for scoped differences in override indexers are missing in some cases #73384

Open RikkiGibson opened 1 month ago

RikkiGibson commented 1 month ago
public ref struct RS { }

public class Base
{
    public virtual RS this[scoped RS rs] { get => default; }
    public virtual RS this[scoped RS rs, int _] { get => default; set { }}
}

public class C : Base
{
    public override RS this[RS rs] { get => default; } // error CS8987: The 'scoped' modifier of parameter 'rs' doesn't match overridden or implemented member.
    public override RS this[RS rs, int _] { get => default; set { } } // no error
}

Expected: Both commented lines have errors Actual: Only the first commented line has an error. It seems like adding a set accessor to a similar indexer declaration removes the error.

RikkiGibson commented 1 month ago

I think the issue is here. It is assumed that if setter is present, then only setter needs to be checked, but for the scoped checks to actually run, there needs to be a ref or ref struct output, as well as input, which is only found on the getter.

https://github.com/dotnet/roslyn/blob/f8d563cf15e7da887d141d35abdd947aa4d4eb0f/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs#L1097-L1122