Remove unused properties from ErrorType (dotnet/corefx#23248)
authorJon Hanna <jon@hackcraft.net>
Tue, 5 Sep 2017 20:25:39 +0000 (21:25 +0100)
committerOmar Tawfik <OmarTawfik@users.noreply.github.com>
Tue, 5 Sep 2017 20:25:39 +0000 (13:25 -0700)
* Remove parent parameter to CreateError and pParentType to GetErrorType

Only ever called with null; the one path that sets it to something else
only being hit if another ErrorType instance had it set to something
else, and so never happening.

* Remove TypeParent from ErrorType

Never not null

* Remove by-parent-type lookup for ErrorType instances.

Never used.

* Remove ErrAppendParentType

If called with an ErrorType calls ErrAppendParentCore which returns immediately if the namespace parent is either null or the rootNS, which is the only two possibilities.

As such it's always an no-op, so remove that call. Since this is the only call existing, remove entirely.

* Remove namespace parameter from LookupError and InsertError

Always the root namespace, so doesn't affect lookup.

* Make HasParent a simple boolean property.

Since the namespace, if set, is always the same one, and not examined beyond whether it is set, it's essentially a flag.

* Determine ErrorType.HasParent by whether nameText is null.

Always false if it's null, true otherwise, so use that as the storage.

* Add explanatory comment.

Commit migrated from https://github.com/dotnet/corefx/commit/10a76752c95853ed74297fa817ff897ad389e3d8

src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Errors/UserStringBuilder.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ImplicitConversion.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/MethodTypeInferrer.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/ErrorType.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/Type.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeFactory.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeManager.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Types/TypeTable.cs

index 339881c..0c9b94e 100644 (file)
@@ -150,35 +150,6 @@ namespace Microsoft.CSharp.RuntimeBinder.Errors
             ErrAppendParentCore(sym.parent, pctx);
         }
 
-        private void ErrAppendParentType(CType pType, SubstContext pctx)
-        {
-            if (pType is ErrorType err)
-            {
-                if (err.HasTypeParent())
-                {
-                    ErrAppendType(err.GetTypeParent(), null);
-                    ErrAppendChar('.');
-                }
-                else
-                {
-                    ErrAppendParentCore(err.GetNSParent(), pctx);
-                }
-            }
-            else if (pType is AggregateType agg)
-            {
-                ErrAppendParentCore(agg.GetOwningAggregate(), pctx);
-            }
-            else
-            {
-                var part = pType.GetBaseOrParameterOrElementType();
-                if (part != null)
-                {
-                    ErrAppendType(part, null);
-                    ErrAppendChar('.');
-                }
-            }
-        }
-
         private void ErrAppendParentCore(Symbol parent, SubstContext pctx)
         {
             if (null == parent)
@@ -533,10 +504,9 @@ namespace Microsoft.CSharp.RuntimeBinder.Errors
 
                 case TypeKind.TK_ErrorType:
                     ErrorType err = (ErrorType)pType;
-                    if (err.HasParent())
+                    if (err.HasParent)
                     {
                         Debug.Assert(err.nameText != null && err.typeArgs != null);
-                        ErrAppendParentType(pType, pctx);
                         ErrAppendName(err.nameText);
                         ErrAppendTypeParameters(err.typeArgs, pctx, true);
                     }
index 363d8fa..ff0006d 100644 (file)
@@ -93,7 +93,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
                 switch (_typeDest.GetTypeKind())
                 {
                     case TypeKind.TK_ErrorType:
-                        Debug.Assert(((ErrorType)_typeDest).HasParent());
+                        Debug.Assert(((ErrorType)_typeDest).HasParent);
                         if (_typeSrc != _typeDest)
                         {
                             return false;
index 8018189..016c112 100644 (file)
@@ -195,8 +195,6 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
                 }
 
                 _pFixedResults[iParam] = GetTypeManager().GetErrorType(
-                                        null/*pParentType*/,
-                                        null,
                                         ((TypeParameterType)_pMethodTypeParameters[iParam]).GetName(),
                                         BSYMMGR.EmptyTypeArray());
             }
index 0a68e5e..7376f4c 100644 (file)
@@ -18,17 +18,11 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
         public Name nameText;
         public TypeArray typeArgs;
 
-        public bool HasParent() { return _pParentType != null || _pParentNS != null; }
-
-        public bool HasTypeParent() { return _pParentType != null; }
-        public CType GetTypeParent() { return _pParentType; }
-        public void SetTypeParent(CType pType) { _pParentType = pType; }
-
-        public bool HasNSParent() { return _pParentNS != null; }
-        public AssemblyQualifiedNamespaceSymbol GetNSParent() { return _pParentNS; }
-        public void SetNSParent(AssemblyQualifiedNamespaceSymbol pNS) { _pParentNS = pNS; }
-
-        private CType _pParentType;
-        private AssemblyQualifiedNamespaceSymbol _pParentNS;
+        // ErrorTypes are always either the per-TypeManager singleton ErrorType
+        // that has a null nameText and no namespace parent, or else have a
+        // non-null nameText and have the root namespace as the namespace parent,
+        // so checking that nameText isn't null is equivalent to checking if the
+        // type has a parent.
+        public bool HasParent => nameText != null;
     }
 }
