Skip to content

Commit

Permalink
Merge pull request #41485 from jaredpar/framenull
Browse files Browse the repository at this point in the history
Use framework nullable annotations
  • Loading branch information
jaredpar authored Feb 11, 2020
2 parents c9b8d6a + ed3ef5e commit 41bc895
Show file tree
Hide file tree
Showing 49 changed files with 91 additions and 94 deletions.
16 changes: 8 additions & 8 deletions src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ internal override IEnumerable<ReferenceDirective> ReferenceDirectives
{
RoslynDebug.Assert(directive.SyntaxTree.FilePath is object);

MetadataReference reference;
MetadataReference? reference;
return ReferenceDirectiveMap.TryGetValue((directive.SyntaxTree.FilePath, directive.File.ValueText), out reference) ? reference : null;
}

Expand Down Expand Up @@ -1268,13 +1268,13 @@ internal SourceAssemblySymbol SourceAssembly
return null;
}

private ConcurrentDictionary<string?, NamespaceSymbol>? _externAliasTargets;
private ConcurrentDictionary<string, NamespaceSymbol>? _externAliasTargets;

internal bool GetExternAliasTarget(string? aliasName, out NamespaceSymbol? @namespace)
internal bool GetExternAliasTarget(string aliasName, out NamespaceSymbol? @namespace)
{
if (_externAliasTargets == null)
{
Interlocked.CompareExchange(ref _externAliasTargets, new ConcurrentDictionary<string?, NamespaceSymbol>(), null);
Interlocked.CompareExchange(ref _externAliasTargets, new ConcurrentDictionary<string, NamespaceSymbol>(), null);
}
else if (_externAliasTargets.TryGetValue(aliasName, out @namespace))
{
Expand Down Expand Up @@ -1443,7 +1443,7 @@ private static CSDiagnosticInfo CreateReflectionTypeNotFoundError(Type type)
// The type or namespace name '{0}' could not be found in the global namespace (are you missing an assembly reference?)
return new CSDiagnosticInfo(
ErrorCode.ERR_GlobalSingleTypeNameNotFound,
new object[] { type.AssemblyQualifiedName },
new object[] { type.AssemblyQualifiedName ?? "" },
ImmutableArray<Symbol>.Empty,
ImmutableArray<Location>.Empty
);
Expand Down Expand Up @@ -2045,7 +2045,7 @@ internal BinderFactory GetBinderFactory(SyntaxTree syntaxTree)
binderFactories = Interlocked.CompareExchange(ref _binderFactories, binderFactories, null) ?? binderFactories;
}

BinderFactory previousFactory;
BinderFactory? previousFactory;
var previousWeakReference = binderFactories[treeNum];
if (previousWeakReference != null && previousWeakReference.TryGetTarget(out previousFactory))
{
Expand All @@ -2062,7 +2062,7 @@ private BinderFactory AddNewFactory(SyntaxTree syntaxTree, [NotNull] ref WeakRef

while (true)
{
BinderFactory previousFactory;
BinderFactory? previousFactory;
WeakReference<BinderFactory>? previousWeakReference = slot;
if (previousWeakReference != null && previousWeakReference.TryGetTarget(out previousFactory))
{
Expand Down Expand Up @@ -3620,7 +3620,7 @@ private void AppendMemberSymbolsWithName(
}

private NamespaceOrTypeSymbol? GetCachedSymbol(MergedNamespaceOrTypeDeclaration declaration)
=> _cache.TryGetValue(declaration, out NamespaceOrTypeSymbol symbol)
=> _cache.TryGetValue(declaration, out NamespaceOrTypeSymbol? symbol)
? symbol
: null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<PackageDescription>
.NET Compiler Platform ("Roslyn") support for C#, Microsoft.CodeAnalysis.CSharp.dll.
</PackageDescription>

<!-- Disable the nullable warnings when compiling for netcoreapp3.1 during our transition period -->
<DisableNullableWarnings Condition="'$(TargetFramework)' == 'netcoreapp3.1'">true</DisableNullableWarnings>
<DisableNullableWarnings Condition="'$(TargetFramework)' == 'netstandard2.0'">false</DisableNullableWarnings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Portable\Microsoft.CodeAnalysis.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Symbols/ReferenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ private static MissingAssemblySymbol GetOrAddMissingAssemblySymbol(
AssemblyIdentity assemblyIdentity,
ref Dictionary<AssemblyIdentity, MissingAssemblySymbol>? missingAssemblies)
{
MissingAssemblySymbol missingAssembly;
MissingAssemblySymbol? missingAssembly;

if (missingAssemblies == null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/Core/Portable/AssemblyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static ImmutableArray<string> FindAssemblySet(string filePath)
var reference = metadataReader.GetAssemblyReference(handle);
var referenceName = metadataReader.GetString(reference.Name);

string referencePath = Path.Combine(directory, referenceName + ".dll");
string referencePath = Path.Combine(directory!, referenceName + ".dll");

if (!assemblySet.Contains(referencePath) &&
File.Exists(referencePath))
Expand Down Expand Up @@ -111,7 +111,7 @@ public static ImmutableArray<string> FindSatelliteAssemblies(string filePath)

var builder = ImmutableArray.CreateBuilder<string>();

string directory = Path.GetDirectoryName(filePath);
string? directory = Path.GetDirectoryName(filePath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string resourcesNameWithoutExtension = fileNameWithoutExtension + ".resources";
string resourcesNameWithExtension = resourcesNameWithoutExtension + ".dll";
Expand Down
14 changes: 7 additions & 7 deletions src/Compilers/Core/Portable/CaseInsensitiveComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ private static int CompareLowerUnicode(char c1, char c2)
return (c1 == c2) ? 0 : ToLower(c1) - ToLower(c2);
}

public override int Compare(string str1, string str2)
public override int Compare(string? str1, string? str2)
{
if ((object)str1 == str2)
if ((object?)str1 == str2)
{
return 0;
}

if ((object)str1 == null)
if (str1 is null)
{
return -1;
}

if ((object)str2 == null)
if (str2 is null)
{
return 1;
}
Expand All @@ -124,14 +124,14 @@ private static bool AreEqualLowerUnicode(char c1, char c2)
return c1 == c2 || ToLower(c1) == ToLower(c2);
}

public override bool Equals(string str1, string str2)
public override bool Equals(string? str1, string? str2)
{
if ((object)str1 == str2)
if ((object?)str1 == str2)
{
return true;
}

if ((object)str1 == null || (object)str2 == null)
if (str1 is null || str2 is null)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public string ToString(string? format, IFormatProvider? formatProvider)
{
if (_targetResourceId != null)
{
return CodeAnalysisResources.ResourceManager.GetString(_targetResourceId, formatProvider as System.Globalization.CultureInfo);
return CodeAnalysisResources.ResourceManager.GetString(_targetResourceId, formatProvider as System.Globalization.CultureInfo) ?? string.Empty;
}

return string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/CodeGen/ArrayMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ArrayMethod GetArrayAddress(Cci.IArrayTypeReference arrayType)
private ArrayMethod GetArrayMethod(Cci.IArrayTypeReference arrayType, ArrayMethodKind id)
{
var key = ((byte)id, arrayType);
ArrayMethod result;
ArrayMethod? result;

var dict = _dict;
if (!dict.TryGetValue(key, out result))
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/Core/Portable/CodeGen/LocalSlotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public bool Equals(LocalSignature other)
public override int GetHashCode()
=> Hash.Combine(_type, (int)_constraints);

public override bool Equals(object obj)
=> Equals((LocalSignature)obj);
public override bool Equals(object? obj)
=> obj is LocalSignature ls && Equals(ls);
}

// maps local identities to locals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internal Cci.IFieldReference GetModuleVersionId(Cci.ITypeReference mvidType)

internal Cci.IFieldReference GetOrAddInstrumentationPayloadRoot(int analysisKind, Cci.ITypeReference payloadRootType)
{
InstrumentationPayloadRootField payloadRootField;
InstrumentationPayloadRootField? payloadRootField;
if (!_instrumentationPayloadRootFields.TryGetValue(analysisKind, out payloadRootField))
{
Debug.Assert(!IsFrozen);
Expand Down Expand Up @@ -222,9 +222,9 @@ internal bool TryAddSynthesizedMethod(Cci.IMethodDefinition method)
}

// Get method by name, if one exists. Otherwise return null.
internal Cci.IMethodDefinition GetMethod(string name)
internal Cci.IMethodDefinition? GetMethod(string name)
{
Cci.IMethodDefinition method;
Cci.IMethodDefinition? method;
_synthesizedMethods.TryGetValue(name, out method);
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static Dictionary<uint, HashBucket> ComputeStringHashMap(

uint hash = computeStringHashcodeDelegate((string?)stringConstant.Value);

HashBucket bucket;
HashBucket? bucket;
if (!stringHashMap.TryGetValue(hash, out bucket))
{
bucket = new HashBucket();
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/Collections/BitVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool Equals(BitVector other)
&& _bits.AsSpan().SequenceEqual(other._bits.AsSpan());
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is BitVector other && Equals(other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void AddIdentifier(string identifier)
{
RoslynDebug.Assert(identifier != null);

object value;
object? value;
if (!_map.TryGetValue(identifier, out value))
{
AddInitialSpelling(identifier);
Expand Down Expand Up @@ -116,7 +116,7 @@ private bool CaseInsensitiveContains(string identifier)

private bool CaseSensitiveContains(string identifier)
{
object spellings;
object? spellings;
if (_map.TryGetValue(identifier, out spellings))
{
var spelling = spellings as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ internal static ImmutableArray<T> DangerousCreateFromUnderlyingArray<T>([MaybeNu
}

internal static Dictionary<K, ImmutableArray<T>> ToDictionary<K, T>(this ImmutableArray<T> items, Func<T, K> keySelector, IEqualityComparer<K>? comparer = null)
where K : notnull
{
if (items.Length == 1)
{
Expand Down
5 changes: 2 additions & 3 deletions src/Compilers/Core/Portable/CommandLine/SarifErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ protected static bool HasPath(Location location)
protected static string GetUri(string path)
{
Debug.Assert(!string.IsNullOrEmpty(path));
Uri uri;

// Note that in general, these "paths" are opaque strings to be
// interpreted by resolvers (see SyntaxTree.FilePath documentation).
Expand All @@ -134,7 +133,7 @@ protected static string GetUri(string path)
// N.B. URI does not handle multiple backslashes or `..` well, so call GetFullPath
// to normalize before going to URI
var fullPath = Path.GetFullPath(path);
if (Uri.TryCreate(fullPath, UriKind.Absolute, out uri))
if (Uri.TryCreate(fullPath, UriKind.Absolute, out var uri))
{
// We use Uri.AbsoluteUri and not Uri.ToString() because Uri.ToString()
// is unescaped (e.g. spaces remain unreplaced by %20) and therefore
Expand All @@ -151,7 +150,7 @@ protected static string GetUri(string path)
path = PathUtilities.NormalizeWithForwardSlash(path);
}

if (Uri.TryCreate(path, UriKind.Relative, out uri))
if (Uri.TryCreate(path, UriKind.Relative, out var uri))
{
// First fallback attempt: attempt to interpret as relative path/URI.
// (Perhaps the resolver works that way.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private sealed class DiagnosticDescriptorSet
public string Add(DiagnosticDescriptor descriptor)
{
// Case 1: Descriptor has already been seen -> retrieve key from cache.
if (_keys.TryGetValue(descriptor, out string key))
if (_keys.TryGetValue(descriptor, out string? key))
{
return key;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/Core/Portable/Compilation/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2803,12 +2803,12 @@ internal bool SerializeToPeStream(
}
catch (Cci.PeWritingException e)
{
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.InnerException.ToString()));
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.InnerException?.ToString() ?? ""));
return false;
}
catch (ResourceException e)
{
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_CantReadResource, Location.None, e.Message, e.InnerException.Message));
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_CantReadResource, Location.None, e.Message, e.InnerException?.Message ?? ""));
return false;
}
catch (PermissionSetFileReadException e)
Expand Down Expand Up @@ -2967,7 +2967,7 @@ internal static bool SerializePeToStream(
}
catch (Cci.PeWritingException e)
{
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.InnerException.ToString()));
diagnostics.Add(MessageProvider.CreateDiagnostic(MessageProvider.ERR_PeWritingFailure, Location.None, e.InnerException?.ToString() ?? ""));
return null;
}
catch (PermissionSetFileReadException e)
Expand All @@ -2980,7 +2980,7 @@ internal static bool SerializePeToStream(

internal string? Feature(string p)
{
string v;
string? v;
return _features.TryGetValue(p, out v) ? v : null;
}

Expand Down Expand Up @@ -3020,7 +3020,7 @@ internal bool IsImportDirectiveUsed(SyntaxTree syntaxTree, int position)
return true;
}

SmallConcurrentSetOfInts usedImports;
SmallConcurrentSetOfInts? usedImports;
return syntaxTree != null &&
TreeToUsedImportDirectivesMap.TryGetValue(syntaxTree, out usedImports) &&
usedImports.Contains(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public ImmutableArray<Diagnostic> Errors
get { return _lazyErrors.Value; }
}

public abstract override bool Equals(object obj);
public abstract override bool Equals(object? obj);

protected bool EqualsHelper(CompilationOptions other)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/Compilation/LoadDirective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public bool Equals(LoadDirective other)
this.Diagnostics.SequenceEqual(other.Diagnostics);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is LoadDirective && Equals((LoadDirective)obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool Equals(PreprocessingSymbolInfo other)
&& object.Equals(this.IsDefined, other.IsDefined);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is PreprocessingSymbolInfo p && this.Equals(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected SourceReferenceResolver()
{
}

public abstract override bool Equals(object other);
public abstract override bool Equals(object? other);
public abstract override int GetHashCode();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public bool IsValid
}
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is SubsystemVersion && Equals((SubsystemVersion)obj);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/ConstantValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ public override int GetHashCode()
return this.Discriminator.GetHashCode();
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return this.Equals(obj as ConstantValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override bool Equals(Diagnostic? obj)
Equals(_programmaticSuppressionInfo, other._programmaticSuppressionInfo);
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return this.Equals(obj as Diagnostic);
}
Expand Down
Loading

0 comments on commit 41bc895

Please sign in to comment.