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

Interaction of var, nameof, and Color Color #832

Open
KalleOlaviNiemitalo opened this issue Jul 2, 2023 · 3 comments
Open

Interaction of var, nameof, and Color Color #832

KalleOlaviNiemitalo opened this issue Jul 2, 2023 · 3 comments
Labels
type: bug The Standard does not describe the language as intended or implemented
Milestone

Comments

@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Jul 2, 2023

C# 7 draft §13.6.2 (Local variable declarations) says this about "implicitly typed local variable declaration":

The initializer expression cannot refer to the declared variable itself

As specified and implemented, this disallows also var x = nameof(x); even though the type of the expression would be unambiguous in this case. That was discussed in dotnet/roslyn#766 and C# Design Meeting Notes for Mar 4, 2015.

However, the wording "refer to the declared variable" seems a bit ambiguous in the "Color Color" case (C# 7 §12.8.7.2, Identical simple names and type names).

In the following example, does nameof(Color.Beige) refer to the variable, the type, or both?

class C {
    void M() {
        var Color = nameof(Color.Beige) == "nope" ? (Color)1 : (Color)2;
    }

    enum Color {
        Beige,
        Mauve,
        Fuchsia,
    }
}

(Continued from #831 (comment))

@KalleOlaviNiemitalo
Copy link
Contributor Author

A similar example without nameof:

class C {
    void M() {
        var Color = Color.Beige;
    }

    enum Color {
        Beige,
        Mauve,
        Fuchsia,
    }
}

Here, to find the type of the initialiser expression Color.Beige, the compiler needs to check whether the Color Color rule applies, and for that, it needs to know the type of the Color variable, which in turn depends on the type of the expression. So the standard should forbid this. I'm not sure whether it currently does.

@jskeet jskeet added the type: bug The Standard does not describe the language as intended or implemented label Jul 3, 2023
@jskeet
Copy link
Contributor

jskeet commented Jul 3, 2023

Thanks for this - we're currently focusing on getting C# 7 "done" and probably won't get to this for a while, but we'll look carefully when we get a chance.

@jskeet jskeet added this to the C# 7.x milestone Aug 7, 2023
@jskeet
Copy link
Contributor

jskeet commented Aug 7, 2023

Given that this doesn't look like it's directly related to a C# 7 feature (so was already broken in C# 7) I'm going to punt this to C# 8. As ever, feel free to disagree if I've missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug The Standard does not describe the language as intended or implemented
Projects
None yet
Development

No branches or pull requests

2 participants