dotnet / csharpstandard

Working space for ECMA-TC49-TG2, the C# standard committee.
Creative Commons Attribution 4.0 International
718 stars 86 forks source link

Error CS0122 when attempting to implement a nested `private protected` interface in descendant classes #272

Open williamb1024 opened 4 years ago

williamb1024 commented 4 years ago

Version Used: Visual Studio 16.5.4

public class X: X.XI, X.XII
{
        private interface XI
        {
        }

        private protected interface XII
        {
        }
}

public class XX: X, X.XII
{
        public void Test()
        {
            XII z = this;
        }
 }

Expected Behavior:

The class type XX would be able to implement the X.XII interface.

Actual Behavior:

The error "CS0122: 'X.XII' is inaccessible due to its protection level" is generated.

jcouv commented 4 years ago

Tagging @gafter who worked on private protected

gafter commented 4 years ago

The same is true for protected:

public class X : X.XII
{
    protected interface XII
    {
    }
}

public class XX : X, X.XII // X.XII is inaccessible due to its protection level
{
    public void Test()
    {
        XII z = this;
    }
}

This is due to the fact that the compiler temporarily assumes, when binding the base clause, that the base class is object. The specification says that "While determining the meaning of the direct base class specification A of a class B, the direct base class of B is temporarily assumed to be object." But the base clause does not have a distinguished direct base class specification in the syntax, so the specification does not make sense as written. It is taken in the compiler to apply to the whole base clause.

This is a known open issue in the ECMA specification, which the committee has punted from addressing in the C# 5 specification.

BillWagner commented 3 years ago

Moving to csharpstandard for the ECMA committee.