From 92b609911b5ce8276a5f71a9932ae095c4204513 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 23 Nov 2020 09:45:07 +0000 Subject: [PATCH] Enable nullable: System.Management.Automation.Language.TypeName --- .../engine/parser/ast.cs | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/ast.cs b/src/System.Management.Automation/engine/parser/ast.cs index 2a126397d5d..0fca50860e1 100644 --- a/src/System.Management.Automation/engine/parser/ast.cs +++ b/src/System.Management.Automation/engine/parser/ast.cs @@ -28,6 +28,7 @@ namespace System.Management.Automation.Language using SwitchClause = Tuple; using System.Runtime.CompilerServices; using System.Reflection.Emit; + using System.Diagnostics; internal interface ISupportsAssignment { @@ -8175,12 +8176,13 @@ internal interface ISupportsTypeCaching /// /// A simple type that is not an array or does not have generic arguments. /// +#nullable enable public sealed class TypeName : ITypeName, ISupportsTypeCaching { internal readonly string _name; - internal Type _type; + internal Type? _type; internal readonly IScriptExtent _extent; - internal TypeDefinitionAst _typeDefinitionAst; + internal TypeDefinitionAst? _typeDefinitionAst; /// /// Construct a simple typename. @@ -8252,7 +8254,7 @@ public TypeName(IScriptExtent extent, string name, string assembly) /// /// The name of the assembly, if specified, otherwise null. /// - public string AssemblyName { get; internal set; } + public string? AssemblyName { get; internal set; } /// /// Always returns false, array typenames are instances of . @@ -8273,7 +8275,7 @@ internal bool HasDefaultCtor() { if (_typeDefinitionAst == null) { - Type reflectionType = GetReflectionType(); + Type? reflectionType = GetReflectionType(); if (reflectionType == null) { // we are pessimistic about default ctor presence. @@ -8312,7 +8314,7 @@ internal bool HasDefaultCtor() /// The if possible, null otherwise. Null may be returned for valid typenames if the assembly /// containing the type has not been loaded. /// - public Type GetReflectionType() + public Type? GetReflectionType() { if (_type == null) { @@ -8347,7 +8349,7 @@ public Type GetReflectionType() /// The if possible, null otherwise. Null may be returned for valid typenames if the assembly /// containing the type has not been loaded. /// - public Type GetReflectionAttributeType() + public Type? GetReflectionAttributeType() { var result = GetReflectionType(); if (result == null || !typeof(Attribute).IsAssignableFrom(result)) @@ -8379,7 +8381,7 @@ public override string ToString() } /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is TypeName other)) return false; @@ -8418,7 +8420,8 @@ public override int GetHashCode() /// internal bool IsType(Type type) { - string fullTypeName = type.FullName; + string? fullTypeName = type.FullName; + Debug.Assert(fullTypeName is not null); if (fullTypeName.Equals(Name, StringComparison.OrdinalIgnoreCase)) return true; int lastDotIndex = fullTypeName.LastIndexOf('.'); @@ -8430,7 +8433,7 @@ internal bool IsType(Type type) return false; } - Type ISupportsTypeCaching.CachedType + Type? ISupportsTypeCaching.CachedType { get { return _type; } @@ -8443,8 +8446,8 @@ Type ISupportsTypeCaching.CachedType /// public sealed class GenericTypeName : ITypeName, ISupportsTypeCaching { - private string _cachedFullName; - private Type _cachedType; + private string? _cachedFullName; + private Type? _cachedType; /// /// Construct a generic type name. @@ -8556,7 +8559,7 @@ public string Name /// /// The name of the assembly, if specified, otherwise null. /// - public string AssemblyName { get { return TypeName.AssemblyName; } } + public string? AssemblyName { get { return TypeName.AssemblyName; } } /// /// Always returns false because this class does not represent arrays. @@ -8586,11 +8589,11 @@ public string Name /// /// Returns the that this typename represents, if such a type exists, null otherwise. /// - public Type GetReflectionType() + public Type? GetReflectionType() { if (_cachedType == null) { - Type generic = GetGenericType(TypeName.GetReflectionType()); + Type? generic = GetGenericType(TypeName.GetReflectionType()); if (generic != null && generic.ContainsGenericParameters) { @@ -8639,7 +8642,7 @@ public Type GetReflectionType() /// /// /// - internal Type GetGenericType(Type generic) + internal Type? GetGenericType(Type? generic) { if (generic == null || !generic.ContainsGenericParameters) { @@ -8662,12 +8665,12 @@ internal Type GetGenericType(Type generic) /// The if possible, null otherwise. Null may be returned for valid typenames if the assembly /// containing the type has not been loaded. /// - public Type GetReflectionAttributeType() + public Type? GetReflectionAttributeType() { - Type type = GetReflectionType(); + Type? type = GetReflectionType(); if (type == null) { - Type generic = TypeName.GetReflectionAttributeType(); + Type? generic = TypeName.GetReflectionAttributeType(); if (generic == null || !generic.ContainsGenericParameters) { if (!TypeName.FullName.Contains('`')) @@ -8697,7 +8700,7 @@ public override string ToString() } /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is GenericTypeName other)) return false; @@ -8731,7 +8734,7 @@ public override int GetHashCode() return hash; } - Type ISupportsTypeCaching.CachedType + Type? ISupportsTypeCaching.CachedType { get { return _cachedType; } @@ -8744,8 +8747,8 @@ Type ISupportsTypeCaching.CachedType /// public sealed class ArrayTypeName : ITypeName, ISupportsTypeCaching { - private string _cachedFullName; - private Type _cachedType; + private string? _cachedFullName; + private Type? _cachedType; /// /// Construct an ArrayTypeName. @@ -8836,7 +8839,7 @@ public string Name /// /// The name of the assembly, if specified, otherwise null. /// - public string AssemblyName { get { return ElementType.AssemblyName; } } + public string? AssemblyName { get { return ElementType.AssemblyName; } } /// /// Returns true always as this class represents arrays. @@ -8866,14 +8869,14 @@ public string Name /// /// Returns the that this typename represents, if such a type exists, null otherwise. /// - public Type GetReflectionType() + public Type? GetReflectionType() { try { RuntimeHelpers.EnsureSufficientExecutionStack(); if (_cachedType == null) { - Type elementType = ElementType.GetReflectionType(); + Type? elementType = ElementType.GetReflectionType(); if (elementType != null) { Type type = Rank == 1 ? elementType.MakeArrayType() : elementType.MakeArrayType(Rank); @@ -8908,7 +8911,7 @@ public Type GetReflectionType() /// /// Always return null, arrays can never be an attribute. /// - public Type GetReflectionAttributeType() + public Type? GetReflectionAttributeType() { return null; } @@ -8922,7 +8925,7 @@ public override string ToString() } /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is ArrayTypeName other)) return false; @@ -8936,7 +8939,7 @@ public override int GetHashCode() return Utils.CombineHashCodes(ElementType.GetHashCode(), Rank.GetHashCode()); } - Type ISupportsTypeCaching.CachedType + Type? ISupportsTypeCaching.CachedType { get { return _cachedType; } @@ -8981,7 +8984,7 @@ public ReflectionTypeName(Type type) /// /// The name of the assembly. /// - public string AssemblyName { get { return _type.Assembly.FullName; } } + public string? AssemblyName { get { return _type.Assembly.FullName; } } /// /// Returns true if the type is an array, false otherwise. @@ -9023,7 +9026,7 @@ public override string ToString() } /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is ReflectionTypeName other)) return false; @@ -9036,7 +9039,7 @@ public override int GetHashCode() return _type.GetHashCode(); } - Type ISupportsTypeCaching.CachedType + Type? ISupportsTypeCaching.CachedType { get { return _type; } @@ -9101,6 +9104,7 @@ internal override AstVisitAction InternalVisit(AstVisitor visitor) #endregion Visitors } +#nullable restore /// /// The ast representing a variable reference, either normal references, e.g. $true, or splatted references