Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException: Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind' #68194

Closed
vsfeedback opened this issue May 15, 2023 · 1 comment · Fixed by #68203
Assignees

Comments

@vsfeedback
Copy link

vsfeedback commented May 15, 2023

https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1818036

This issue has been moved from a ticket on Developer Community.


[severity:I'm unable to use this version]
I have a mixed C#/VB.NET solution. In C#, there is a class with ref-indexer (ref T this[int i]).
In VB, return value of the indexer is used in a With statement (With mWeeks(WeekIndex))
This results in exception in Roslyn: Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind' which breaks every active analyser etc.
F.e. if the CS project is referenced from VB, but the class name is not fully qualified, pressing Alt-Enter on the mWeeks variable below
results in "Feature 'Missing import discovery' is currently unavailable due to an internal error." with the stack trace below.

VB.NET class:

Public Class Class1
    Private Structure WeekType
        Dim WeekNumber As Integer
    End Structure

    Private mWeeks As MySpecialList(Of WeekType)

    Sub S()
        Dim WeekIndex As Integer
        With mWeeks(WeekIndex)
            .WeekNumber = 0
        End With

        Dim weekNumber = mWeeks(WeekIndex).WeekNumber
    End Sub
End Class

C# class:

namespace CSProj
{
    public class MySpecialList<T>
    {
        private T dummy;
        public ref T this[int i]
        {
            get
            {
                return ref dummy;
            }
        }
    }
}

Stacktrace:

StreamJsonRpc.RemoteInvocationException: One or more errors occurred. (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind') (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind') (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind')
   at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__145`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection`1.<TryInvokeAsync>d__22`1.MoveNext()
RPC server exception:
System.AggregateException: One or more errors occurred. (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind') (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind') (Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind')
 ---> System.InvalidOperationException: Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind'
      at Microsoft.CodeAnalysis.VisualBasic.WithExpressionRewriter.CaptureWithExpression(BoundExpression value, State state)
      at Microsoft.CodeAnalysis.VisualBasic.WithExpressionRewriter.AnalyzeWithExpression(Symbol containingMember, BoundExpression value, Boolean doNotUseByRefLocal, Binder binder, Boolean preserveIdentityOfLValues)
      at Microsoft.CodeAnalysis.VisualBasic.WithBlockBinder.EnsureExpressionAndPlaceholder()
      at Microsoft.CodeAnalysis.VisualBasic.WithBlockBinder.CreateBoundWithBlock(WithBlockSyntax node, Binder boundBlockBinder, BindingDiagnosticBag diagnostics)
      at Microsoft.CodeAnalysis.VisualBasic.Binder.CreateBoundWithBlock(WithBlockSyntax node, Binder boundBlockBinder, BindingDiagnosticBag diagnostics)
      at Microsoft.CodeAnalysis.VisualBasic.Binder.BindStatement(StatementSyntax node, BindingDiagnosticBag diagnostics)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.IncrementalBinder.BindStatement(StatementSyntax node, BindingDiagnosticBag diagnostics)
      at Microsoft.CodeAnalysis.VisualBasic.VBSemanticModel.Bind(Binder binder, SyntaxNode node, BindingDiagnosticBag diagnostics)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.GuardedIncrementalBind(SyntaxNode bindingRoot, Binder enclosingBinder)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.GetBoundNodes(SyntaxNode node)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.GetUpperBoundNode(SyntaxNode node)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.GetBoundNodeSummary(VisualBasicSyntaxNode node)
      at Microsoft.CodeAnalysis.VisualBasic.MemberSemanticModel.GetExpressionSymbolInfo(ExpressionSyntax node, SymbolInfoOptions options, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.SyntaxTreeSemanticModel.GetExpressionSymbolInfo(ExpressionSyntax node, SymbolInfoOptions options, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.VBSemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.VBSemanticModel.GetSymbolInfoForNode(SyntaxNode node, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.VBSemanticModel.GetSymbolInfoCore(SyntaxNode node, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.ModelExtensions.GetSymbolInfo(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService.Rewriter.GetAliasOrAnySymbol(SemanticModel model, SyntaxNode node, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService.Rewriter.VisitIdentifier(SyntaxToken token, SyntaxToken newToken)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService.Rewriter.VisitToken(SyntaxToken token)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService._Closure$__6-0._Lambda$__2(SyntaxToken t)
      at System.Threading.Tasks.Parallel.<>c__DisplayClass44_0`2.<PartitionerForEachWorker>b__1(IEnumerator& partitionState, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
   --- End of stack trace from previous location ---
      at System.Threading.Tasks.Parallel.<>c__DisplayClass44_0`2.<PartitionerForEachWorker>b__1(IEnumerator& partitionState, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
      at System.Threading.Tasks.TaskReplicator.Replica`1.ExecuteAction(Boolean& yieldedBeforeCompletion)
      at System.Threading.Tasks.TaskReplicator.Replica.Execute()
   --- End of inner exception stack trace ---
      at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure)
      at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   --- End of stack trace from previous location ---
      at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException)
      at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
      at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
      at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService.AddReplacementsWorker(SemanticModel semanticModel, SyntaxNode root, TextSpan span, ConcurrentDictionary`2 replacements, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.VisualBasic.CaseCorrection.VisualBasicCaseCorrectionService.AddReplacements(SemanticModel semanticModel, SyntaxNode root, ImmutableArray`1 spans, ConcurrentDictionary`2 replacements, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.CaseCorrection.AbstractCaseCorrectionService.CaseCorrect(SemanticModel semanticModel, SyntaxNode root, ImmutableArray`1 spans, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.CaseCorrection.AbstractCaseCorrectionService.CaseCorrectAsync(Document document, ImmutableArray`1 spans, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.CaseCorrection.CaseCorrector.CaseCorrectAsync(Document document, SyntaxAnnotation annotation, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.CodeActions.CodeAction.CleanupDocumentAsync(Document document, CodeCleanupOptions options, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.AddImport.AbstractAddImportFeatureService`1.SymbolReference.GetTextChangesAsync(Document document, SyntaxNode contextNode, CodeCleanupOptions options, Boolean hasExistingImport, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.AddImport.AbstractAddImportFeatureService`1.SymbolReference.TryGetFixDataAsync(Document document, SyntaxNode node, CodeCleanupOptions options, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.AddImport.AbstractAddImportFeatureService`1.GetFixesInCurrentProcessAsync(Document document, TextSpan span, String diagnosticId, Int32 maxResults, ISymbolSearchService symbolSearchService, AddImportOptions options, ImmutableArray`1 packageSources, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.AddImport.AbstractAddImportFeatureService`1.GetFixesAsync(Document document, TextSpan span, String diagnosticId, Int32 maxResults, ISymbolSearchService symbolSearchService, AddImportOptions options, ImmutableArray`1 packageSources, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.Remote.RemoteMissingImportDiscoveryService.<>c__DisplayClass3_0.<<GetFixesAsync>b__0>d.MoveNext()
   --- End of stack trace from previous location ---
      at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<>c__DisplayClass7_0`1.<<RunWithSolutionAsync>g__ProcessSolutionAsync|1>d.MoveNext()
   --- End of stack trace from previous location ---
      at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunWithSolutionAsync[T](Checksum solutionChecksum, Func`2 implementation, CancellationToken cancellationToken)
      at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunServiceImplAsync[T](Func`2 implementation, CancellationToken cancellationToken)

Original Comments

Feedback Bot on 5/11/2023, 06:14 PM:

(private comment, text removed)

Feedback Bot on 5/15/2023, 11:09 AM:

(private comment, text removed)


Original Solutions

(no solutions)

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 15, 2023
@AlekseyTs AlekseyTs self-assigned this May 15, 2023
@AlekseyTs AlekseyTs removed the untriaged Issues and PRs which have not yet been triaged by a lead label May 15, 2023
@AlekseyTs
Copy link
Contributor

It looks like support for ref-returns wasn't implemented for With statement:

        <Fact()>
        Public Sub With_Method()
            Dim comp1 = CreateCSharpCompilation(
"public class C<T>
{
#pragma warning disable 0649
    private T _p;
    public ref T GetP()
    {
        return ref _p;
    }
}")
            comp1.VerifyDiagnostics()
            Dim comp2 = CreateVisualBasicCompilation(
                Nothing,
"Module M
    Sub Main()
        Dim o = New C(Of Integer)()
        o.GetP() += 1
        With o.GetP()
            System.Console.Write(.ToString())
            o.GetP() = 2
            System.Console.Write(.ToString())
        End With
    End Sub
End Module",
                referencedCompilations:={comp1},
                compilationOptions:=TestOptions.DebugExe)
            Dim verifier = CompileAndVerify(comp2, expectedOutput:="12")
            verifier.VerifyIL("M.Main",
            <![CDATA[
{
}
]]>)
            verifier.VerifyDiagnostics()
        End Sub

        <Fact()>
        Public Sub With_Property()
            Dim comp1 = CreateCSharpCompilation(
"public class C<T>
{
#pragma warning disable 0649
    private T _p;
    public ref T P
    {
        get { return ref _p; }
    }
}")
            comp1.VerifyDiagnostics()
            Dim comp2 = CreateVisualBasicCompilation(
                Nothing,
"Module M
    Sub Main()
        Dim o = New C(Of Integer)()
        o.P += 1
        With o.P
            System.Console.Write(.ToString())
            o.P = 2
            System.Console.Write(.ToString())
        End With
    End Sub
End Module",
                referencedCompilations:={comp1},
                compilationOptions:=TestOptions.DebugExe)
            Dim verifier = CompileAndVerify(comp2, expectedOutput:="12")
            verifier.VerifyIL("M.Main",
            <![CDATA[
{
}
]]>)
            verifier.VerifyDiagnostics()
        End Sub

        <Fact()>
        Public Sub With_Indexer()
            Dim comp1 = CreateCSharpCompilation(
"public class C<T>
{
#pragma warning disable 0649
    private T _p;
    public ref T this[int i]
    {
        get { return ref _p; }
    }
}")
            comp1.VerifyDiagnostics()
            Dim comp2 = CreateVisualBasicCompilation(
                Nothing,
"Module M
    Sub Main()
        Dim o = New C(Of Integer)()
        o(0) += 1
        With o(0)
            System.Console.Write(.ToString())
            o(0) = 2
            System.Console.Write(.ToString())
        End With
    End Sub
End Module",
                referencedCompilations:={comp1},
                compilationOptions:=TestOptions.DebugExe)
            Dim verifier = CompileAndVerify(comp2, expectedOutput:="12")
            verifier.VerifyIL("M.Main",
            <![CDATA[
{
}
]]>)
            verifier.VerifyDiagnostics()
        End Sub

Observed:

 Microsoft.CodeAnalysis.VisualBasic.UnitTests.CodeGenRefReturnTests.With_Method [FAIL]
   System.InvalidOperationException : Unexpected value 'Call' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind'
Microsoft.CodeAnalysis.VisualBasic.UnitTests.CodeGenRefReturnTests.With_Property [FAIL]
  System.InvalidOperationException : Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind'
Microsoft.CodeAnalysis.VisualBasic.UnitTests.CodeGenRefReturnTests.With_Indexer [FAIL]
  System.InvalidOperationException : Unexpected value 'PropertyAccess' of type 'Microsoft.CodeAnalysis.VisualBasic.BoundKind'

AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue May 16, 2023
TymurGubayev pushed a commit to TymurGubayev/CodeConverter that referenced this issue Dec 8, 2023
Roslyn 4.7.0/MsBuild 17.7 fixed an issue with `With ByRef` (dotnet/roslyn#68194)
GrahamTheCoder added a commit to TymurGubayev/CodeConverter that referenced this issue Jul 20, 2024
Require roslyn , >=4.8.0 for ref return fix: dotnet/roslyn#68194
Require roslyn <= ~4.1.0 or >=4.11.0 for span classification fix: dotnet/roslyn#63921
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants