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

nameof requires a non-static member in a sub-expression #20600

Closed
alrz opened this issue Jul 1, 2017 · 6 comments
Closed

nameof requires a non-static member in a sub-expression #20600

alrz opened this issue Jul 1, 2017 · 6 comments
Assignees
Labels
Area-Compilers Bug Language-C# Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@alrz
Copy link
Contributor

alrz commented Jul 1, 2017

Version Used: 5fbdd5c

Steps to Reproduce:

class C
{
    public string Instance;
    public static string Static;
      
    void M()
    {
         _ = nameof(C.Instance);             // OK         
         _ = nameof(C.Instance.Length);      // ERROR
         _ = nameof(C.Static);               // OK  
         _ = nameof(C.Static.Length);        // OK
    }
}

class C<T>
{
    public T Instance;
    public static T Static;
     
    void M()
    {
         _ = nameof(C<string>.Instance);	// OK
	 _ = nameof(C<string>.Instance.Length); // ERROR
         _ = nameof(C<string>.Static);		// OK
         _ = nameof(C<string>.Static.Length);   // OK
    }
}

Expected Behavior: No error.

Actual Behavior:

CS0120: An object reference is required for the non-static field, method, or property 'C.Instance'

@alrz
Copy link
Contributor Author

alrz commented Jul 1, 2017

I suspect this is not per spec.

@sharwell
Copy link
Member

sharwell commented Jul 2, 2017

I'm marking as a bug even though I'm not sure whether or not this is the correct behavior (it's either by design or a bug, so I figured this was the right label).

@gafter
Copy link
Member

gafter commented Feb 9, 2018

According to https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#nameof-expressions this should be legal. It is therefore a compiler bug.

@gafter gafter added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Feb 9, 2018
@gafter gafter self-assigned this Feb 9, 2018
@gafter gafter added this to the 15.7 milestone Feb 9, 2018
@gafter gafter added 3 - Working and removed help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Feb 10, 2018
gafter added a commit to gafter/roslyn that referenced this issue Feb 10, 2018
@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 3 - Working labels Feb 10, 2018
@gafter
Copy link
Member

gafter commented Feb 12, 2018

According to https://github.com/dotnet/vblang/blob/master/meetings/2014/LDM-2014-10-23.md (Yes, C# design notes in the VB repo because that's where the meeting was held)... the compiler was correct until @MadsTorgersen wrote a spec that didn't exactly agree.

@gafter
Copy link
Member

gafter commented Feb 12, 2018

Having said that, I like what @MadsTorgersen wrote into the spec and I'm happy to make the compiler do that.

gafter added a commit that referenced this issue Feb 16, 2018
…f(...) (#24761)

* Permit selecting an instance from a type in a sub-expression in nameof(...)

Fixes #20600
@gafter
Copy link
Member

gafter commented Feb 20, 2018

Fixed in #24761 for dev15.7.x.

@gafter gafter closed this as completed Feb 20, 2018
@gafter gafter added Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented and removed 4 - In Review A fix for the issue is submitted for review. labels Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Language-C# Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

3 participants