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

Add scoping rules for receiver parameters #9185

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MadsTorgersen
Copy link
Contributor

This adds scoping rules for receiver parameters. The upshot is that they should behave like primary constructor parameters: They are in scope within contained members, but it is illegal to reference them from a static context.

@MadsTorgersen MadsTorgersen requested a review from a team as a code owner February 28, 2025 01:26
@jcouv jcouv self-assigned this Feb 28, 2025
@jcouv jcouv requested a review from AlekseyTs March 7, 2025 18:25
}
```

In the example, `*1*` and `*2*` denote local variable declaration spaces, where `*2*` is nested within `*1*` and thus prohibited from introducing the same local variable names as `*1*`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either I am misinterpreting what this statement is saying, or it looks like it contradicts the current language rules, which allow shadowing of both parameters and locals within nested methods. For example, the following code is legal:

class C2(int p)
{
    
    void M1(long p)
    {}

    static void M2(long p)
    {}

    void M3()
    {
        long p = 0;
    }

    static void M4()
    {
        long p = 0;
    }
    
    void M5(int p1)
    {
        string local1 (string p1) => p1;
        local1("");

        int l1 = 0;
        
        string local2 ()
        {
            string p1 = "";
            string l1 = "";
            return p1 + l1;
        }
        
        local2();
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think shadowing of locals and parameters within local functions and lambdas was allowed in Roslyn time frame. It is quite possible this relaxation didn't make it into the spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the relevant proposal - https://github.com/dotnet/csharplang/blob/main/proposals/csharp-8.0/shadowing-in-nested-functions.md. With primary constructor parameters we followed the same rules, i.e. they can be shadowed by member names and by parameters and locals declared by members.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants