Roslyn: csc.exe compiler error -2146232797 with ref struct C# 7.2 modifier

Created on 12 Feb 2018  路  11Comments  路  Source: dotnet/roslyn

Hello, both VS 15.5 Preview 5 and the official VS 15.5 fail to compile my library using the C# 7.2 ref struct modifier in one of the structs I'm using.

Here's the link to the exact code that reproduces the error, just clone and try to compile the library "NeuralNetwork.NET":
https://github.com/Sergio0694/NeuralNetwork.NET/tree/VS_test_csc-compiler-error

Removing the ref struct modifier from the "FloatSpan2D" struct solves the issue.

If this is not a bug and that struct is actually being moved to the heap somehow/somewhere, it'd be useful to have some sort of warning to be able to fix the code error.
Otherwise, I hope this will help you pinpoint the cause of the issue.
Cheers!

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/160134/cscexe-compiler-error-2146232797-with-ref-struct-c.html
VSTS ticketId: 533142_
_These are the original issue comments:_
(no comments)
_These are the original issue solutions:_
(no solutions)


Edit by @OmarTawfik
Smaller repro:

public ref struct Tensor {
    public void TryFree() { }
}
public class C {
        public static unsafe void TryFree(params Tensor[] tensors)
        {
            fixed (Tensor* p = tensors)
                for (int i = 0; i < tensors.Length; i++)
                    p[i].TryFree();
        }
}

It hits the following assert:

System.InvalidOperationException: Unexpected value 'PointerElementAccess expression of Tensor type' of type 'System.String'
   at Microsoft.CodeAnalysis.CSharp.Binder.GetValEscape(BoundExpression expr, UInt32 scopeOfTheContainingExpression)
   at Microsoft.CodeAnalysis.CSharp.Binder.CheckInvocationArgMixing(SyntaxNode syntax, Symbol symbol, BoundExpression receiverOpt, ImmutableArray`1 parameters, ImmutableArray`1 argsOpt, ImmutableArray`1 argRefKindsOpt, ImmutableArray`1 argsToParamsOpt, UInt32 scopeOfTheContainingExpression, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpressionContinued(SyntaxNode node, SyntaxNode expression, String methodName, OverloadResolutionResult`1 result, AnalyzedArguments analyzedArguments, MethodGroup methodGroup, NamedTypeSymbol delegateTypeOpt, DiagnosticBag diagnostics, CSharpSyntaxNode queryClause)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodGroupInvocation(SyntaxNode syntax, SyntaxNode expression, String methodName, BoundMethodGroup methodGroup, AnalyzedArguments analyzedArguments, DiagnosticBag diagnostics, CSharpSyntaxNode queryClause, Boolean allowUnexpandedForm)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpression(SyntaxNode node, SyntaxNode expression, String methodName, BoundExpression boundExpression, AnalyzedArguments analyzedArguments, DiagnosticBag diagnostics, CSharpSyntaxNode queryClause, Boolean allowUnexpandedForm)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpression(InvocationExpressionSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(CSharpSyntaxNode node, ExpressionSyntax syntax, Boolean allowsAnyExpression, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(ExpressionStatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindPossibleEmbeddedStatement(StatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.ForLoopBinder.BindForParts(ForStatementSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.ForLoopBinder.BindForParts(DiagnosticBag diagnostics, Binder originalBinder)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindPossibleEmbeddedStatement(StatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindFixedStatementParts(FixedStatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindFixedStatement(FixedStatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlockParts(BlockSyntax node, DiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.BindMethodBody(MethodSymbol method, TypeCompilationState compilationState, DiagnosticBag diagnostics, ImportChain& importChain, Boolean& originalBodyNested)
   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__DisplayClass21_0.<CompileNamedTypeAsTask>b__0()

Area-Compilers Bug Developer Community Resolution-Fixed

Most helpful comment

Attached the root cause to the issue (please see the top message). It is an unexpected value in escape rules.
cc @VSadov
Thanks @Sergio0694!

All 11 comments

Tagging @VSadov @OmarTawfik

Looks like the link from the initial bug report is broken. @Sergio0694 can you point us to a commit where this problem repos?

@jaredpar My bad, I removed that branch a while back as I got no answer from my issue and thought the error was already in your backlog, I'll see if I have a copy of that test branch somewhere, or if I can reproduce the same issue in another branch 馃憤

@Sergio0694 yeah sorry for the slow response. Ideally VS feedback issues should make it to the right team quickly but it can get delayed at times for a variety of reasons. If you can get the branch please lte us know cause we'd like to make sure that's fixed.

@jaredpar No worries, I should have just left that branch there 馃槉
I'm currently recreating a repro for you to use, and in the meantime I've found another related bug.

Steps to reproduce:

  • Open VS (I'm using VS Enterprise 15.5.5)
  • Clone this: https://github.com/Sergio0694/NeuralNetwork.NET
  • Inside the NeuralNetwork.NET project, go to APIs > Structs > Tensor, and add the ref modifier (Note: I know the ref modifier is not valid for this struct, see the note at the bottom)
  • Save the file, and VS will crash with this message (tried twice in a row just now):

image

The message says: "An irreversible error occurred in a Visual Studio process. It is recommended to save the files, close and restart Visual Studio".

I'll let you know as soon as I have a branch published with a repro for that other bug, if I manage to reproduce it.

NOTE: I have changed the library since my original issue, so I can no longer apply the ref modifier in theory right now, as I have a few places where I'm storing instances of this Tensor struct in the heap. I'll see if I can reproduce that same compiler error anyways.

NOTE 2: while working on the repro, I've noticed that in general, as soon as I add the ref modifier to any struct in the project, Visual Studio becomes unstable and crashes every now and then (4th time in 5 minutes now).

@jaredpar I've got a partial repro here - it doesn't reproduce that same error, but for some reason every single time I open it Visual Studio doesn't last a single minute before crashing.

Hope this helps! 馃槃

NeuralNetworkDotNetReproV1.zip

Seeing if I can get a smaller repro now.

Attached the root cause to the issue (please see the top message). It is an unexpected value in escape rules.
cc @VSadov
Thanks @Sergio0694!

@OmarTawfik Sure, glad I could help! 馃槃

I'll take this one.

Omar fixed this in 15.7 (https://github.com/dotnet/roslyn/pull/24909)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glennblock picture glennblock  路  3Comments

orthoxerox picture orthoxerox  路  3Comments

MadsTorgersen picture MadsTorgersen  路  3Comments

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments

binki picture binki  路  3Comments