dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.1k stars 4.04k forks source link

Wrong error when compiling code containing IEnumerable<Span<T>> #76007

Closed aannenko closed 4 days ago

aannenko commented 4 days ago

Version Used: 9.0.100

Steps to Reproduce:

  1. dotnet new console
  2. Replace contents of the Program.cs with this:
    
    foreach (var item in IntsAsStrings(5))
    Console.WriteLine(item.ToString());

// intentionally invalid code static IEnumerable<ReadOnlySpan> IntsAsStrings(int count) { var chars = new char[11]; for (var i = 0; i < count; i++) { i.TryFormat(chars, out var charsWritten); yield return chars.AsSpan(0, charsWritten); } }

3. Try compiling it.

**Diagnostic Id**:
| Severity | Code | Description | Project | File | Line | Suppression State |
|---|---|---|---|---|---|---|
| Error (active) | MSB6006 | "csc.exe" exited with code -2146232797. | test | C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets | 89 | |

**Expected Behavior**:
Like in .NET 8, we get an appropriate error:

CS9244 The type 'ReadOnlySpan<char>' may not be a ref struct or a type parameter allowing ref structs in order to use it as parameter 'T' in the generic type or method 'IEnumerable<T>'

**Actual Behavior**:
csc.exe tries and fails to compile the above code.

`dotnet build` produces the following output:

PS D:\Code\test> dotnet build Restore complete (0.2s) test failed with 58 error(s) (8.0s) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : Process terminated. System.InvalidOperationException: Unexpected value 'NoConversion' of type 'Microsoft.CodeAnalysis.CSharp.ConversionKind' C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversion(BoundConversion conversion) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversionExpression(BoundConversion conversion, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpressionCoreWithStackGuard(BoundExpression expression, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(BoundExpression expression, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitReturnStatement(BoundReturnStatement boundReturnStatement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatements(ImmutableArray1 statements) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitUninstrumentedBlock(BoundBlock block) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray1 lambdaDebugInfo, ImmutableArray1 orderedLambdaRuntimeRudeEdits, ImmutableArray1 closureDebugInfo, ImmutableArray1 stateMachineStateDebugInfos, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, BindingDiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, ImmutableArray1 codeCoverageSpans, AsyncForwardEntryPoint entryPointOpt) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileSynthesizedMethods(TypeCompilationState compilationState) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>cDisplayClass25_0.b0() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Environment.FailFast(System.Runtime.CompilerServices.StackCrawlMarkHandle, System.String, System.Runtime.CompilerServices.ObjectHandleOnStack, System.String) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Environment.FailFast(System.Threading.StackCrawlMark ByRef, System.String, System.Exception, System.String) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Environment.FailFast(System.String, System.Exception) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.FailFast.OnFatalException(System.Exception) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.ErrorReporting.FatalError.Report(System.Exception, Microsoft.CodeAnalysis.ErrorReporting.ErrorSeverity, Boolean) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.ErrorReporting.FatalError.ReportAndPropagateUnlessCanceled(System.Exception, Microsoft.CodeAnalysis.ErrorReporting.ErrorSeverity) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>cDisplayClass25_0.b0() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Runtime.EH.FindFirstPassHandler(System.Object, UInt32, System.Runtime.StackFrameIterator ByRef, UInt32 ByRef, Byte* ByRef) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Runtime.EH.DispatchEx(System.Runtime.StackFrameIterator ByRef, ExInfo ByRef) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Runtime.EH.RhThrowEx(System.Object, ExInfo ByRef) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversion(Microsoft.CodeAnalysis.CSharp.BoundConversion) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversionExpression(Microsoft.CodeAnalysis.CSharp.BoundConversion, Boolean) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpressionCoreWithStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression, Boolean) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(Microsoft.CodeAnalysis.CSharp.BoundExpression, Boolean) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitReturnStatement(Microsoft.CodeAnalysis.CSharp.BoundReturnStatement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(Microsoft.CodeAnalysis.CSharp.BoundStatement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatements(System.Collections.Immutable.ImmutableArray1<Microsoft.CodeAnalysis.CSharp.BoundStatement>) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitUninstrumentedBlock(Microsoft.CodeAnalysis.CSharp.BoundBlock) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(Microsoft.CodeAnalysis.CSharp.BoundStatement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,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.ImmutableArray1, System.Collections.Immutable.ImmutableArray1<Microsoft.CodeAnalysis.CodeGen.LambdaRuntimeRudeEditInfo>, System.Collections.Immutable.ImmutableArray1, System.Collections.Immutable.ImmutableArray1<Microsoft.CodeAnalysis.CodeGen.StateMachineStateDebugInfo>, Microsoft.CodeAnalysis.CSharp.StateMachineTypeSymbol, Microsoft.CodeAnalysis.CodeGen.VariableSlotAllocator, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider, Microsoft.CodeAnalysis.CSharp.ImportChain, Boolean, System.Collections.Immutable.ImmutableArray1, AsyncForwardEntryPoint) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileSynthesizedMethods(Microsoft.CodeAnalysis.CSharp.TypeCompilationState) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler+<>cDisplayClass25_0.b0() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Threading.ThreadPoolWorkQueue.Dispatch() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : System.InvalidOperationException: Unexpected value 'NoConversion' of type 'Microsoft.CodeAnalysis.CSharp.ConversionKind' C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversion(BoundConversion conversion) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitConversionExpression(BoundConversion conversion, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpressionCoreWithStackGuard(BoundExpression expression, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(BoundExpression expression, Boolean used) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitReturnStatement(BoundReturnStatement boundReturnStatement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatements(ImmutableArray1 statements) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitUninstrumentedBlock(BoundBlock block) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.GenerateImpl() C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.GenerateMethodBody(PEModuleBuilder moduleBuilder, MethodSymbol method, Int32 methodOrdinal, BoundStatement block, ImmutableArray1 lambdaDebugInfo, ImmutableArray1 orderedLambdaRuntimeRudeEdits, ImmutableArray1 closureDebugInfo, ImmutableArray1 stateMachineStateDebugInfos, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, BindingDiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, ImmutableArray1 codeCoverageSpans, AsyncForwardEntryPoint entryPointOpt) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileSynthesizedMethods(TypeCompilationState compilationState) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) C:\Program Files\dotnet\sdk\9.0.100\Roslyn\Microsoft.CSharp.Core.targets(89,5): error : at Microsoft.CodeAnalysis.CSharp.MethodCompiler.<>cDisplayClass25_0.b0()

jjonescz commented 4 days ago

Looks like a duplicate of https://github.com/dotnet/roslyn/issues/75569.

aannenko commented 4 days ago

Yes, thanks. Should I close this one then?