)

in src/Compilers/VisualBasic/Portable/Binding/Binder_Invocation.vb [1992:2465]


        )
            Dim candidate As OverloadResolution.Candidate = candidateAnalysisResult.Candidate

            If arguments.IsDefault Then
                arguments = ImmutableArray(Of BoundExpression).Empty
            End If

            Debug.Assert(argumentNames.IsDefaultOrEmpty OrElse (argumentNames.Length > 0 AndAlso argumentNames.Length = arguments.Length))
            Debug.Assert(allowUnexpandedParamArrayForm OrElse allowExpandedParamArrayForm)

            If candidateAnalysisResult.State = VisualBasic.OverloadResolution.CandidateAnalysisResultState.HasUseSiteError OrElse
               candidateAnalysisResult.State = VisualBasic.OverloadResolution.CandidateAnalysisResultState.HasUnsupportedMetadata Then
                If lookupResult <> LookupResultKind.Inaccessible Then
                    Debug.Assert(lookupResult = LookupResultKind.Good)
                    ReportDiagnostic(diagnostics, diagnosticLocation, candidate.UnderlyingSymbol.GetUseSiteErrorInfo())
                End If

                Return
            End If

            
            If Not argumentNames.IsDefault AndAlso argumentNames.Length = 0 Then
                argumentNames = Nothing
            End If

            Dim parameterToArgumentMap As ArrayBuilder(Of Integer) = ArrayBuilder(Of Integer).GetInstance(candidate.ParameterCount, -1)
            Dim paramArrayItems As ArrayBuilder(Of Integer) = ArrayBuilder(Of Integer).GetInstance()

            Try
                
                
                
                
                
                
                

                Dim positionalArguments As Integer = 0
                Dim paramIndex = 0
                Dim someArgumentsBad As Boolean = False
                Dim someParamArrayArgumentsBad As Boolean = False
                Dim seenOutOfPositionNamedArgIndex As Integer = -1

                Dim candidateSymbol As Symbol = candidate.UnderlyingSymbol
                Dim candidateIsExtension As Boolean = candidate.IsExtensionMethod

                For i As Integer = 0 To arguments.Length - 1 Step 1

                    
                    If Not argumentNames.IsDefault AndAlso argumentNames(i) IsNot Nothing Then
                        If Not candidate.TryGetNamedParamIndex(argumentNames(i), paramIndex) Then
                            Exit For
                        End If

                        If paramIndex <> i Then
                            
                            seenOutOfPositionNamedArgIndex = i
                            Exit For
                        End If

                        If paramIndex = candidate.ParameterCount - 1 AndAlso candidate.Parameters(paramIndex).IsParamArray Then
                            Exit For
                        End If

                        Debug.Assert(parameterToArgumentMap(paramIndex) = -1)
                    End If

                    If paramIndex = candidate.ParameterCount Then
                        If Not someArgumentsBad Then
                            If Not includeMethodNameInErrorMessages Then
                                ReportDiagnostic(diagnostics, arguments(i).Syntax, ERRID.ERR_TooManyArgs)
                            ElseIf candidateIsExtension Then
                                ReportDiagnostic(diagnostics, arguments(i).Syntax,
                                                 ERRID.ERR_TooManyArgs2,
                                                 candidateSymbol, candidateSymbol.ContainingType)
                            Else
                                ReportDiagnostic(diagnostics, arguments(i).Syntax,
                                                 ERRID.ERR_TooManyArgs1, If(representCandidateInDiagnosticsOpt, candidateSymbol))
                            End If

                            someArgumentsBad = True
                        End If

                    ElseIf paramIndex = candidate.ParameterCount - 1 AndAlso
                           candidate.Parameters(paramIndex).IsParamArray Then

                        
                        While i < arguments.Length

                            If Not argumentNames.IsDefault AndAlso argumentNames(i) IsNot Nothing Then
                                
                                Continue For
                            End If

                            If arguments(i).Kind = BoundKind.OmittedArgument Then
                                ReportDiagnostic(diagnostics, arguments(i).Syntax, ERRID.ERR_OmittedParamArrayArgument)
                                someParamArrayArgumentsBad = True
                            Else
                                paramArrayItems.Add(i)
                            End If

                            positionalArguments += 1
                            i += 1
                        End While

                        Exit For

                    Else
                        parameterToArgumentMap(paramIndex) = i
                        paramIndex += 1
                    End If

                    positionalArguments += 1
                Next

                Dim skippedSomeArguments As Boolean = False

                
                
                
                
                
                For i As Integer = positionalArguments To arguments.Length - 1 Step 1

                    Debug.Assert(argumentNames(i) Is Nothing OrElse argumentNames(i).Length > 0)

                    If argumentNames(i) Is Nothing Then
                        
                        If Not someArgumentsBad Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(seenOutOfPositionNamedArgIndex).Syntax),
                                         ERRID.ERR_BadNonTrailingNamedArgument, argumentNames(seenOutOfPositionNamedArgIndex))
                        End If
                        Return
                    End If

                    If Not candidate.TryGetNamedParamIndex(argumentNames(i), paramIndex) Then
                        
                        
                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax), ERRID.ERR_NamedParamNotFound1, argumentNames(i))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedParamNotFound3, argumentNames(i),
                                             candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedParamNotFound2, argumentNames(i), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If

                        someArgumentsBad = True
                        Continue For
                    End If

                    If paramIndex = candidate.ParameterCount - 1 AndAlso
                        candidate.Parameters(paramIndex).IsParamArray Then
                        
                        ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax), ERRID.ERR_NamedParamArrayArgument)
                        someArgumentsBad = True
                        Continue For
                    End If

                    If parameterToArgumentMap(paramIndex) <> -1 AndAlso arguments(parameterToArgumentMap(paramIndex)).Kind <> BoundKind.OmittedArgument Then
                        
                        
                        
                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax), ERRID.ERR_NamedArgUsedTwice1, argumentNames(i))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedArgUsedTwice3, argumentNames(i),
                                             candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedArgUsedTwice2, argumentNames(i), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If

                        someArgumentsBad = True
                        Continue For
                    End If

                    
                    
                    If paramIndex < positionalArguments Then
                        
                        
                        
                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax), ERRID.ERR_NamedArgAlsoOmitted1, argumentNames(i))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedArgAlsoOmitted3, argumentNames(i),
                                             candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, GetNamedArgumentIdentifier(arguments(i).Syntax),
                                             ERRID.ERR_NamedArgAlsoOmitted2, argumentNames(i), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If

                        someArgumentsBad = True
                    End If

                    parameterToArgumentMap(paramIndex) = i
                Next

                
                If candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt IsNot Nothing Then
                    diagnostics.AddRange(candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt)
                End If

                If candidate.IsGeneric AndAlso candidateAnalysisResult.State = OverloadResolution.CandidateAnalysisResultState.TypeInferenceFailed Then
                    
                    
                    If delegateSymbol IsNot Nothing Then
                        ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_DelegateBindingTypeInferenceFails)
                        Return
                    End If

                    If Not candidateAnalysisResult.SomeInferenceFailed Then

                        Dim reportedAnError As Boolean = False

                        For i As Integer = 0 To candidate.Arity - 1 Step 1
                            If candidateAnalysisResult.NotInferredTypeArguments(i) Then
                                If Not includeMethodNameInErrorMessages Then
                                    ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_UnboundTypeParam1, candidate.TypeParameters(i))
                                ElseIf candidateIsExtension Then
                                    ReportDiagnostic(diagnostics, diagnosticLocation,
                                                     ERRID.ERR_UnboundTypeParam3, candidate.TypeParameters(i),
                                                     candidateSymbol, candidateSymbol.ContainingType)
                                Else
                                    ReportDiagnostic(diagnostics, diagnosticLocation,
                                                     ERRID.ERR_UnboundTypeParam2, candidate.TypeParameters(i), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                                End If

                                reportedAnError = True
                            End If
                        Next

                        If reportedAnError Then
                            Return
                        End If
                    End If

                    Dim inferenceErrorReasons As InferenceErrorReasons = candidateAnalysisResult.InferenceErrorReasons

                    If (inferenceErrorReasons And InferenceErrorReasons.Ambiguous) <> 0 Then
                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitAmbiguous1, ERRID.ERR_TypeInferenceFailureAmbiguous1))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitAmbiguous3, ERRID.ERR_TypeInferenceFailureAmbiguous3), candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitAmbiguous2, ERRID.ERR_TypeInferenceFailureAmbiguous2), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If
                    ElseIf (inferenceErrorReasons And InferenceErrorReasons.NoBest) <> 0 Then
                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitNoBest1, ERRID.ERR_TypeInferenceFailureNoBest1))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitNoBest3, ERRID.ERR_TypeInferenceFailureNoBest3), candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicitNoBest2, ERRID.ERR_TypeInferenceFailureNoBest2), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If
                    Else
                        If candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt IsNot Nothing AndAlso
                           candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt.HasAnyResolvedErrors Then
                            
                            Return
                        End If

                        If Not includeMethodNameInErrorMessages Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicit1, ERRID.ERR_TypeInferenceFailure1))
                        ElseIf candidateIsExtension Then
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicit3, ERRID.ERR_TypeInferenceFailure3), candidateSymbol, candidateSymbol.ContainingType)
                        Else
                            ReportDiagnostic(diagnostics, diagnosticLocation, If(queryMode, ERRID.ERR_TypeInferenceFailureNoExplicit2, ERRID.ERR_TypeInferenceFailure2), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                        End If
                    End If

                    Return
                End If

                
                If candidateAnalysisResult.State = OverloadResolution.CandidateAnalysisResultState.GenericConstraintsViolated Then
                    Debug.Assert(candidate.IsGeneric)
                    Debug.Assert(candidate.UnderlyingSymbol.Kind = SymbolKind.Method)

                    Dim method = DirectCast(candidate.UnderlyingSymbol, MethodSymbol)
                    
                    
                    Dim succeeded = method.CheckConstraints(diagnosticLocation, diagnostics)
                    Debug.Assert(Not succeeded)
                    Return
                End If

                If candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt IsNot Nothing AndAlso
                   candidateAnalysisResult.TypeArgumentInferenceDiagnosticsOpt.HasAnyErrors Then
                    Return
                End If

                
                

                Dim argIndex As Integer
                Dim candidateIsAProperty As Boolean = (candidateSymbol.Kind = SymbolKind.Property)

                For paramIndex = 0 To candidate.ParameterCount - 1 Step 1

                    Dim param As ParameterSymbol = candidate.Parameters(paramIndex)
                    Dim isByRef As Boolean = param.IsByRef
                    Dim targetType As TypeSymbol = param.Type

                    Dim argument As BoundExpression = Nothing

                    If param.IsParamArray AndAlso paramIndex = candidate.ParameterCount - 1 Then

                        If targetType.Kind <> SymbolKind.ArrayType Then

                            If targetType.Kind <> SymbolKind.ErrorType Then
                                
                                ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_ParamArrayWrongType)
                            End If

                            someArgumentsBad = True
                            Continue For

                        ElseIf someParamArrayArgumentsBad Then
                            Continue For
                        End If


                        If paramArrayItems.Count = 1 Then
                            Dim paramArrayArgument = arguments(paramArrayItems(0))

                            
                            
                            
                            Dim arrayConversion As KeyValuePair(Of ConversionKind, MethodSymbol) = Nothing
                            If allowUnexpandedParamArrayForm AndAlso
                                Not (Not paramArrayArgument.HasErrors AndAlso
                                    OverloadResolution.CanPassToParamArray(paramArrayArgument, targetType, arrayConversion, Me, Nothing)) Then
                                allowUnexpandedParamArrayForm = False
                            End If

                            
                            
                            If allowExpandedParamArrayForm AndAlso
                                paramArrayArgument.IsNothingLiteral() Then
                                allowExpandedParamArrayForm = False
                            End If

                        Else
                            
                            If Not allowExpandedParamArrayForm Then

                                If paramArrayItems.Count = 0 Then
                                    If Not includeMethodNameInErrorMessages Then
                                        ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_OmittedArgument1, CustomSymbolDisplayFormatter.ShortErrorName(param))
                                    ElseIf candidateIsExtension Then
                                        ReportDiagnostic(diagnostics, diagnosticLocation,
                                                         ERRID.ERR_OmittedArgument3, CustomSymbolDisplayFormatter.ShortErrorName(param),
                                                         candidateSymbol, candidateSymbol.ContainingType)
                                    Else
                                        ReportDiagnostic(diagnostics, diagnosticLocation,
                                                         ERRID.ERR_OmittedArgument2, CustomSymbolDisplayFormatter.ShortErrorName(param),
                                                         If(representCandidateInDiagnosticsOpt, candidateSymbol))
                                    End If
                                Else
                                    If Not includeMethodNameInErrorMessages Then
                                        ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_TooManyArgs)
                                    ElseIf candidateIsExtension Then
                                        ReportDiagnostic(diagnostics, diagnosticLocation,
                                                           ERRID.ERR_TooManyArgs2, candidateSymbol, candidateSymbol.ContainingType)
                                    Else
                                        ReportDiagnostic(diagnostics, diagnosticLocation,
                                                 ERRID.ERR_TooManyArgs1, If(representCandidateInDiagnosticsOpt, candidateSymbol))
                                    End If
                                End If

                                someArgumentsBad = True
                                Continue For
                            End If

                            allowUnexpandedParamArrayForm = False
                        End If

                        If allowUnexpandedParamArrayForm Then
                            argument = arguments(paramArrayItems(0))
                            ReportByValConversionErrors(param, argument, targetType, reportNarrowingConversions, diagnostics)

                        ElseIf allowExpandedParamArrayForm Then
                            Dim arrayType = DirectCast(targetType, ArrayTypeSymbol)

                            If Not arrayType.IsSZArray Then
                                
                                ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_ParamArrayWrongType)
                                someArgumentsBad = True
                                Continue For
                            End If

                            Dim arrayElementType = arrayType.ElementType

                            For i As Integer = 0 To paramArrayItems.Count - 1 Step 1
                                argument = arguments(paramArrayItems(i))
                                ReportByValConversionErrors(param, argument, arrayElementType, reportNarrowingConversions, diagnostics)
                            Next
                        Else
                            Debug.Assert(paramArrayItems.Count = 1)
                            Dim paramArrayArgument = arguments(paramArrayItems(0))
                            ReportDiagnostic(diagnostics, paramArrayArgument.Syntax, ERRID.ERR_ParamArrayArgumentMismatch)
                        End If

                        Continue For
                    End If

                    argIndex = parameterToArgumentMap(paramIndex)
                    argument = If(argIndex = -1, Nothing, arguments(argIndex))

                    
                    
                    If argument Is Nothing OrElse argument.Kind = BoundKind.OmittedArgument Then

                        If argument Is Nothing AndAlso skippedSomeArguments Then
                            someArgumentsBad = True
                            Continue For
                        End If

                        

                        
                        

                        argument = GetArgumentForParameterDefaultValue(param, node, diagnostics, callerInfoOpt)

                        If argument Is Nothing Then
                            If Not includeMethodNameInErrorMessages Then
                                ReportDiagnostic(diagnostics, diagnosticLocation, ERRID.ERR_OmittedArgument1, CustomSymbolDisplayFormatter.ShortErrorName(param))
                            ElseIf candidateIsExtension Then
                                ReportDiagnostic(diagnostics, diagnosticLocation,
                                                 ERRID.ERR_OmittedArgument3, CustomSymbolDisplayFormatter.ShortErrorName(param),
                                                 candidateSymbol, candidateSymbol.ContainingType)
                            Else
                                ReportDiagnostic(diagnostics, diagnosticLocation,
                                                 ERRID.ERR_OmittedArgument2, CustomSymbolDisplayFormatter.ShortErrorName(param), If(representCandidateInDiagnosticsOpt, candidateSymbol))
                            End If

                            someArgumentsBad = True
                            Continue For
                        End If
                    End If

                    Debug.Assert(Not isByRef OrElse param.IsExplicitByRef OrElse targetType.IsStringType())

                    
                    
                    
                    
                    
                    
                    
                    
                    
                    If isByRef AndAlso Not candidateIsAProperty AndAlso
                       (param.IsExplicitByRef OrElse (argument.Type IsNot Nothing AndAlso argument.Type.IsStringType())) Then
                        ReportByRefConversionErrors(candidate, param, argument, targetType, reportNarrowingConversions, diagnostics,
                                                    diagnosticNode:=node, delegateSymbol:=delegateSymbol)
                    Else
                        ReportByValConversionErrors(param, argument, targetType, reportNarrowingConversions, diagnostics,
                                                    diagnosticNode:=node, delegateSymbol:=delegateSymbol)
                    End If
                Next

            Finally
                paramArrayItems.Free()
                parameterToArgumentMap.Free()
            End Try
        End Sub