Skip to content

Primary-constructor conversion emits this in a field initializer (CS0027) #3984

Description

@siegfriedpammer

Sibling of #3979: the same transform produces an uncompilable field initializer when the constructor reads the field through an explicit this..

Repro

public struct S2
{
    public int value;
    public int b;

    // The parameter shadows the field, so the qualifier is mandatory.
    public S2(int value)
    {
        this.value = value;
        b = this.value + 1;
    }
}

Decompiled with master (d60a8662a):

public struct S2(int value)
{
    public int value = value;

    public int b = this.value + 1;
}

Recompiling that output:

error CS0027: Keyword 'this' is not available in the current context

Notes

Possible direction: also treat an ILVariableResolveResult whose Variable.IsThis() — plus the ByReferenceResolveResult that wraps it for structs — as an instance-member reference. The existing generated-backing-field (<x>P) carve-out would then need to skip the target subtree, since under AlwaysQualifyMemberReferences that access is spelled this.<x>P.


Filed by an AI agent (Claude) on behalf of @siegfriedpammer. Repro built and both outputs recompiled to confirm the diagnostics.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions