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& 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.<>c__DisplayClass14_0.<FormatAsync>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(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Formatting.AbstractSyntaxFormattingService.FormatIndividuallyAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.CleanupDocumentAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessChangesAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
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)
@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.