Skip to content
New issue

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

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

Already on GitHub? # to your account

Error when opening Intellisense suggestion to resolve CA2012 #66726

Closed
Nick-Stanton opened this issue Feb 6, 2023 · 2 comments · Fixed by #75644
Closed

Error when opening Intellisense suggestion to resolve CA2012 #66726

Nick-Stanton opened this issue Feb 6, 2023 · 2 comments · Fixed by #75644
Assignees
Milestone

Comments

@Nick-Stanton
Copy link
Member

Version Used:
.NET 7.0.2
VS 17.4.4

Steps to Reproduce:
Minimal Repro - https://github.com/Nick-Stanton/ReproRoslynError

  1. Navigate to ReproComponent.razor.cs
  2. There should be a CA2012 on line 11
  3. Ctrl+. or R-click -> Quick Actions and Refactorings...

Diagnostic Id: CA2012

Expected Behavior: CSharpAddAwaitCodeRefactoringProvider does not encounter an error.

Actual Behavior:
Visual Studio alerts that CSharpAddAwaitCodeRefactoringProvider has encountered an error with the below stack trace.

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.FindConditionalAccessNodeForBinding(CSharpSyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.Binder.GetReceiverForConditionalBinding(ExpressionSyntax binding,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMemberBindingExpression(MemberBindingExpressionSyntax node,Boolean invoked,Boolean indexed,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpression(InvocationExpressionSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindAwait(AwaitExpressionSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindConditionalAccessExpression(ConditionalAccessExpressionSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindAssignment(AssignmentExpressionSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node,BindingDiagnosticBag diagnostics,Boolean invoked,Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(CSharpSyntaxNode node,ExpressionSyntax syntax,Boolean allowsAnyExpression,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlockParts(BlockSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlock(BlockSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindPossibleEmbeddedStatement(StatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindIfStatement(IfStatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlockParts(BlockSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlock(BlockSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodBody(CSharpSyntaxNode declaration,BlockSyntax blockBody,ArrowExpressionClauseSyntax expressionBody,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodBody(CSharpSyntaxNode syntax,BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.EnsureNullabilityAnalysisPerformedIfNecessary()
   at Microsoft.CodeAnalysis.CSharp.MethodBodySemanticModel.TryGetSpeculativeSemanticModelCore(SyntaxTreeSemanticModel parentModel,Int32 position,StatementSyntax statement,SemanticModel& speculativeModel)
   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.TryGetSpeculativeSemanticModelCore(SyntaxTreeSemanticModel parentModel,Int32 position,StatementSyntax statement,SemanticModel& speculativeModel)
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.TryGetSpeculativeSemanticModel(Int32 position,StatementSyntax statement,SemanticModel& speculativeModel)
   at Microsoft.CodeAnalysis.CSharp.Utilities.SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(SyntaxNode nodeToSpeculate,SemanticModel semanticModel,Int32 position,Boolean isInNamespaceOrTypeContext)
   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.<>c__DisplayClass15_1.<ReduceAsync>b__0[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.ReduceAsync[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.ReduceCoreAsync[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Simplification.AbstractSimplificationService`3.ReduceAsync[TExpressionSyntax,TStatementSyntax,TCrefSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.CleanupDocumentAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessChangesAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessChangesAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.PostProcessAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 6, 2023
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 13, 2023
@jaredpar jaredpar added this to the 17.6 milestone Feb 13, 2023
@jaredpar jaredpar modified the milestones: 17.6, 17.7 Mar 20, 2023
@jcouv jcouv modified the milestones: 17.7, 17.8 Jul 27, 2023
@jcouv
Copy link
Member

jcouv commented Aug 17, 2023

Moving to Area-IDE. The compiler (GetSymbolInfo API) is given nonsensical syntax node. There's no expectation of proper behavior in such case.

Here's the tree I observed:

SimpleAssignmentExpression
├─IdentifierName "_"
├─EqualsToken "="
└─ConditionalAccessExpression
  ├─IdentifierName "c"
  ├─QuestionToken "?"
  └─ParenthesizedExpression
    ├─OpenParenToken "("
    ├─AwaitExpression
    │ ├─AwaitKeyword "await"
    │ └─ParenthesizedExpression
    │   ├─OpenParenToken "("
    │   ├─InvocationExpression
    │   │ ├─MemberBindingExpression
    │   │ │ ├─DotToken "."
    │   │ │ └─IdentifierName "MyIntMethodAsync"
    │   │ └─ArgumentList
    │   │   ├─OpenParenToken "("
    │   │   └─CloseParenToken ")"
    │   └─CloseParenToken ")"
    └─CloseParenToken ")"

For repro, add the following test to AddAwaitTests:


        [Fact]
        public async Task TODO2()
        {
            // crashes on assertion
            await TestInRegularAndScriptAsync(
                """
                using System.Threading.Tasks;

                class TestClass
                {
                    private async Task MyTestMethod1Async(TestClass c)
                    {
                        _ = c?.[|MyIntMethodAsync()|];
                    }

                    private Task<int> MyIntMethodAsync()
                    {
                        return Task.FromResult(result: 1);
                    }
                }
                """,
                """
                using System.Threading.Tasks;

                class TestClass
                {
                    private async Task MyTestMethod1Async()
                    {
                        int myInt = await MyIntMethodAsync();
                    }

                    private Task<int> MyIntMethodAsync()
                    {
                        return Task.FromResult(result: 1);
                    }
                }
                """);
        }

@jcouv jcouv added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead and removed Area-Compilers labels Aug 17, 2023
@jcouv jcouv removed their assignment Aug 17, 2023
@jcouv jcouv removed this from the 17.8 milestone Aug 17, 2023
@jasonmalinowski jasonmalinowski removed the untriaged Issues and PRs which have not yet been triaged by a lead label Oct 10, 2023
@jasonmalinowski jasonmalinowski added this to the 17.8 milestone Oct 10, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants