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

var variables cannot be used in nameof in the same line. #45146

Open
333fred opened this issue Jun 13, 2020 · 4 comments
Open

var variables cannot be used in nameof in the same line. #45146

333fred opened this issue Jun 13, 2020 · 4 comments
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@333fred
Copy link
Member

333fred commented Jun 13, 2020

class C
{
    static string M(string s)
    {
        var a = M(nameof(a)); // Error: a is not definitely assigned
        string b = M(nameof(b)); // Works fine.
    }
}
@Youssef1313
Copy link
Member

        var a = M(nameof(a)); // Error: a is not definitely assigned
        string b = M(nameof(b)); // Works fine.

The second behavior (no errors) is the correct one right?

@333fred
Copy link
Member Author

333fred commented Jun 18, 2020

Yes, the second behavior is the correct behavior.

@gafter
Copy link
Member

gafter commented Jun 22, 2020

The compiler is behaving as specified. From https://github.com/dotnet/csharplang/blob/master/spec/statements.md#local-variable-declarations

In the context of a local variable declaration, the identifier var acts as a contextual keyword (Keywords).When the local_variable_type is specified as var and no type named var is in scope, the declaration is an implicitly typed local variable declaration, whose type is inferred from the type of the associated initializer expression. Implicitly typed local variable declarations are subject to the following restrictions:

  • The local_variable_declaration cannot include multiple local_variable_declarators.
  • The local_variable_declarator must include a local_variable_initializer.
  • The local_variable_initializer must be an expression.
  • The initializer expression must have a compile-time type.
  • The initializer expression cannot refer to the declared variable itself

[my emphasis]

While it is reasonable to expect that your example might work, it is currently required to be an error.

The specific diagnostic produced for this should be improved.

@gafter gafter added Resolution-By Design The behavior reported in the issue matches the current design and removed Bug labels Jun 22, 2020
@jaredpar jaredpar added the Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. label Jun 22, 2020
@jaredpar jaredpar added this to the Backlog milestone Jun 22, 2020
@333fred
Copy link
Member Author

333fred commented Jun 22, 2020

Hmph. Alright, I might end up proposing something over at csharplang for this.

@gafter gafter added Bug and removed Resolution-By Design The behavior reported in the issue matches the current design labels Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
None yet
Development

No branches or pull requests

4 participants