/// <summary> /// Substitute types, and return the results without duplicates, preserving the original order. /// Note, all occurrences of 'dynamic' in resulting types will be replaced with 'object'. /// </summary> internal void SubstituteConstraintTypesDistinctWithoutModifiers( TypeParameterSymbol owner, ImmutableArray <TypeWithAnnotations> original, ArrayBuilder <TypeWithAnnotations> result, HashSet <TypeParameterSymbol> ignoreTypesDependentOnTypeParametersOpt) { DynamicTypeEraser dynamicEraser = null; if (original.Length == 0) { return; } else if (original.Length == 1) { var type = original[0]; if (ignoreTypesDependentOnTypeParametersOpt == null || !type.Type.ContainsTypeParameters(ignoreTypesDependentOnTypeParametersOpt)) { result.Add(substituteConstraintType(type)); } } else { var map = PooledDictionary <TypeSymbol, int> .GetInstance(); foreach (var type in original) { if (ignoreTypesDependentOnTypeParametersOpt == null || !type.Type.ContainsTypeParameters(ignoreTypesDependentOnTypeParametersOpt)) { var substituted = substituteConstraintType(type); if (!map.TryGetValue(substituted.Type, out int mergeWith)) { map.Add(substituted.Type, result.Count); result.Add(substituted); } else { result[mergeWith] = ConstraintsHelper.ConstraintWithMostSignificantNullability(result[mergeWith], substituted); } } } map.Free(); } TypeWithAnnotations substituteConstraintType(TypeWithAnnotations type) { if (dynamicEraser == null) { dynamicEraser = new DynamicTypeEraser(owner.ContainingAssembly.CorLibrary.GetSpecialType(SpecialType.System_Object)); } TypeWithAnnotations substituted = SubstituteType(type); return(substituted.WithTypeAndModifiers(dynamicEraser.EraseDynamic(substituted.Type), substituted.CustomModifiers)); } }
protected static void CopyEventCustomModifiers(EventSymbol eventWithCustomModifiers, ref TypeWithAnnotations type, AssemblySymbol containingAssembly) { Debug.Assert((object)eventWithCustomModifiers != null); TypeSymbol overriddenEventType = eventWithCustomModifiers.Type; // We do an extra check before copying the type to handle the case where the overriding // event (incorrectly) has a different type than the overridden event. In such cases, // we want to retain the original (incorrect) type to avoid hiding the type given in source. if (type.Type.Equals(overriddenEventType, TypeCompareKind.IgnoreCustomModifiersAndArraySizesAndLowerBounds | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes | TypeCompareKind.IgnoreDynamic)) { type = type.WithTypeAndModifiers(CustomModifierUtils.CopyTypeCustomModifiers(overriddenEventType, type.Type, containingAssembly), eventWithCustomModifiers.TypeWithAnnotations.CustomModifiers); } }
/// <remarks> /// Out params are updated by assignment. If you require thread-safety, pass temps and then /// CompareExchange them back into shared memory. /// </remarks> internal static void CopyMethodCustomModifiers( MethodSymbol sourceMethod, MethodSymbol destinationMethod, out TypeWithAnnotations returnType, out ImmutableArray <CustomModifier> customModifiers, out ImmutableArray <ParameterSymbol> parameters, bool alsoCopyParamsModifier) // Last since always named. { Debug.Assert((object)sourceMethod != null); // Assert: none of the method's type parameters have been substituted Debug.Assert((object)sourceMethod == sourceMethod.ConstructedFrom); // For the most part, we will copy custom modifiers by copying types. // The only time when this fails is when the type refers to a type parameter // owned by the overridden method. We need to replace all such references // with (equivalent) type parameters owned by this method. We know that // we can perform this mapping positionally, because the method signatures // have already been compared. MethodSymbol constructedSourceMethod = sourceMethod.ConstructIfGeneric(destinationMethod.TypeArgumentsWithAnnotations); customModifiers = destinationMethod.RefKind != RefKind.None ? constructedSourceMethod.RefCustomModifiers : ImmutableArray <CustomModifier> .Empty; parameters = CopyParameterCustomModifiers(constructedSourceMethod.Parameters, destinationMethod.Parameters, alsoCopyParamsModifier); returnType = destinationMethod.ReturnTypeWithAnnotations; // Default value - in case we don't copy the custom modifiers. TypeSymbol returnTypeSymbol = returnType.Type; var sourceMethodReturnType = constructedSourceMethod.ReturnTypeWithAnnotations; // We do an extra check before copying the return type to handle the case where the overriding // method (incorrectly) has a different return type than the overridden method. In such cases, // we want to retain the original (incorrect) return type to avoid hiding the return type // given in source. TypeSymbol returnTypeWithCustomModifiers = sourceMethodReturnType.Type; if (returnTypeSymbol.Equals(returnTypeWithCustomModifiers, TypeCompareKind.AllIgnoreOptions)) { returnType = returnType.WithTypeAndModifiers(CopyTypeCustomModifiers(returnTypeWithCustomModifiers, returnTypeSymbol, destinationMethod.ContainingAssembly), sourceMethodReturnType.CustomModifiers); } }
protected override void MethodChecks(DiagnosticBag diagnostics) { // These values may not be final, but we need to have something set here in the // event that we need to find the overridden accessor. _lazyParameters = ComputeParameters(diagnostics); _lazyReturnType = ComputeReturnType(diagnostics); _lazyRefCustomModifiers = ImmutableArray <CustomModifier> .Empty; if (_explicitInterfaceImplementations.Length > 0) { Debug.Assert(_explicitInterfaceImplementations.Length == 1); MethodSymbol implementedMethod = _explicitInterfaceImplementations[0]; CustomModifierUtils.CopyMethodCustomModifiers(implementedMethod, this, out _lazyReturnType, out _lazyRefCustomModifiers, out _lazyParameters, alsoCopyParamsModifier: false); } else if (this.IsOverride) { // This will cause another call to SourceMethodSymbol.LazyMethodChecks, // but that method already handles reentrancy for exactly this case. MethodSymbol overriddenMethod = this.OverriddenMethod; if ((object)overriddenMethod != null) { CustomModifierUtils.CopyMethodCustomModifiers(overriddenMethod, this, out _lazyReturnType, out _lazyRefCustomModifiers, out _lazyParameters, alsoCopyParamsModifier: true); } } else if (_lazyReturnType.SpecialType != SpecialType.System_Void) { PropertySymbol associatedProperty = _property; var type = associatedProperty.TypeWithAnnotations; _lazyReturnType = _lazyReturnType.WithTypeAndModifiers( CustomModifierUtils.CopyTypeCustomModifiers(type.Type, _lazyReturnType.Type, this.ContainingAssembly), type.CustomModifiers); _lazyRefCustomModifiers = associatedProperty.RefCustomModifiers; } }