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

Comments

Projects
None yet
3 participants
@alrz
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor Author

commented Jul 1, 2017

I suspect this is not per spec.

@sharwell

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

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 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 this to Next Up in Compiler: Gafter Feb 10, 2018

@gafter gafter added 3 - Working and removed help wanted labels Feb 10, 2018

gafter added a commit to gafter/roslyn that referenced this issue Feb 10, 2018

@gafter gafter added 4 - In Review and removed 3 - Working labels Feb 10, 2018

@gafter gafter moved this from Next Up to In Review in Compiler: Gafter Feb 10, 2018

@gafter

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

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

Permit selecting an instance from a type in a sub-expression in nameo…
…f(...) (#24761)

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

Fixes #20600
@gafter

This comment has been minimized.

Copy link
Member

commented Feb 20, 2018

Fixed in #24761 for dev15.7.x.

@gafter gafter closed this Feb 20, 2018

@gafter gafter removed this from In Review in Compiler: Gafter Feb 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.