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.07k stars 4.04k forks source link

Error MSB6006 "csc.exe" exited with code -2146232797 #73252

Open gao-artur opened 6 months ago

gao-artur commented 6 months ago

Version Used: 8.0.204

Steps to Reproduce:

  1. Download the repro: https://github.com/SimonCropp/RuntimeHelpersRepro
  2. Try to build

Expected Behavior: Builds successfully

Actual Behavior: Build fails

Error MSB6006 "csc.exe" exited with code -2146232797. ClassLibrary1 C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets 84

There is an exception when building with the dotnet build command

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CodeGen.ReferenceDependencyWalker.VisitSignature(ISignature signature, EmitContext context)
   at Microsoft.CodeAnalysis.Emit.CommonPEModuleBuilder.GetFakeSymbolTokenForIL(ISignature symbol, SyntaxNode syntaxNode, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CodeGen.ILBuilder.EmitArrayBlockInitializer(ImmutableArray`1 data, SyntaxNode syntaxNode, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.<TryEmitOptimizedReadonlySpanCreation>g__tryEmitAsCachedArrayFromBlob|88_1(NamedTypeSymbol spanType, BoundExpression wrappedExpression, Int32 elementCount, ImmutableArray`1 data, ArrayTypeSymbol& arrayType, TypeSymbol elementType)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.TryEmitOptimizedReadonlySpanCreation(NamedTypeSymbol spanType, BoundExpression wrappedExpression, Boolean used, BoundExpression inPlaceTarget, Boolean& avoidInPlace, BoundExpression start, BoundExpression length)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.TryEmitOptimizedReadonlySpan(BoundObjectCreationExpression expression, Boolean used, BoundExpression inPlaceTarget, Boolean& avoidInPlace)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitObjectCreationExpression(BoundObjectCreationExpression expression, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(BoundExpression expression, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitArguments(ImmutableArray`1 arguments, ImmutableArray`1 parameters, ImmutableArray`1 argRefKindsOpt)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStaticCallExpression(BoundCall call, UseKind useKind)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(BoundExpression expression, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitSequenceExpression(BoundSequence sequence, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpressionCoreWithStackGuard(BoundExpression expression, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitExpression(BoundExpression expression, Boolean used)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatementAndCountInstructions(BoundStatement statement)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitSequencePointStatement(BoundSequencePoint node)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatements(ImmutableArray`1 statements)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitUninstrumentedBlock(BoundBlock block)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatements(ImmutableArray`1 statements)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitUninstrumentedBlock(BoundBlock block)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatementList(BoundStatementList list)
   at Microsoft.CodeAnalysis.CSharp.CodeGen.CodeGenerator.EmitStatement(BoundStatement statement)
   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 orderedLambdaRuntimeRudeEdits, ImmutableArray`1 closureDebugInfo, ImmutableArray`1 stateMachineStateDebugInfos, StateMachineTypeSymbol stateMachineTypeOpt, VariableSlotAllocator variableSlotAllocatorOpt, BindingDiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, ImportChain importChainOpt, Boolean emittingPdb, ImmutableArray`1 codeCoverageSpans, 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__DisplayClass25_0.<CompileNamedTypeAsync>b__0()

Note that the content of the RuntimeHelpers doesn't matter. The build fails also when the class is empty. When the class is renamed to anything else but RuntimeHelpers the build succeeds. For the context, the error was encountered when I tried to add support for the array range operator to the Polyfill library that brings newer compiler features to older TFMs.

cston commented 6 months ago

It looks like the compiler is not checking for the existence of System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray.

gao-artur commented 6 months ago

@cston does it mean I can just add this as a workaround?

[Intrinsic]
public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Field, Inherited = false)]
internal sealed class IntrinsicAttribute : Attribute
{
}

It seems to work, but I just want to confirm this is the correct way to do that.

cston commented 6 months ago

@gao-artur, I'm not sure if re-declaring the existing BCL method as extern is expected to work.

However, in general, it does appear that re-declaring an existing BCL type in source will essentially hide any well-known members of that type that the compiler may depend on that are not also re-declared.

gao-artur commented 6 months ago

It works, all tests pass, but ReSharper unit tests runner shows the following error. Is it safe to ignore, or can it explode in runtime?

ERROR TestRunner: JetBrains.ReSharper.TestRunner.Adapters.NUnit3.NUnitTestReporter Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. ----> Could not load type 'System.Runtime.CompilerServices.RuntimeHelpers' from assembly 'Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'InitializeArray' has no implementation (no RVA).

CyrusNajmabadi commented 6 months ago

@gao-artur you'd have to try out and see.