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.IndexOutOfRangeException when binding #43223

Open
MidasLamb opened this issue Apr 9, 2020 · 4 comments
Open

System.IndexOutOfRangeException when binding #43223

MidasLamb opened this issue Apr 9, 2020 · 4 comments

Comments

@MidasLamb
Copy link

Analyzer package

Microsoft.CodeAnalysis.CSharp

Package Version

3.5.0

Repro steps

I have an analyzer project with multiple analyzers and codefixes. Up untill now the Test helpers generated when creating the project worked fine. With the latest codefix I created however, some error from deep down get's thrown up:
image

Stack trace:

Test Name:	TestFixForEmptyAsyncFunctionWithNoReturnType
Test FullName:	Analyzer.Test.MissingImplementationTests.TestFixForEmptyAsyncFunctionWithNoReturnType
Test Source:	Analyzer.Test\MissingImplementationTests.cs : line 190
Test Outcome:	Failed
Test Duration:	0:01:08,5549479

Result StackTrace:	
at Microsoft.CodeAnalysis.CSharp.Binder.ConvertAndBindArrayInitialization(DiagnosticBag diagnostics, InitializerExpressionSyntax node, ArrayTypeSymbol type, Nullable`1[] knownSizes, Int32 dimension, ImmutableArray`1 boundInitExpr, Int32& boundInitExprIndex) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 3204
   at Microsoft.CodeAnalysis.CSharp.Binder.BindArrayCreationWithInitializer(DiagnosticBag diagnostics, ExpressionSyntax creationSyntax, InitializerExpressionSyntax initSyntax, ArrayTypeSymbol type, ImmutableArray`1 sizes, ImmutableArray`1 boundInitExprOpt, Boolean hasErrors) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 3385
   at Microsoft.CodeAnalysis.CSharp.Binder.BindArrayCreationExpression(ArrayCreationExpressionSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 3020
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 482
   at Microsoft.CodeAnalysis.CSharp.Binder.BindArgumentExpression(DiagnosticBag diagnostics, ExpressionSyntax argumentExpression, RefKind refKind, Boolean allowArglist) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 2812
   at Microsoft.CodeAnalysis.CSharp.Binder.BindArgumentAndName(AnalyzedArguments result, DiagnosticBag diagnostics, Boolean& hadError, Boolean& hadLangVersionError, ArgumentSyntax argumentSyntax, Boolean allowArglist, Boolean isDelegateCreation) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 2547
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpression(InvocationExpressionSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs:line 151
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 478
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 423
   at Microsoft.CodeAnalysis.CSharp.Binder.BindAwait(AwaitExpressionSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Await.cs:line 20
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 645
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node, DiagnosticBag diagnostics, Boolean invoked, Boolean indexed) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs:line 423
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(CSharpSyntaxNode node, ExpressionSyntax syntax, Boolean allowsAnyExpression, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 615
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(ExpressionStatementSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 608
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 56
   at Microsoft.CodeAnalysis.CSharp.Binder.BindBlockParts(BlockSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 1689
   at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 47
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodBody(CSharpSyntaxNode declaration, BlockSyntax blockBody, ArrowExpressionClauseSyntax expressionBody, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 3239
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodBody(CSharpSyntaxNode syntax, DiagnosticBag diagnostics) in /_/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs:line 3196
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.BindMethodBody(MethodSymbol method, TypeCompilationState compilationState, DiagnosticBag diagnostics, ImportChain& importChain, Boolean& originalBodyNested, InitialState& forSemanticModel) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 1681
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(MethodSymbol methodSymbol, Int32 methodOrdinal, ProcessedFieldInitializers& processedInitializers, SynthesizedSubmissionFields previousSubmissionFields, TypeCompilationState compilationState) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 985
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(NamedTypeSymbol containingType) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 514
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.VisitNamedType(NamedTypeSymbol symbol, TypeCompilationState arg) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 392
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamespace(NamespaceSymbol symbol) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 366
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.VisitNamespace(NamespaceSymbol symbol, TypeCompilationState arg) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 346
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamespace(NamespaceSymbol symbol) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 366
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.VisitNamespace(NamespaceSymbol symbol, TypeCompilationState arg) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 346
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamespace(NamespaceSymbol symbol) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 366
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethodBodies(CSharpCompilation compilation, PEModuleBuilder moduleBeingBuiltOpt, Boolean emittingPdb, Boolean emitTestCoverageData, Boolean hasDeclarationErrors, DiagnosticBag diagnostics, Predicate`1 filterOpt, CancellationToken cancellationToken) in /_/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs:line 155
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetDiagnosticsForAllMethodBodies(DiagnosticBag diagnostics, CancellationToken cancellationToken) in /_/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs:line 2445
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetDiagnostics(CompilationStage stage, Boolean includeEarlierStages, DiagnosticBag diagnostics, CancellationToken cancellationToken) in /_/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs:line 2405
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetDiagnostics(CancellationToken cancellationToken) in /_/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs:line 2306
   at Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.ComputeAnalyzerDiagnosticsWithoutStateTrackingAsync(CancellationToken cancellationToken) in /_/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs:line 414
   at Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerDiagnosticsWithoutStateTrackingAsync(ImmutableArray`1 analyzers, CancellationToken cancellationToken) in /_/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs:line 384
   at Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(CancellationToken cancellationToken) in /_/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs:line 279
--- End of inner exception stack trace ---
    at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at TestHelper.DiagnosticVerifier.GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) in Analyzer.Test\Helpers\DiagnosticVerifier.Helper.cs:line 61
   at TestHelper.CodeFixVerifier.VerifyFix(String language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, String oldSource, String newSource, Nullable`1 codeFixIndex, Boolean allowNewCompilerDiagnostics) in Analyzer.Test\Verifiers\CodeFixVerifier.cs:line 99
   at TestHelper.CodeFixVerifier.VerifyCSharpFix(String oldSource, String newSource, Nullable`1 codeFixIndex, Boolean allowNewCompilerDiagnostics) in Analyzer.Test\Verifiers\CodeFixVerifier.cs:line 46
   at Analyzer.Test.MissingImplementationTests.TestFixForEmptyAsyncFunctionWithNoReturnType() in Analyzer.Test\MissingImplementationTests.cs:line 252
Result Message:	
Test method Analyzer.Test.MissingImplementationTests.TestFixForEmptyAsyncFunctionWithNoReturnType threw exception: 
System.AggregateException: One or more errors occurred. (Index was outside the bounds of the array.) ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.

I don't know what is going wrong. I've created a test which takes the outcome of the codefix generated in the above test that fails, and runs the Analyzer on that one, that one succeeds without any problem. Running the codefix in an experimental instance of Visual Studio also seems to work fine.

Expected behavior

CompilationWithAnalyzers.GetAnalyzerDiagnosticsAsync() returns n diagnostics and does not throw an exception.

Actual behavior

CompilationWithAnalyzers.GetAnalyzerDiagnosticsAsync() throws an exception of IndexOutOfRangeException

@333fred
Copy link
Member

333fred commented Apr 9, 2020

This actually looks like a compiler problem, not an analyzer problem. Can you share the test that is hitting this?

@333fred 333fred transferred this issue from dotnet/roslyn-analyzers Apr 9, 2020
@333fred 333fred changed the title DiagnosticVerifier.Helper throws IndexOutOfRangeException System.IndexOutOfRangeException when binding Apr 9, 2020
@MidasLamb
Copy link
Author

@333fred Sorry for the mixup.
I'm not sure if i'm allowed to share the test as i'm not sure whether or not it contains anything confidential (I'm not well versed in said area so I tend to lean on the cautionary side).
I'll try to create a standalone solution which can replicate this. I'll give you the big lines already:

The analyzer does the following:

  • Check if certain functions only contain throw new NotImplementedException(); or is empty, if so generate a warning

The codefix:

  • Generate a default implementation based on some stuff from the method signature such as parameters etc. and replace the empty function body (or the body with only throw new NotImplementedException();) with that default implementation

@333fred
Copy link
Member

333fred commented Apr 9, 2020

Is it something that you'd be able to share privately? Looking at your stack trace is telling me that whatever syntax you're testing on is actually running into this issue. I'd guess it's not a problem with the analyzer, just the literal text you're calling VerifyAnalyzerDiagnostics on.

@MidasLamb
Copy link
Author

MidasLamb commented Apr 9, 2020

@333fred , I boiled it down a bit and removed everything in this repo.
The offending test goes through the following stages:
input

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
   class TypeName
   {   
      public async Task Test(int a)
      {
      }
    
   }
}

after codefix
Got from debugging and inspecting the returned syntaxroot

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
   class TypeName
   {   
      public async Task Test(int a)
        {
            await TestStaticClass.TestFunction<ISomeInterface>(new object[] { a });
        }
    }
}

after running the codefix it crashes when rerunning the analyzer on the output, but I also have a testcase that runs the analyzer on the output as I show it above, and that functions fine.

In the repo: method call that fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants