Roslyn: PopulateSwitchCodeFixProvider encountered an error and has been disabled

Created on 1 Jan 2019  路  6Comments  路  Source: dotnet/roslyn

I am trying to play with a new C# 8 language feature using switch. The compiler doesn't like what I have done and adds a red squiggle to the "switch" keyword. When I put the cursor on it and then click the light bulb, I get a message in the yellow info bar at the top that says:

'PopulateSwitchCodeFixProvider' encountered an error and has been disabled

It then has a link to "Show Stack Trace" (I have included it below) and 2 buttons, "Enable" and "Enable and ignore future errors".

If I click on "Enable" and then on the light bulb, it does the same thing again.

The code I am trying to write is:

static string GetName(Person p)
{
    return (p.FirstName, p.MiddleName, p.LastName) switch
        {
            (string f, string m, string l)  => $"{f} {m[0]}. {l}",
            (string f, null, string l)  => $"{f} {l}",
            (string f, string m, null)  => $"{f} {m[0]}. <someone>",
            (string f, null, null)      => $"{f} <someone>",
            (null, string m, string l)  => $"Ms/Mr {m[0]}. {l}",
            (null, null, string l)      => $"Ms/Mr {l}",
            (null, string m, null)      => $"Ms/Mr {m[0]}. <someone>",
            (null, null, null)      => $"Ms/Mr <someone>",
        };
}
The stack trace is:
System.InvalidOperationException : Unexpected false
   at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition,String message)
   at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaDataFactory.Analyzer.Analyze(SyntaxTriviaList list,AnalysisResult&amp; result)
   at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaDataFactory.Analyzer.Between(SyntaxToken token1,SyntaxToken token2)
   at Microsoft.CodeAnalysis.CSharp.Formatting.TriviaDataFactory.Create(SyntaxToken token1,SyntaxToken token2)
   at Microsoft.CodeAnalysis.Formatting.TokenStream.GetOriginalTriviaData(TokenData token1,TokenData token2)
   at Microsoft.CodeAnalysis.Formatting.TokenStream.GetColumn(TokenData tokenData,Func`3 triviaDataGetter)
   at Microsoft.CodeAnalysis.Formatting.FormattingContext.AddAnchorIndentationOperation(AnchorIndentationOperation operation)
   at Roslyn.Utilities.EnumerableExtensions.Do[T](IEnumerable`1 source,Action`1 action)
   at Microsoft.CodeAnalysis.Formatting.AbstractFormatEngine.&lt;&gt;c__DisplayClass14_0.&lt;FormatAsync&gt;b__0(Task`1 task)
   at Microsoft.CodeAnalysis.Formatting.TaskExecutor.SynchronousExecutor.ContinueWith[T1,T2](Task`1 previousTask,Func`2 nextAction,CancellationToken cancellationToken)
   at async Microsoft.CodeAnalysis.Formatting.AbstractFormatEngine.FormatAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Formatting.AbstractSyntaxFormattingService.FormatIndividuallyAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.CleanupDocumentAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessChangesAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.&lt;&gt;c__DisplayClass11_0.&lt;GetPreviewAsync&gt;b__0(&lt;Unknown Parameters&gt;)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](&lt;Unknown Parameters&gt;)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/416889/populateswitchcodefixprovider-encountered-an-error.html
VSTS ticketId: 757882_
_These are the original issue comments:_
(no comments)
_These are the original issue solutions:_
(no solutions)

Area-IDE Bug Developer Community

All 6 comments

@jinujoseph This seems similar, but I'm not sure it's a duplicate

I wasn't able to repro the stack trace above in preview 2, however the switch expression still doesn't compile for me. The lightbulb suggests fixing unreachable code by changing to the following:

C# return (p.FirstName, p.MiddleName, p.LastName) switch () { default: break; } (string f, string m, string l) => $"{f} {m[0]}. {l}", (string f, null, string l) => $"{f} {l}", (string f, string m, null) => $"{f} {m[0]}. <someone>", (string f, null, null) => $"{f} <someone>", (null, string m, string l) => $"Ms/Mr {m[0]}. {l}", (null, null, string l) => $"Ms/Mr {l}", (null, string m, null) => $"Ms/Mr {m[0]}. <someone>", (null, null, null) => $"Ms/Mr <someone>" };

Looks related to #nullable enable to hit original stack trace. Without enabling nullable doesn't hit this issue

also reported at DC

I believe this is fixed in Preview 2, which will be available soon. If you are still hitting this issue please reach out to us.

Was this page helpful?
0 / 5 - 0 ratings