Ejemplo n.º 1
0
            private void AppendMethodTypeParameterFromConstraint(SortedDictionary <int, ITypeParameterSymbol> sortedMap)
            {
                var typeParametersInConstraint = new List <ITypeParameterSymbol>();

                // collect all type parameter appears in constraint
                foreach (var typeParameter in sortedMap.Values)
                {
                    var constraintTypes = typeParameter.ConstraintTypes;
                    if (constraintTypes.IsDefaultOrEmpty)
                    {
                        continue;
                    }

                    foreach (var type in constraintTypes)
                    {
                        // constraint itself is type parameter
                        typeParametersInConstraint.AddRange(TypeParameterCollector.Collect(type));
                    }
                }

                // pick up only valid type parameter and add them to the map
                foreach (var typeParameter in typeParametersInConstraint)
                {
                    AddTypeParameterToMap(typeParameter, sortedMap);
                }
            }
Ejemplo n.º 2
0
            private void AppendMethodTypeVariableFromDataFlowAnalysis(
                SemanticModel model,
                IDictionary <ISymbol, VariableInfo> variableInfoMap,
                IDictionary <int, ITypeParameterSymbol> sortedMap)
            {
                foreach (var symbol in variableInfoMap.Keys)
                {
                    switch (symbol)
                    {
                    case IParameterSymbol parameter:
                        AddTypeParametersToMap(TypeParameterCollector.Collect(parameter.Type), sortedMap);
                        continue;

                    case ILocalSymbol local:
                        AddTypeParametersToMap(TypeParameterCollector.Collect(local.Type), sortedMap);
                        continue;

                    case IRangeVariableSymbol rangeVariable:
                    {
                        var type = GetRangeVariableType(model, rangeVariable);
                        AddTypeParametersToMap(TypeParameterCollector.Collect(type), sortedMap);
                        continue;
                    }
                    }

                    Contract.Fail(FeaturesResources.Unknown_symbol_kind);
                }
            }
            private void AppendMethodTypeVariableFromDataFlowAnalysis(
                SemanticModel model,
                IDictionary <ISymbol, VariableInfo> variableInfoMap,
                IDictionary <int, ITypeParameterSymbol> sortedMap)
            {
                foreach (var symbol in variableInfoMap.Keys)
                {
                    var parameter = symbol as IParameterSymbol;
                    if (parameter != null)
                    {
                        AddTypeParametersToMap(TypeParameterCollector.Collect(parameter.Type), sortedMap);
                        continue;
                    }

                    var local = symbol as ILocalSymbol;
                    if (local != null)
                    {
                        AddTypeParametersToMap(TypeParameterCollector.Collect(local.Type), sortedMap);
                        continue;
                    }

                    var rangeVariable = symbol as IRangeVariableSymbol;
                    if (rangeVariable != null)
                    {
                        var type = GetRangeVariableType(model, rangeVariable);
                        AddTypeParametersToMap(TypeParameterCollector.Collect(type), sortedMap);
                        continue;
                    }

                    //Contract.Fail(FeaturesResources.UnknownSymbolKind);
                }
            }
Ejemplo n.º 4
0
            private void AppendMethodTypeVariableFromDataFlowAnalysis(
                SemanticModel model,
                IDictionary <ISymbol, VariableInfo> variableInfoMap,
                IDictionary <int, ITypeParameterSymbol> sortedMap)
            {
                foreach (var symbol in variableInfoMap.Keys)
                {
                    switch (symbol)
                    {
                    case IParameterSymbol parameter:
                        AddTypeParametersToMap(TypeParameterCollector.Collect(parameter.Type), sortedMap);
                        continue;

                    case ILocalSymbol local:
                        AddTypeParametersToMap(TypeParameterCollector.Collect(local.Type), sortedMap);
                        continue;

                    case IRangeVariableSymbol rangeVariable:
                        var type = GetRangeVariableType(model, rangeVariable);
                        AddTypeParametersToMap(TypeParameterCollector.Collect(type), sortedMap);
                        continue;

                    default:
                        throw ExceptionUtilities.UnexpectedValue(symbol);
                    }
                }
            }
Ejemplo n.º 5
0
            private IEnumerable <ITypeParameterSymbol> GetMethodTypeParametersInDeclaration(ITypeSymbol returnType, SortedDictionary <int, ITypeParameterSymbol> sortedMap)
            {
                // add return type to the map
                AddTypeParametersToMap(TypeParameterCollector.Collect(returnType), sortedMap);

                AppendMethodTypeParameterFromConstraint(sortedMap);

                return(sortedMap.Values.ToList());
            }
Ejemplo n.º 6
0
        protected override async Task <OperationStatus> CheckTypeAsync(
            Document document,
            SyntaxNode contextNode,
            Location location,
            ITypeSymbol type,
            CancellationToken cancellationToken
            )
        {
            Contract.ThrowIfNull(type);

            // this happens when there is no return type
            if (type.SpecialType == SpecialType.System_Void)
            {
                return(OperationStatus.Succeeded);
            }

            if (type.TypeKind == TypeKind.Error || type.TypeKind == TypeKind.Unknown)
            {
                return(OperationStatus.ErrorOrUnknownType);
            }

            // if it is type parameter, make sure we are getting same type parameter
            var semanticModel = await document
                                .GetSemanticModelAsync(cancellationToken)
                                .ConfigureAwait(false);

            foreach (var typeParameter in TypeParameterCollector.Collect(type))
            {
                var typeName    = SyntaxFactory.ParseTypeName(typeParameter.Name);
                var currentType =
                    semanticModel.GetSpeculativeTypeInfo(
                        contextNode.SpanStart,
                        typeName,
                        SpeculativeBindingOption.BindAsTypeOrNamespace
                        ).Type;
                if (
                    currentType == null ||
                    !SymbolEqualityComparer.Default.Equals(currentType, typeParameter)
                    )
                {
                    return(new OperationStatus(
                               OperationStatusFlag.BestEffort,
                               string.Format(
                                   FeaturesResources.Type_parameter_0_is_hidden_by_another_type_parameter_1,
                                   typeParameter.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
                                   currentType == null
                              ? string.Empty
                              : currentType.ToDisplayString(
                                       SymbolDisplayFormat.FullyQualifiedFormat
                                       )
                                   )
                               ));
                }
            }

            return(OperationStatus.Succeeded);
        }