Roslyn: Cannot compile some condition of switch expression

Created on 4 Aug 2020  路  8Comments  路  Source: dotnet/roslyn

_This issue has been moved from a ticket on Developer Community._

Related Developer Community post: https://developercommunity.visualstudio.com/content/problem/1161747/visual-studio-2019-professional-1672-doesnt-build.html

Duplicate issue: #46877
Duplicate issue: #46990
Duplicate issue: #46899
Duplicate issue: dotnet/core#5087
Duplicate issue: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1179705


[severity:I'm unable to use this version of Visual Studio] [regression] [worked-in:'16.6.3']
Simply some switch expression code cannot be compiled due to compiler crash.
I don't know what conditions occur this, but I already have 100% repro code.
Repro code is here:

using System;

namespace SwitchExpressionBug
{
    public enum EnumA { A, B, C }

    public enum EnumB { X, Y, Z }

    public class Class1
    {
        public string Repro(EnumA a, EnumB b)
            => (a, b) switch
            {
                (EnumA.A, EnumB.X) => "AX",
                (_, EnumB.Y) => "_Y",
                (EnumA.B, EnumB.X) => "BZ",
                (_, EnumB.Z) => "_Z",
                (_, _) => throw new ArgumentException()
            };
    }
}

Visual Studio build result:

1>------ Rebuild All started: Project: SwitchExpressionBug, Configuration: Debug Any CPU ------
1>You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(59,5): error MSB6006: "csc.exe" exited with code -2146232797.
1>Done building project "SwitchExpressionBug.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

.NET 5.0 SDK 5.0.100-preview.6.20318.15

$ dotnet build
Microsoft (R) Build Engine version 16.7.0-preview-20310-07+ee1c9fd0c for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error : Process terminated. System.NullReferenceException: Object reference not set to an instance of an object. [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.BasicBlock.ShortenBranches(Int32& delta) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray`1 lambdaDebugInfo, ImmutableArray`1 closureDebugInfo, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, Boolean emitTestCoverageData, ImmutableArray`1 dynamicAnalysisSpans, AsyncForwardEntryPoint entryPointOpt) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Environment.FailFast(System.String, System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FailFast.OnFatalException(System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FatalError.Report(System.Exception, System.Action`1<System.Exception>) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FatalError.ReportUnlessCanceled(System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder+BasicBlock.ShortenBranches(Int32 ByRef) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(Microsoft.CodeAnalysis.CSharp.Emit.PEModuleBuilder, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, Microsoft.CodeAnalysis.CSharp.BoundStatement, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.LambdaDebugInfo>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.ClosureDebugInfo>, Microsoft.CodeAnalysis.CSharp.StateMachineTypeSymbol, Microsoft.CodeAnalysis.CodeGen.VariableSlotAllocator, Microsoft.CodeAnalysis.DiagnosticBag, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider, Microsoft.CodeAnalysis.CSharp.ImportChain, Boolean, Boolean, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.SourceSpan>, AsyncForwardEntryPoint) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, ProcessedFieldInitializers ByRef, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields, Microsoft.CodeAnalysis.CSharp.TypeCompilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Roslyn.Utilities.UICultureUtilities+<>c__DisplayClass5_0.<WithCurrentUICulture>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.InnerInvoke() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task+<>c.<.cctor>b__276_0(System.Object) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.ThreadPoolWorkQueue.Dispatch() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error : System.NullReferenceException: Object reference not set to an instance of an object. [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.BasicBlock.ShortenBranches(Int32& delta) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray`1 lambdaDebugInfo, ImmutableArray`1 closureDebugInfo, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, Boolean emitTestCoverageData, ImmutableArray`1 dynamicAnalysisSpans, AsyncForwardEntryPoint entryPointOpt) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]

Build FAILED.

C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error : Process terminated. System.NullReferenceException: Object reference not set to an instance of an object. [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.BasicBlock.ShortenBranches(Int32& delta) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray`1 lambdaDebugInfo, ImmutableArray`1 closureDebugInfo, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, Boolean emitTestCoverageData, ImmutableArray`1 dynamicAnalysisSpans, AsyncForwardEntryPoint entryPointOpt) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Environment.FailFast(System.String, System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FailFast.OnFatalException(System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FatalError.Report(System.Exception, System.Action`1<System.Exception>) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.FatalError.ReportUnlessCanceled(System.Exception) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder+BasicBlock.ShortenBranches(Int32 ByRef) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(Microsoft.CodeAnalysis.CSharp.Emit.PEModuleBuilder, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, Microsoft.CodeAnalysis.CSharp.BoundStatement, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.LambdaDebugInfo>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.ClosureDebugInfo>, Microsoft.CodeAnalysis.CSharp.StateMachineTypeSymbol, Microsoft.CodeAnalysis.CodeGen.VariableSlotAllocator, Microsoft.CodeAnalysis.DiagnosticBag, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider, Microsoft.CodeAnalysis.CSharp.ImportChain, Boolean, Boolean, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.SourceSpan>, AsyncForwardEntryPoint) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, ProcessedFieldInitializers ByRef, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields, Microsoft.CodeAnalysis.CSharp.TypeCompilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Roslyn.Utilities.UICultureUtilities+<>c__DisplayClass5_0.<WithCurrentUICulture>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.InnerInvoke() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task+<>c.<.cctor>b__276_0(System.Object) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading.ThreadPoolWorkQueue.Dispatch() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error : System.NullReferenceException: Object reference not set to an instance of an object. [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.BasicBlock.ShortenBranches(Int32& delta) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray`1 lambdaDebugInfo, ImmutableArray`1 closureDebugInfo, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, Boolean emitTestCoverageData, ImmutableArray`1 dynamicAnalysisSpans, AsyncForwardEntryPoint entryPointOpt) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
C:\Program Files\dotnet\sdk\5.0.100-preview.6.20318.15\Roslyn\Microsoft.CSharp.Core.targets(59,5): error :    at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0() [C:\temp\SwitchExpressionBug\SwitchExpressionBug\SwitchExpressionBug.csproj]
    0 Warning(s)
    43 Error(s)

Time Elapsed 00:00:06.03

This could be compiled with Visual Studio 2019 but cannot with Visual Studio 2019 Preview.


Original Comments

Feedback Bot on 7/21/2020, 08:11 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Area-Compilers Bug New Language Feature - Pattern Matching

Most helpful comment

The fix for this bug will be available in a 16.7 patch release. Thanks again for reporting.

All 8 comments

This bug now leaked into the latest stable release of VS 16.7
I'm getting similar error, after upgrade

Description: The application requested process termination through System.Environment.FailFast(string message).
Message: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.BasicBlock.ShortenBranches(Int32& delta)
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches()
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks()
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize()
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl()
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray`1 lambdaDebugInfo, ImmutableArray`1 closureDebugInfo, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, Boolean emitTestCoverageData, ImmutableArray`1 dynamicAnalysisSpans, AsyncForwardEntryPoint entryPointOpt)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0()
Stack:
   at System.Environment.FailFast(System.String, System.Exception)
   at Microsoft.CodeAnalysis.FailFast.OnFatalException(System.Exception)
   at Microsoft.CodeAnalysis.FatalError.ReportUnlessCanceled(System.Exception)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0()
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder+BasicBlock.ShortenBranches(Int32 ByRef)
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.ComputeOffsetsAndAdjustBranches()
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.RealizeBlocks()
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.Realize()
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl()
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(Microsoft.CodeAnalysis.CSharp.Emit.PEModuleBuilder, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, Microsoft.CodeAnalysis.CSharp.BoundStatement, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.LambdaDebugInfo>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.ClosureDebugInfo>, Microsoft.CodeAnalysis.CSharp.StateMachineTypeSymbol, Microsoft.CodeAnalysis.CodeGen.VariableSlotAllocator, Microsoft.CodeAnalysis.DiagnosticBag, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider, Microsoft.CodeAnalysis.CSharp.ImportChain, Boolean, Boolean, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.CodeGen.SourceSpan>, AsyncForwardEntryPoint)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Int32, ProcessedFieldInitializers ByRef, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields, Microsoft.CodeAnalysis.CSharp.TypeCompilationState)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>c__DisplayClass22_0.<CompileNamedTypeAsync>b__0()
   at Roslyn.Utilities.UICultureUtilities+<>c__DisplayClass5_0.<WithCurrentUICulture>b__0()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Tried to remove all switch patterns that might cause the issue but still can't build.

The following code will reproduce the issue

public class StateMachine
    {
        public enum States
        {
            Uninitialized,
            Initialized,

            Opened,
            Closed,
        }

        public bool IsValid(States fromState, States toState) => (fromState, toState) switch
        {
            (States.Uninitialized, States.Initialized) => true,

            (States.Initialized, States.Opened) => true,
            (States.Initialized, States.Closed) => true, // --> comment only this line to allow build without errors

            (States.Opened, States.Closed) => true,

            (_, States.Uninitialized) => true, // --> comment only this line to allow build without errors

            _ => false
        };
    }

as stated above, this would compile correctly on previous version 16.6.x

changing the line (_, States.Uninitialized) => true to (_, States next) when next == States.Uninitialized => true, will work in this specific case, but in other cases will not

using global.json to enforce previous sdk version 3.1.302 working when using dotnet cli, but via VS still failing although it should catch the same global.json rules

We hit an assert in the decision dag rewriter when compiling using a debug compiler.

The active test run was aborted. Reason: Test host process crashed : Process terminated. Assertion failed.
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.DecisionDagRewriter.LowerDecisionDagCore(BoundDecisionDag decisionDag) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.DecisionDagRewriter.cs:line 386
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.BaseSwitchLocalRewriter.LowerDecisionDag(BoundDecisionDag decisionDag) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_BasePatternSwitchLocalRewriter.cs:line 72
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.SwitchExpressionLocalRewriter.LowerSwitchExpression(BoundConvertedSwitchExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_SwitchExpression.cs:line 62
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.SwitchExpressionLocalRewriter.Rewrite(LocalRewriter localRewriter, BoundConvertedSwitchExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_SwitchExpression.cs:line 40
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitConvertedSwitchExpression(BoundConvertedSwitchExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_SwitchExpression.cs:line 26
   at Microsoft.CodeAnalysis.CSharp.BoundConvertedSwitchExpression.Accept(BoundTreeVisitor visitor) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Generated\BoundNodes.xml.Generated.cs:line 4572
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.Visit(BoundNode node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 149
   at Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithoutStackGuard(BoundExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\BoundTree\BoundTreeRewriter.cs:line 98
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.VisitExpressionWithStackGuard(BoundExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 230
   at Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.VisitExpressionWithStackGuard(Int32& recursionDepth, BoundExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\BoundTree\BoundTreeVisitors.cs:line 209
   at Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithStackGuard(BoundExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\BoundTree\BoundTreeRewriter.cs:line 93
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitExpressionImpl(BoundExpression node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs:line 209
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.Visit(BoundNode node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs:line 168
   at Microsoft.CodeAnalysis.CSharp.BoundTreeRewriter.VisitReturnStatement(BoundReturnStatement node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Generated\BoundNodes.xml.Generated.cs:line 10123
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitReturnStatement(BoundReturnStatement node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_ReturnStatement.cs:line 17
   at Microsoft.CodeAnalysis.CSharp.BoundReturnStatement.Accept(BoundTreeVisitor visitor) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Generated\BoundNodes.xml.Generated.cs:line 3130
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatement(BoundStatement node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs:line 194
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitPossibleUsingDeclaration(BoundStatement node, ImmutableArray`1 statements, Int32 statementIndex, Boolean& replacedLocalDeclarations) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_Block.cs:line 103
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatementSubList(ArrayBuilder`1 builder, ImmutableArray`1 statements, Int32 startIndex) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_Block.cs:line 61
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitBlock(BoundBlock node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter_Block.cs:line 22
   at Microsoft.CodeAnalysis.CSharp.BoundBlock.Accept(BoundTreeVisitor visitor) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Generated\BoundNodes.xml.Generated.cs:line 2862
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatement(BoundStatement node) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs:line 194
   at Microsoft.CodeAnalysis.CSharp.LocalRewriter.Rewrite(CSharpCompilation compilation, MethodSymbol method, Int32 methodOrdinal, NamedTypeSymbol containingType, BoundStatement statement, TypeCompilationState compilationState, SynthesizedSubmissionFields previousSubmissionFields, Boolean allowOmissionOfConditionalCalls, Boolean instrumentForDynamicAnalysis, ImmutableArray`1& dynamicAnalysisSpans, DebugDocumentProvider debugDocumentProvider, DiagnosticBag diagnostics, Boolean& sawLambdas, Boolean& sawLocalFunctions, Boolean& sawAwaitInExceptionHandler) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Lowering\LocalRewriter\LocalRewriter.cs:line 109
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.LowerBodyOrInitializer(MethodSymbol method, Int32 methodOrdinal, BoundStatement body, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState, Boolean instrumentForDynamicAnalysis, DebugDocumentProvider debugDocumentProvider, ImmutableArray`1& dynamicAnalysisSpans, DiagnosticBag diagnostics, VariableSlotAllocator& lazyVariableSlotAllocator, ArrayBuilder`1 lambdaDebugInfoBuilder, ArrayBuilder`1 closureDebugInfoBuilder, StateMachineTypeSymbol& stateMachineTypeOpt) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs:line 1302
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs:line 1129
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) in c:\Users\rikki\src\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs:line 506

We're facing a similar error with the same NullReferenceException in BasicBlock.ShortenBranches(Int32& delta) when using dotnet SDK 3.1.401. The code compiles fine with SDK 3.1.302.

My smallest reproduction is the following code. Just remove any of the non-default cases and the code compiles just fine.

namespace RoslynCrash
{
    public class Program
    {
        public static int Main(string[] _)
        {
            return ("", 0) switch
            {
                ("", 1) => 0,
                ("", 2) => 0,
                (_, 3) => 0,
                _ => 0
            };
        }
    }
}

Closing as duplicate of https://github.com/dotnet/roslyn/issues/46877~~ My bad this issue is newer.

I believe the problem is in lowering decision dags. If we disable generating a switch dispatch by changing the line to if (false && GenerateSwitchDispatch(...)), the test starts passing.

https://github.com/dotnet/roslyn/blob/894972542b1756efe24811c08420f98ad502ea93/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter.DecisionDagRewriter.cs#L396

I think the general scenario that's broken is when there is more than one way of reaching a node in the dag, and we are also generating a switch dispatch for one of the inputs. Thus a case like the following also crashes:

class Program
{
    static int Main()
    {
        return ("", "") switch
        {
            ("a", "x") => 0,
            ("a", "y") => 0, 
            ("a" or "b", "z") => 0
        };
    }
}

Here you can reach the "a" or "b" arm in multiple ways (either reached when item1 is "a" and item2 is not 'x" and not "y", or reached when item2 is not "a").

Another case of similar complexity:

class Program
{
    static int Main()
    {
        return (false, 0) switch
        {
            (false, 0) => 0,
            (false, 1) => 0, 
            (false or true, 2) => 0
        };
    }
}

I found that the bug was introduced in #42313 by checking out its parent commit and running the repro test on it. Not certain what the correct fix is yet. /cc @gafter

The fix for this bug will be available in a 16.7 patch release. Thanks again for reporting.

Was this page helpful?
0 / 5 - 0 ratings