Skip to content

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

@vsfeedback

Description

@vsfeedback

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()

Metadata

Metadata

Assignees

Labels

Area-CompilersBugDeveloper CommunityThe issue was originally reported on https://developercommunity.visualstudio.comResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions