Allow nameof to access instance members.#48754
Allow nameof to access instance members.#48754YairHalberstadt wants to merge 19 commits intodotnet:mainfrom
Conversation
… to capture ref/struct this variables. This fixes a bug where members of instance members could not be accessed inside a nameof in a static context.
|
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
| (currentType.IsInterface && (declaringType.IsObjectType() || currentType.AllInterfacesNoUseSiteDiagnostics.Contains(declaringType)))) | ||
| { | ||
| bool hasErrors = false; | ||
| if (EnclosingNameofArgument != node) |
There was a problem hiding this comment.
@gafter I think you wrote this code originally. Was it intentional to not allow nameof(InstanceProperty.Member) in a static context, but allow nameof(InstanceProperty)?
|
I am not sure I agree with interpretation of the spec. I think the quote about Simple names and Member access talks about them at the top level of the are expected because, even though |
|
If you look at the grammar given in the spec, both "P" and "Length" are parsed as named_entity |
|
Here is the quote in context:
The paragraph is talking about the named_entity of a nameof_expression, i.e. the one in the grammar nameof_expression
: 'nameof' '(' named_entity ')'
; Not about any named entity |
|
So either both of these should be an error or none? public class C
{
[Attr(nameof(Other.Length))] // error
public string Other;
string M() => nameof(Other.Length); // ok
} |
|
Ah I think attribute case is considered a static context so the error is correct. |
|
I've opened an issue on csharplang to clarify the meaning of the spec, and consider changing it in a future language version if the current implementation is not a bug: dotnet/csharplang#4037 |
…nameof-to-access-instance-members
|
I've now changed this to only work in preview language version, thereby implementing dotnet/csharplang#4037 |
|
Done review pass (commit 3). I'm not certain what IDE features will be impacted by this, will need a buddy to advise. #Closed |
Allow accessing static members from an instance in nameof
|
Done review pass (commit 7). Looking good overall, couple of small comments. If the recursive tests do end up becoming an issue let me know on discord and I can help you troubleshoot. #Closed |
|
@AlekseyTs for a second review. |
|
@YairHalberstadt It looks like there are legitimate test failures and a merge conflict. Could you please address those? |
…ess-instance-members * upstream/main: (669 commits) Fix 'hasStaticConstructor' check in MethodCompiler (dotnet#59116) Update dependencies from https://github.com/dotnet/arcade build 20220127.8 (dotnet#59134) Update StreamJsonRpc (dotnet#59073) Resources Filter cancellation exceptions in generator driver (dotnet#58843) Revert "Remove dependency on EditorFeatures from Remote.ServiceHub project (dotnet#59059)" Strings Inline Convert to switch expression Explicitly test empty string case. Fix comment Simplify test code Run all Add tests Delete test generator Add support for specifying server in tests Remove options review feedback Format document after each provider (dotnet#59091) [main] Update dependencies from dotnet/arcade (dotnet#59015) ...
|
Done with review pass (commit 14) |
|
Moving this to draft to take it off the backlog. @YairHalberstadt, do you think you'll have time to address feedback here? |
|
@333fred probably not |
|
Alright. We're going to have @jjonescz pick this up to get it across the finish line. |
8250658 to
ad3e699
Compare
|
For some reason CodeFlow crashes while trying to load this PR. Could be due to very old commits in the PR. In order to confirm this theory, could you clone the branch in a new branch, rebase it on top of main and create a different PR from that new branch? |
Yes, happens to me, as well.
|
|
CodeFlow works for that PR. I suggest continue working in that branch. |
Alright. Closing this in favor of #67461. |
Fixes #40229
nameofis allowed to access instance members in a static context, and to capture ref / structthisvariables. This fixes a bug where members of instance members could not be accessed inside anameofin a static context.The relevant part of the spec is this:
For example, currently the following is allowed:
But the following isn't:
This PR fixes these cases.
There was also an inconsistency in the previous implementation, where the following was allowed:
But this produces an error:
It is probably a bug that this is allowed for methods, but this cannot be changed due to back compat. Whilst I'm not sure whether this is worth fixing on it's own for various reasons, I think it fits well enough under the umbrella of the other changes being made here that it's worth allowing in C#10 for consistency.
Things this PR doesn't fix
The following is still illegal:
This is because even if we removed that error, we would still get the following error:
So I didn't think it worth fixing.
I also don't allow use of
thisandbaseinnameofsince I'm not sure if that's a bug.