index d182dd9..8a83c9a 100644 (file)
@@ -179,7 +179,8 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
         public void InitFromParent()
         {
             Debug.Assert(!(this is AggregateType));
-            _fHasErrors = (this is ErrorType err ? err.GetTypeParent() : GetBaseOrParameterOrElementType()).HasErrors();
+            Debug.Assert(!(this is ErrorType));
+            _fHasErrors = GetBaseOrParameterOrElementType().HasErrors();
         }
 
         public bool HasErrors()
@@ -514,7 +515,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
         // be equivalent or convertible (like ANONMETHSYMs)
         public bool IsNeverSameType()
         {
-            return this is MethodGroupType || this is ErrorType err && !err.HasParent();
+            return this is MethodGroupType || this is ErrorType err && !err.HasParent;
         }
     }
 }
index 33b3895..f737321 100644 (file)
@@ -71,8 +71,6 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
 
         public ErrorType CreateError(
             Name name,
-            CType parent,
-            AssemblyQualifiedNamespaceSymbol pParentNS,
             Name nameText,
             TypeArray typeArgs)
         {
@@ -80,8 +78,6 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
             e.SetName(name);
             e.nameText = nameText;
             e.typeArgs = typeArgs;
-            e.SetTypeParent(parent);
-            e.SetNSParent(pParentNS);
 
             e.SetTypeKind(TypeKind.TK_ErrorType);
             return e;
index 95b595e..f311312 100644 (file)
@@ -37,7 +37,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
             _typeTable = new TypeTable();
 
             // special types with their own symbol kind.
-            _errorType = _typeFactory.CreateError(null, null, null, null, null);
+            _errorType = _typeFactory.CreateError(null, null, null);
             _voidType = _typeFactory.CreateVoid();
             _nullType = _typeFactory.CreateNull();
             _typeMethGrp = _typeFactory.CreateMethodGroup();
@@ -314,18 +314,10 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
         }
 
         public ErrorType GetErrorType(
-                CType pParentType,
-                AssemblyQualifiedNamespaceSymbol pParentNS,
                 Name nameText,
                 TypeArray typeArgs)
         {
             Debug.Assert(nameText != null);
-            Debug.Assert(pParentType == null || pParentNS == null);
-            if (pParentType == null && pParentNS == null)
-            {
-                // Use the root namespace as the parent.
-                pParentNS = _BSymmgr.GetRootNsAid();
-            }
             if (typeArgs == null)
             {
                 typeArgs = BSYMMGR.EmptyTypeArray();
@@ -334,30 +326,14 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
             Name name = _BSymmgr.GetNameFromPtrs(nameText, typeArgs);
             Debug.Assert(name != null);
 
-            ErrorType pError = null;
-            if (pParentType != null)
-            {
-                pError = _typeTable.LookupError(name, pParentType);
-            }
-            else
-            {
-                Debug.Assert(pParentNS != null);
-                pError = _typeTable.LookupError(name, pParentNS);
-            }
+            ErrorType pError = _typeTable.LookupError(name);
 
             if (pError == null)
             {
                 // No existing error symbol. Create a new one.
-                pError = _typeFactory.CreateError(name, pParentType, pParentNS, nameText, typeArgs);
+                pError = _typeFactory.CreateError(name, nameText, typeArgs);
                 pError.SetErrors(true);
-                if (pParentType != null)
-                {
-                    _typeTable.InsertError(name, pParentType, pError);
-                }
-                else
-                {
-                    _typeTable.InsertError(name, pParentNS, pError);
-                }
+                _typeTable.InsertError(name, pError);
             }
             else
             {
@@ -503,22 +479,16 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
 
                 case TypeKind.TK_ErrorType:
                     ErrorType err = (ErrorType)type;
-                    if (err.HasParent())
+                    if (err.HasParent)
                     {
                         Debug.Assert(err.nameText != null && err.typeArgs != null);
-
-                        CType pParentType = null;
-                        if (err.HasTypeParent())
-                        {
-                            pParentType = SubstTypeCore(err.GetTypeParent(), pctx);
-                        }
-
                         TypeArray typeArgs = SubstTypeArray(err.typeArgs, pctx);
-                        if (typeArgs != err.typeArgs || (err.HasTypeParent() && pParentType != err.GetTypeParent()))
+                        if (typeArgs != err.typeArgs)
                         {
-                            return GetErrorType(pParentType, err.GetNSParent(), err.nameText, typeArgs);
+                            return GetErrorType(err.nameText, typeArgs);
                         }
                     }
+
                     return type;
 
                 case TypeKind.TK_TypeParameterType:
@@ -658,45 +628,27 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
 
                 case TypeKind.TK_ErrorType:
                     ErrorType errSrc = (ErrorType)typeSrc;
-                    if (!(typeDst is ErrorType errDst) || !errSrc.HasParent() || !errDst.HasParent())
+                    if (!(typeDst is ErrorType errDst) || !errSrc.HasParent || !errDst.HasParent)
                         return false;
-                    {
-                        Debug.Assert(errSrc.nameText != null && errSrc.typeArgs != null);
-                        Debug.Assert(errDst.nameText != null && errDst.typeArgs != null);
 
-                        if (errSrc.nameText != errDst.nameText || errSrc.typeArgs.Count != errDst.typeArgs.Count)
-                            return false;
+                {
+                    Debug.Assert(errSrc.nameText != null && errSrc.typeArgs != null);
+                    Debug.Assert(errDst.nameText != null && errDst.typeArgs != null);
 
-                        if (errSrc.HasTypeParent() != errDst.HasTypeParent())
-                        {
-                            return false;
-                        }
-                        if (errSrc.HasTypeParent())
-                        {
-                            if (errSrc.GetTypeParent() != errDst.GetTypeParent())
-                            {
-                                return false;
-                            }
-                            if (!SubstEqualTypesCore(errDst.GetTypeParent(), errSrc.GetTypeParent(), pctx))
-                            {
-                                return false;
-                            }
-                        }
-                        else
-                        {
-                            if (errSrc.GetNSParent() != errDst.GetNSParent())
-                            {
-                                return false;
-                            }
-                        }
+                    if (errSrc.nameText != errDst.nameText || errSrc.typeArgs.Count != errDst.typeArgs.Count
+                        || errSrc.HasParent != errDst.HasParent)
+                    {
+                        return false;
+                    }
 
-                        // All the args must unify.
-                        for (int i = 0; i < errSrc.typeArgs.Count; i++)
-                        {
-                            if (!SubstEqualTypesCore(errDst.typeArgs[i], errSrc.typeArgs[i], pctx))
-                                return false;
-                        }
+                    // All the args must unify.
+                    for (int i = 0; i < errSrc.typeArgs.Count; i++)
+                    {
+                        if (!SubstEqualTypesCore(errDst.typeArgs[i], errSrc.typeArgs[i], pctx))
+                            return false;
                     }
+                }
+
                     return true;
 
                 case TypeKind.TK_TypeParameterType:
@@ -782,7 +734,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
 
                 case TypeKind.TK_ErrorType:
                     ErrorType err = (ErrorType)type;
-                    if (err.HasParent())
+                    if (err.HasParent)
                     {
                         Debug.Assert(err.nameText != null && err.typeArgs != null);
 
@@ -791,12 +743,8 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
                             if (TypeContainsType(err.typeArgs[i], typeFind))
                                 return true;
                         }
-                        if (err.HasTypeParent())
-                        {
-                            type = err.GetTypeParent();
-                            goto LRecurse;
-                        }
                     }
+
                     return false;
 
                 case TypeKind.TK_TypeParameterType:
@@ -841,7 +789,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
 
                 case TypeKind.TK_ErrorType:
                     ErrorType err = (ErrorType)type;
-                    if (err.HasParent())
+                    if (err.HasParent)
                     {
                         Debug.Assert(err.nameText != null && err.typeArgs != null);
 
@@ -852,12 +800,8 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
                                 return true;
                             }
                         }
-                        if (err.HasTypeParent())
-                        {
-                            type = err.GetTypeParent();
-                            goto LRecurse;
-                        }
                     }
+
                     return false;
 
                 case TypeKind.TK_TypeParameterType:
index 63b8902..2329a33 100644 (file)
@@ -48,12 +48,11 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
     {
         // Two way hashes
         private readonly Dictionary<KeyPair<AggregateSymbol, Name>, AggregateType> _pAggregateTable;
-        private readonly Dictionary<KeyPair<CType, Name>, ErrorType> _pErrorWithTypeParentTable;
-        private readonly Dictionary<KeyPair<AssemblyQualifiedNamespaceSymbol, Name>, ErrorType> _pErrorWithNamespaceParentTable;
         private readonly Dictionary<KeyPair<CType, Name>, ArrayType> _pArrayTable;
         private readonly Dictionary<KeyPair<CType, Name>, ParameterModifierType> _pParameterModifierTable;
 
         // One way hashes
+        private readonly Dictionary<Name, ErrorType> _pErrorWithNamespaceParentTable;
         private readonly Dictionary<CType, PointerType> _pPointerTable;
         private readonly Dictionary<CType, NullableType> _pNullableTable;
         private readonly Dictionary<TypeParameterSymbol, TypeParameterType> _pTypeParameterTable;
@@ -61,8 +60,7 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
         public TypeTable()
         {
             _pAggregateTable = new Dictionary<KeyPair<AggregateSymbol, Name>, AggregateType>();
-            _pErrorWithNamespaceParentTable = new Dictionary<KeyPair<AssemblyQualifiedNamespaceSymbol, Name>, ErrorType>();
-            _pErrorWithTypeParentTable = new Dictionary<KeyPair<CType, Name>, ErrorType>();
+            _pErrorWithNamespaceParentTable = new Dictionary<Name, ErrorType>();
             _pArrayTable = new Dictionary<KeyPair<CType, Name>, ArrayType>();
             _pParameterModifierTable = new Dictionary<KeyPair<CType, Name>, ParameterModifierType>();
             _pPointerTable = new Dictionary<CType, PointerType>();
@@ -90,38 +88,13 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
             _pAggregateTable.Add(new KeyPair<AggregateSymbol, Name>(pAggregateSymbol, pName), pAggregate);
         }
 
-        public ErrorType LookupError(Name pName, CType pParentType)
-        {
-            var key = new KeyPair<CType, Name>(pParentType, pName);
-            ErrorType result;
-            if (_pErrorWithTypeParentTable.TryGetValue(key, out result))
-            {
-                return result;
-            }
-            return null;
-        }
-
-        public ErrorType LookupError(Name pName, AssemblyQualifiedNamespaceSymbol pParentNS)
-        {
-            var key = new KeyPair<AssemblyQualifiedNamespaceSymbol, Name>(pParentNS, pName);
-            ErrorType result;
-            if (_pErrorWithNamespaceParentTable.TryGetValue(key, out result))
-            {
-                return result;
-            }
-            return null;
-        }
-
-        public void InsertError(Name pName, CType pParentType, ErrorType pError)
-        {
-            Debug.Assert(LookupError(pName, pParentType) == null);
-            _pErrorWithTypeParentTable.Add(new KeyPair<CType, Name>(pParentType, pName), pError);
-        }
+        public ErrorType LookupError(Name pName) =>
+            _pErrorWithNamespaceParentTable.TryGetValue(pName, out ErrorType result) ? result : null;
 
-        public void InsertError(Name pName, AssemblyQualifiedNamespaceSymbol pParentNS, ErrorType pError)
+        public void InsertError(Name pName, ErrorType pError)
         {
-            Debug.Assert(LookupError(pName, pParentNS) == null);
-            _pErrorWithNamespaceParentTable.Add(new KeyPair<AssemblyQualifiedNamespaceSymbol, Name>(pParentNS, pName), pError);
+            Debug.Assert(LookupError(pName) == null);
+            _pErrorWithNamespaceParentTable.Add(pName, pError);
         }
 
         public ArrayType LookupArray(Name pName, CType pElementType)