roslyn: UnboundIdentifiers: 'nameof' does not exist in the current context.

In a lambda expression. If I write a #warning directive, the nameof will be error with

CS0103 The name ‘nameof’ does not exist in the current context.

Test codes:

Action action = () =>
{
    var x = nameof(System);
#warning xxx
};

But there is an interesting phenomenon, the codes can be compile successed and run correctly.

In my test, this bug is appears in both of Visual Studio 2015 and Visual Studio 2017.

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/71314/nameof-does-not-exist-in-the-current-context.html VSTS ticketId: 454314 These are the original issue comments:

彭闽 on 6/21/2017, 11:43 PM (169 days ago):

Action action = () => {
var x = nameof(System);
#warning xxx
};

Dan J on 8/30/2017, 11:06 AM (99 days ago): I also have this happen in 15.3.3, does not prevent build from suceeding, but constantly having 20 errors makes it hard to find what is actually boken These are the original issue solutions: (no solutions)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 63 (56 by maintainers)

Commits related to this issue

Most upvoted comments

I think the compiler should report semantic diagnostics from broken lambdas just like it does in broken methods.

For preventing analyzers from producing “CS…” diagnostics, I was thinking of adding a debug assertion in the `AnalyzerExecutor.IsSupportedDiagnostic".

Nice idea @jcouv! I would recommend going a step further and instead of adding a debug assert, we throw an ArgumentException with invalid diagnostic ID, just like we do for supported diagnostics with invalid identifier as an ID (see here). We probably want a slightly different message then CodeAnalysisResources.InvalidDiagnosticIdReported

@jcouv you can set break points here (all analyzeXXX methods) http://source.roslyn.io/#Microsoft.CodeAnalysis/DiagnosticAnalyzer/CompilerDiagnosticAnalyzer.CompilationAnalyzer.cs,30

and then open the file that shows the error and check the diagnostics returned by those method. one of them should contain that error if it surface to IDE as long as it is us who report it not third party analyzers.