-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable enable GetTypeByMetadataName #58317
Merged
333fred
merged 2 commits into
dotnet:main
from
333fred:nullable-enable-gettypebymetadataname
Dec 14, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1562,7 +1562,7 @@ internal override ISymbolInternal CommonGetSpecialTypeMember(SpecialMember speci | |
internal TypeSymbol GetTypeByReflectionType(Type type, BindingDiagnosticBag diagnostics) | ||
{ | ||
var result = Assembly.GetTypeByReflectionType(type, includeReferences: true); | ||
if ((object)result == null) | ||
if (result is null) | ||
{ | ||
var errorType = new ExtendedErrorTypeSymbol(this, type.Name, 0, CreateReflectionTypeNotFoundError(type)); | ||
diagnostics.Add(errorType.ErrorInfo, NoLocation.Singleton); | ||
|
@@ -1590,9 +1590,9 @@ protected override ITypeSymbol? CommonScriptGlobalsType | |
{ | ||
if (HostObjectType != null && _lazyHostObjectTypeSymbol is null) | ||
{ | ||
TypeSymbol symbol = Assembly.GetTypeByReflectionType(HostObjectType, includeReferences: true); | ||
TypeSymbol? symbol = Assembly.GetTypeByReflectionType(HostObjectType, includeReferences: true); | ||
|
||
if ((object)symbol == null) | ||
if (symbol is null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
MetadataTypeName mdName = MetadataTypeName.FromNamespaceAndTypeName(HostObjectType.Namespace ?? String.Empty, | ||
HostObjectType.Name, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.PortableExecutable; | ||
|
@@ -565,12 +566,13 @@ internal NamedTypeSymbol GetPrimitiveType(Microsoft.Cci.PrimitiveTypeCode type) | |
return GetSpecialType(SpecialTypes.GetTypeFromMetadataName(type)); | ||
} | ||
|
||
#nullable enable | ||
/// <summary> | ||
/// Lookup a type within the assembly using the canonical CLR metadata name of the type. | ||
/// </summary> | ||
/// <param name="fullyQualifiedMetadataName">Type name.</param> | ||
/// <returns>Symbol for the type or null if type cannot be found or is ambiguous. </returns> | ||
public NamedTypeSymbol GetTypeByMetadataName(string fullyQualifiedMetadataName) | ||
public NamedTypeSymbol? GetTypeByMetadataName(string fullyQualifiedMetadataName) | ||
{ | ||
if (fullyQualifiedMetadataName == null) | ||
{ | ||
|
@@ -606,16 +608,16 @@ public NamedTypeSymbol GetTypeByMetadataName(string fullyQualifiedMetadataName) | |
/// In cases a type could not be found because of ambiguity, we return two of the candidates that caused the ambiguity. | ||
/// </param> | ||
/// <returns>Null if the type can't be found.</returns> | ||
internal NamedTypeSymbol GetTypeByMetadataName( | ||
internal NamedTypeSymbol? GetTypeByMetadataName( | ||
string metadataName, | ||
bool includeReferences, | ||
bool isWellKnownType, | ||
out (AssemblySymbol, AssemblySymbol) conflicts, | ||
bool useCLSCompliantNameArityEncoding = false, | ||
DiagnosticBag warnings = null, | ||
DiagnosticBag? warnings = null, | ||
bool ignoreCorLibraryDuplicatedTypes = false) | ||
{ | ||
NamedTypeSymbol type; | ||
NamedTypeSymbol? type; | ||
MetadataTypeName mdName; | ||
|
||
if (metadataName.IndexOf('+') >= 0) | ||
|
@@ -626,7 +628,7 @@ internal NamedTypeSymbol GetTypeByMetadataName( | |
type = GetTopLevelTypeByMetadataName(ref mdName, assemblyOpt: null, includeReferences: includeReferences, isWellKnownType: isWellKnownType, | ||
conflicts: out conflicts, warnings: warnings, ignoreCorLibraryDuplicatedTypes: ignoreCorLibraryDuplicatedTypes); | ||
|
||
for (int i = 1; (object)type != null && !type.IsErrorType() && i < parts.Length; i++) | ||
for (int i = 1; type is object && !type.IsErrorType() && i < parts.Length; i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
mdName = MetadataTypeName.FromTypeName(parts[i]); | ||
NamedTypeSymbol temp = type.LookupMetadataType(ref mdName); | ||
|
@@ -640,7 +642,7 @@ internal NamedTypeSymbol GetTypeByMetadataName( | |
conflicts: out conflicts, warnings: warnings, ignoreCorLibraryDuplicatedTypes: ignoreCorLibraryDuplicatedTypes); | ||
} | ||
|
||
return ((object)type == null || type.IsErrorType()) ? null : type; | ||
return (type is null || type.IsErrorType()) ? null : type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
private static readonly char[] s_nestedTypeNameSeparators = new char[] { '+' }; | ||
|
@@ -652,7 +654,7 @@ internal NamedTypeSymbol GetTypeByMetadataName( | |
/// <param name="type">The type to resolve.</param> | ||
/// <param name="includeReferences">Use referenced assemblies for resolution.</param> | ||
/// <returns>The resolved symbol if successful or null on failure.</returns> | ||
internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | ||
internal TypeSymbol? GetTypeByReflectionType(Type type, bool includeReferences) | ||
{ | ||
System.Reflection.TypeInfo typeInfo = type.GetTypeInfo(); | ||
|
||
|
@@ -663,8 +665,8 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
|
||
if (typeInfo.IsArray) | ||
{ | ||
TypeSymbol symbol = GetTypeByReflectionType(typeInfo.GetElementType(), includeReferences); | ||
if ((object)symbol == null) | ||
TypeSymbol? symbol = GetTypeByReflectionType(typeInfo.GetElementType()!, includeReferences); | ||
if (symbol is null) | ||
{ | ||
return null; | ||
} | ||
|
@@ -675,8 +677,8 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
} | ||
else if (typeInfo.IsPointer) | ||
{ | ||
TypeSymbol symbol = GetTypeByReflectionType(typeInfo.GetElementType(), includeReferences); | ||
if ((object)symbol == null) | ||
TypeSymbol? symbol = GetTypeByReflectionType(typeInfo.GetElementType()!, includeReferences); | ||
if (symbol is null) | ||
{ | ||
return null; | ||
} | ||
|
@@ -707,8 +709,8 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
} | ||
|
||
int i = nestedTypes.Count - 1; | ||
var symbol = (NamedTypeSymbol)GetTypeByReflectionType(nestedTypes[i].AsType(), includeReferences); | ||
if ((object)symbol == null) | ||
var symbol = (NamedTypeSymbol?)GetTypeByReflectionType(nestedTypes[i].AsType(), includeReferences); | ||
if (symbol is null) | ||
{ | ||
return null; | ||
} | ||
|
@@ -719,13 +721,13 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
MetadataTypeName mdName = MetadataTypeName.FromTypeName(nestedTypes[i].Name, forcedArity: forcedArity); | ||
|
||
symbol = symbol.LookupMetadataType(ref mdName); | ||
if ((object)symbol == null || symbol.IsErrorType()) | ||
if (symbol is null || symbol.IsErrorType()) | ||
{ | ||
return null; | ||
} | ||
|
||
symbol = ApplyGenericArguments(symbol, genericArguments, ref typeArgumentIndex, includeReferences); | ||
if ((object)symbol == null) | ||
if (symbol is null) | ||
{ | ||
return null; | ||
} | ||
|
@@ -744,9 +746,9 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
typeInfo.Name, | ||
forcedArity: typeInfo.GenericTypeArguments.Length); | ||
|
||
NamedTypeSymbol symbol = GetTopLevelTypeByMetadataName(ref mdName, assemblyId, includeReferences, isWellKnownType: false, conflicts: out var _); | ||
NamedTypeSymbol? symbol = GetTopLevelTypeByMetadataName(ref mdName, assemblyId, includeReferences, isWellKnownType: false, conflicts: out var _); | ||
|
||
if ((object)symbol == null || symbol.IsErrorType()) | ||
if (symbol is null || symbol.IsErrorType()) | ||
{ | ||
return null; | ||
} | ||
|
@@ -759,7 +761,7 @@ internal TypeSymbol GetTypeByReflectionType(Type type, bool includeReferences) | |
} | ||
} | ||
|
||
private NamedTypeSymbol ApplyGenericArguments(NamedTypeSymbol symbol, Type[] typeArguments, ref int currentTypeArgument, bool includeReferences) | ||
private NamedTypeSymbol? ApplyGenericArguments(NamedTypeSymbol symbol, Type[] typeArguments, ref int currentTypeArgument, bool includeReferences) | ||
{ | ||
int remainingTypeArguments = typeArguments.Length - currentTypeArgument; | ||
|
||
|
@@ -776,7 +778,7 @@ private NamedTypeSymbol ApplyGenericArguments(NamedTypeSymbol symbol, Type[] typ | |
for (int i = 0; i < length; i++) | ||
{ | ||
var argSymbol = GetTypeByReflectionType(typeArguments[currentTypeArgument++], includeReferences); | ||
if ((object)argSymbol == null) | ||
if (argSymbol is null) | ||
{ | ||
return null; | ||
} | ||
|
@@ -786,13 +788,13 @@ private NamedTypeSymbol ApplyGenericArguments(NamedTypeSymbol symbol, Type[] typ | |
return symbol.ConstructIfGeneric(typeArgumentSymbols.ToImmutableAndFree()); | ||
} | ||
|
||
internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | ||
internal NamedTypeSymbol? GetTopLevelTypeByMetadataName( | ||
ref MetadataTypeName metadataName, | ||
AssemblyIdentity assemblyOpt, | ||
AssemblyIdentity? assemblyOpt, | ||
bool includeReferences, | ||
bool isWellKnownType, | ||
out (AssemblySymbol, AssemblySymbol) conflicts, | ||
DiagnosticBag warnings = null, // this is set to collect ambiguity warning for well-known types before C# 7 | ||
DiagnosticBag? warnings = null, // this is set to collect ambiguity warning for well-known types before C# 7 | ||
bool ignoreCorLibraryDuplicatedTypes = false) | ||
{ | ||
// Type from this assembly always wins. | ||
|
@@ -805,7 +807,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
Debug.Assert(warnings is null || isWellKnownType); | ||
|
||
conflicts = default; | ||
NamedTypeSymbol result; | ||
NamedTypeSymbol? result; | ||
|
||
// First try this assembly | ||
result = GetTopLevelTypeByMetadataName(this, ref metadataName, assemblyOpt); | ||
|
@@ -816,7 +818,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
} | ||
|
||
// ignore any types of the same name that might be in referenced assemblies (prefer the current assembly): | ||
if ((object)result != null || !includeReferences) | ||
if (result is object || !includeReferences) | ||
{ | ||
return result; | ||
} | ||
|
@@ -829,7 +831,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
!CorLibrary.IsMissing && | ||
!isWellKnownTypeBeforeCSharp7 && !ignoreCorLibraryDuplicatedTypes) | ||
{ | ||
NamedTypeSymbol corLibCandidate = GetTopLevelTypeByMetadataName(CorLibrary, ref metadataName, assemblyOpt); | ||
NamedTypeSymbol? corLibCandidate = GetTopLevelTypeByMetadataName(CorLibrary, ref metadataName, assemblyOpt); | ||
skipCorLibrary = true; | ||
|
||
if (isValidCandidate(corLibCandidate, isWellKnownType)) | ||
|
@@ -863,7 +865,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
continue; | ||
} | ||
|
||
NamedTypeSymbol candidate = GetTopLevelTypeByMetadataName(assembly, ref metadataName, assemblyOpt); | ||
NamedTypeSymbol? candidate = GetTopLevelTypeByMetadataName(assembly, ref metadataName, assemblyOpt); | ||
|
||
if (!isValidCandidate(candidate, isWellKnownType)) | ||
{ | ||
|
@@ -872,7 +874,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
|
||
Debug.Assert(!TypeSymbol.Equals(candidate, result, TypeCompareKind.ConsiderEverything)); | ||
|
||
if ((object)result != null) | ||
if (result is object) | ||
{ | ||
// duplicate | ||
if (ignoreCorLibraryDuplicatedTypes) | ||
|
@@ -910,7 +912,7 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName( | |
assemblies.Free(); | ||
return result; | ||
|
||
bool isValidCandidate(NamedTypeSymbol candidate, bool isWellKnownType) | ||
bool isValidCandidate([NotNullWhen(true)] NamedTypeSymbol? candidate, bool isWellKnownType) | ||
{ | ||
return candidate is not null | ||
&& (!isWellKnownType || IsValidWellKnownType(candidate)) | ||
|
@@ -923,9 +925,9 @@ private bool IsInCorLib(NamedTypeSymbol type) | |
return (object)type.ContainingAssembly == CorLibrary; | ||
} | ||
|
||
private bool IsValidWellKnownType(NamedTypeSymbol result) | ||
private bool IsValidWellKnownType(NamedTypeSymbol? result) | ||
{ | ||
if ((object)result == null || result.TypeKind == TypeKind.Error) | ||
if (result is null || result.TypeKind == TypeKind.Error) | ||
{ | ||
return false; | ||
} | ||
|
@@ -936,7 +938,7 @@ private bool IsValidWellKnownType(NamedTypeSymbol result) | |
return result.DeclaredAccessibility == Accessibility.Public || IsSymbolAccessible(result, this); | ||
} | ||
|
||
private static NamedTypeSymbol GetTopLevelTypeByMetadataName(AssemblySymbol assembly, ref MetadataTypeName metadataName, AssemblyIdentity assemblyOpt) | ||
private static NamedTypeSymbol? GetTopLevelTypeByMetadataName(AssemblySymbol assembly, ref MetadataTypeName metadataName, AssemblyIdentity? assemblyOpt) | ||
{ | ||
var result = assembly.LookupTopLevelMetadataType(ref metadataName, digThroughForwardedTypes: false); | ||
if (!IsAcceptableMatchForGetTypeByMetadataName(result)) | ||
|
@@ -956,6 +958,7 @@ private static bool IsAcceptableMatchForGetTypeByMetadataName(NamedTypeSymbol ca | |
{ | ||
return candidate.Kind != SymbolKind.ErrorType || !(candidate is MissingMetadataTypeSymbol); | ||
} | ||
#nullable disable | ||
|
||
/// <summary> | ||
/// Lookup member declaration in predefined CorLib type in this Assembly. Only valid if this | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a "style-only" change? Please revert if so. #WontFix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A change of some sort is necessary to
(object?)
or useis
. Personally, I'm happy with the latter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As Julien said, these lines need to change in some way to avoid the nullable warning for casting to
object
. Given that, I used the style of null check I prefer.