dotnet / csharpstandard

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

Base access in expression body #1109

Closed KalleOlaviNiemitalo closed 5 months ago

KalleOlaviNiemitalo commented 6 months ago

Describe the bug

In the C# 7 standard, §12.8.14 (Base access) says:

A base_access is permitted only in the block of an instance constructor, an instance method, an instance accessor (§12.2.1), or a finalizer.

This should allow base_access also in a method_body, ref_method_body, property_body, ref_property_body, indexer_body, ref_indexer_body, constructor_body, or finalizer_body that is not a block.

Example

This should be allowed:

class A {
    private int i;

    public A() {}

    public int M() => 0;
    public ref int RM() => ref this.i;

    public int P => 0;
    public ref int RP => ref this.i;
    public int PA {
        get => 0;
        set {}
    }
    public ref int RPA {
        get => ref this.i;
    }

    public int this[short index] => 0;
    public ref int this[char index] => ref this.i;

    public event System.Action EA {
        add {}
        remove {}
    }
}

class B : A {
    public B() => base.M();

    public new int M() => base.M();
    public new ref int RM() => ref base.RM();

    public new int P => base.P;
    public ref int RP => ref base.RP;
    public new int PA {
        get => base.PA;
        set => base.PA = value;
    }
    public new ref int RPA {
        get => ref base.RPA;
    }

    public new int this[short index] => base[index];
    public new ref int this[char index] => ref base[index];

    public new event System.Action EA {
        add => base.EA += value;
        remove => base.EA -= value;
    }

    ~B() => base.M();
}

Expected behavior

Specify that the preceding is allowed.

Additional context

Exclude anonymous_function_body, local_function_body, and ref_local_function_body. They can contain a base_access only when they occur within some other body.

Exclude static_constructor_body and operator_body. They are not used in instance members.

Nigel-Ecma commented 6 months ago

@KalleOlaviNiemitalo – thanks. Added PR #1110, Github wouldn't let me add you directly as a reviewer but don't let that stop you!

I did wonder about shortening the text to reference "instance members" but went with the one word fix.