Skip to content

Commit

Permalink
Merge pull request #50895 from huoyaoyuan/net5.0-nrt
Browse files Browse the repository at this point in the history
Address NRT issues found by targeting net5.0
  • Loading branch information
jasonmalinowski authored Mar 5, 2021
2 parents b8f5462 + cf72285 commit 0aaa2eb
Show file tree
Hide file tree
Showing 30 changed files with 50 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ internal override bool HasSubmissionResult()
}

// Is there a trailing expression?
var lastGlobalStatement = (GlobalStatementSyntax)root.Members.LastOrDefault(m => m.IsKind(SyntaxKind.GlobalStatement));
var lastGlobalStatement = (GlobalStatementSyntax?)root.Members.LastOrDefault(m => m.IsKind(SyntaxKind.GlobalStatement));
if (lastGlobalStatement != null)
{
var statement = lastGlobalStatement.Statement;
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/Core/Portable/AssemblyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static ImmutableArray<string> FindSatelliteAssemblies(string filePath)
var builder = ImmutableArray.CreateBuilder<string>();

string? directory = Path.GetDirectoryName(filePath);
RoslynDebug.AssertNotNull(directory);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string resourcesNameWithoutExtension = fileNameWithoutExtension + ".resources";
string resourcesNameWithExtension = resourcesNameWithoutExtension + ".dll";
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ private void CompileAndEmit(
var path = Path.Combine(Arguments.GeneratedFilesOutputDirectory!, tree.FilePath);
if (Directory.Exists(Arguments.GeneratedFilesOutputDirectory))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
}

var fileStream = OpenFile(path, diagnostics, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal static void AppendData(this IncrementalHash hash, IEnumerable<ArraySegm

internal static void AppendData(this IncrementalHash hash, ArraySegment<byte> segment)
{
RoslynDebug.AssertNotNull(segment.Array);
hash.AppendData(segment.Array, segment.Offset, segment.Count);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static T EnsureInitialized<T>([NotNull] ref T? target, Func<T> valueFacto
=> LazyInitializer.EnsureInitialized<T>(ref target!, valueFactory);

/// <inheritdoc cref="LazyInitializer.EnsureInitialized{T}(ref T, ref bool, ref object)"/>
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNull] ref object? syncLock)
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock)
=> LazyInitializer.EnsureInitialized<T>(ref target!, ref initialized, ref syncLock);

/// <inheritdoc cref="LazyInitializer.EnsureInitialized{T}(ref T, ref bool, ref object, Func{T})"/>
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNull] ref object? syncLock, Func<T> valueFactory)
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock, Func<T> valueFactory)
=> LazyInitializer.EnsureInitialized<T>(ref target!, ref initialized, ref syncLock, valueFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal static TNode Copy<TNode>(this TNode node, bool copyAttributeAnnotations
{
XContainer temp = new XElement("temp");
temp.Add(node);
copy = temp.LastNode;
copy = temp.LastNode!;
temp.RemoveNodes();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/PEWriter/SigningUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static int CalculateStrongNameSignatureSize(CommonPEModuleBuilder modul

if (keySize == 0 && privateKey.HasValue)
{
keySize = privateKey.Value.Modulus.Length;
keySize = privateKey.Value.Modulus!.Length;
}

if (keySize == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ static Microsoft.CodeAnalysis.SyntaxList<TNode>.implicit operator Microsoft.Code
static Microsoft.CodeAnalysis.SyntaxList<TNode>.implicit operator Microsoft.CodeAnalysis.SyntaxList<TNode!>(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SyntaxList<TNode!>
static Microsoft.CodeAnalysis.SyntaxList<TNode>.operator !=(Microsoft.CodeAnalysis.SyntaxList<TNode!> left, Microsoft.CodeAnalysis.SyntaxList<TNode!> right) -> bool
static Microsoft.CodeAnalysis.SyntaxList<TNode>.operator ==(Microsoft.CodeAnalysis.SyntaxList<TNode!> left, Microsoft.CodeAnalysis.SyntaxList<TNode!> right) -> bool
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.GetCurrentNode<TNode>(this Microsoft.CodeAnalysis.SyntaxNode! root, TNode! node) -> TNode!
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.GetCurrentNode<TNode>(this Microsoft.CodeAnalysis.SyntaxNode! root, TNode! node) -> TNode?
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.GetCurrentNodes<TNode>(this Microsoft.CodeAnalysis.SyntaxNode! root, System.Collections.Generic.IEnumerable<TNode!>! nodes) -> System.Collections.Generic.IEnumerable<TNode!>!
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.GetCurrentNodes<TNode>(this Microsoft.CodeAnalysis.SyntaxNode! root, TNode! node) -> System.Collections.Generic.IEnumerable<TNode!>!
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.InsertNodesAfter<TRoot>(this TRoot! root, Microsoft.CodeAnalysis.SyntaxNode! nodeInList, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxNode!>! newNodes) -> TRoot!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static IEnumerable<TNode> GetCurrentNodes<TNode>(this SyntaxNode root, TN
/// </summary>
/// <param name="root">The root of the subtree containing the current node corresponding to the original tracked node.</param>
/// <param name="node">The node instance originally tracked.</param>
public static TNode GetCurrentNode<TNode>(this SyntaxNode root, TNode node)
public static TNode? GetCurrentNode<TNode>(this SyntaxNode root, TNode node)
where TNode : SyntaxNode
{
return GetCurrentNodes(root, node).SingleOrDefault();
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/TreeDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public TreeDumperNode(string text) : this(text, null, null) { }
public object? Value { get; }
public string Text { get; }
public IEnumerable<TreeDumperNode> Children { get; }
public TreeDumperNode this[string child]
public TreeDumperNode? this[string child]
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ where node.IsKind(SyntaxKind.IdentifierName)
{
if (ctor.Initializer != null)
{
bodyTokens = ctor.Initializer.DescendantTokens().Concat(bodyTokens);
bodyTokens = ctor.Initializer.DescendantTokens().Concat(bodyTokens ?? Enumerable.Empty<SyntaxToken>());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected override async Task FixAllAsync(
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var spanNodes = diagnostics.SelectAsArray(
d => root.FindNode(d.Location.SourceSpan, getInnermostNodeForTie: true)
.GetAncestorsOrThis<TExpressionSyntax>().FirstOrDefault());
.GetAncestorsOrThis<TExpressionSyntax>().First());

await editor.ApplyExpressionLevelSemanticEditsAsync(
document, spanNodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ await FixReferencesAsync(document, changeNamespaceService, addImportService, ref

var fixedDocument = editor.GetChangedDocument();
root = await fixedDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var result = (fixedDocument, containers.SelectAsArray(c => root.GetCurrentNode(c)));
var result = (fixedDocument, containers.SelectAsArray(c => root.GetCurrentNode(c)
?? throw new InvalidOperationException("Can't get SyntaxNode from GetCurrentNode.")));

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ImmutableArray<CompletionItem> GetItemsFromCacheResult(GetCacheResult cacheResul
{
var compilation = await referencedProject.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);
var assembly = SymbolFinder.FindSimilarSymbols(compilation.Assembly, currentCompilation, cancellationToken).SingleOrDefault();
var metadataReference = currentCompilation.GetMetadataReference(assembly);
var metadataReference = assembly != null ? currentCompilation.GetMetadataReference(assembly) : null;

if (HasGlobalAlias(metadataReference))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private async Task<Document> AddAllSymbolInitializationsAsync(
IBlockOperation? currentBlockStatementOpt = null;
if (blockStatementOpt != null)
{
currentBlockStatementOpt = (IBlockOperation?)currentSemanticModel.GetOperation(currentRoot.GetCurrentNode(blockStatementOpt.Syntax), cancellationToken);
currentBlockStatementOpt = (IBlockOperation?)currentSemanticModel.GetOperation(currentRoot.GetCurrentNode(blockStatementOpt.Syntax)!, cancellationToken);
if (currentBlockStatementOpt == null)
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async Task<MetadataAsSourceFile> GetGeneratedFileAsync(Project project, I

// Create the directory. It's possible a parallel deletion is happening in another process, so we may have
// to retry this a few times.
var directoryToCreate = Path.GetDirectoryName(fileInfo.TemporaryFilePath);
var directoryToCreate = Path.GetDirectoryName(fileInfo.TemporaryFilePath)!;
while (!Directory.Exists(directoryToCreate))
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static bool HasGetPrefix(string text)
private static bool HasPrefix(string text, string prefix)
=> text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && text.Length > prefix.Length && !char.IsLower(text[prefix.Length]);

private static IMethodSymbol FindSetMethod(IMethodSymbol getMethod)
private static IMethodSymbol? FindSetMethod(IMethodSymbol getMethod)
{
var containingType = getMethod.ContainingType;
var setMethodName = "Set" + getMethod.Name[GetPrefix.Length..];
Expand Down
8 changes: 6 additions & 2 deletions src/Workspaces/Core/Portable/Editing/SyntaxEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ public ReplaceChange(
public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
{
var current = root.GetCurrentNode(this.Node);
Contract.ThrowIfNull(current, $"GetCurrentNode returned null with the following node: {this.Node}");

var newNode = _modifier(current, generator);
newNode = _editor.ApplyTrackingToNewNode(newNode);

Contract.ThrowIfNull(current, $"GetCurrentNode returned null with the following node: {this.Node}");
return generator.ReplaceNode(root, current, newNode);
}
}
Expand All @@ -342,6 +342,8 @@ public ReplaceWithCollectionChange(
public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
{
var current = root.GetCurrentNode(this.Node);
Contract.ThrowIfNull(current, $"GetCurrentNode returned null with the following node: {this.Node}");

var newNodes = _modifier(current, generator).ToList();
for (var i = 0; i < newNodes.Count; i++)
{
Expand Down Expand Up @@ -373,6 +375,8 @@ public ReplaceChange(
public override SyntaxNode Apply(SyntaxNode root, SyntaxGenerator generator)
{
var current = root.GetCurrentNode(this.Node);
Contract.ThrowIfNull(current, $"GetCurrentNode returned null with the following node: {this.Node}");

var newNode = _modifier(current, generator, _argument);
newNode = _editor.ApplyTrackingToNewNode(newNode);
return generator.ReplaceNode(root, current, newNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private static async Task CascadeBetweenAnonymousFunctionParametersAsync(
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

var lambdaNode = parameter.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).FirstOrDefault();
var lambdaNode = parameter.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).First();
var convertedType = semanticModel.GetTypeInfo(lambdaNode, cancellationToken).ConvertedType;

if (convertedType != null)
Expand Down Expand Up @@ -176,7 +176,7 @@ private static void CascadeBetweenAnonymousFunctionParameters(
SignatureComparer.Instance.HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(parameter.ContainingSymbol, symbol.ContainingSymbol, syntaxFacts.IsCaseSensitive) &&
ParameterNamesMatch(syntaxFacts, (IMethodSymbol)parameter.ContainingSymbol, (IMethodSymbol)symbol.ContainingSymbol))
{
var lambdaNode = symbol.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).FirstOrDefault();
var lambdaNode = symbol.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).First();
var convertedType2 = semanticModel.GetTypeInfo(lambdaNode, cancellationToken).ConvertedType;

if (convertedType1.Equals(convertedType2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private async Task<bool> CheckForConflictAsync(
hasConflict = true;

var newLocationTasks = newReferencedSymbols.Select(async symbol => await GetSymbolLocationAsync(solution, symbol, _cancellationToken).ConfigureAwait(false));
var newLocations = (await Task.WhenAll(newLocationTasks).ConfigureAwait(false)).Where(loc => loc != null && loc.IsInSource);
var newLocations = (await Task.WhenAll(newLocationTasks).ConfigureAwait(false)).WhereNotNull().Where(loc => loc.IsInSource);
foreach (var originalReference in conflictAnnotation.RenameDeclarationLocationReferences.Where(loc => loc.IsSourceLocation))
{
var adjustedStartPosition = conflictResolution.GetAdjustedTokenStartingPosition(originalReference.TextSpan.Start, originalReference.DocumentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private static string GetString(ISymbol symbol)
/// <summary>
/// Gives the First Location for a given Symbol by ordering the locations using DocumentId first and Location starting position second
/// </summary>
private static async Task<Location> GetSymbolLocationAsync(Solution solution, ISymbol symbol, CancellationToken cancellationToken)
private static async Task<Location?> GetSymbolLocationAsync(Solution solution, ISymbol symbol, CancellationToken cancellationToken)
{
var locations = symbol.Locations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private static XNode[] RewriteMany(ISymbol symbol, HashSet<ISymbol>? visitedSymb
string xpathValue;
if (string.IsNullOrEmpty(pathAttribute?.Value))
{
xpathValue = BuildXPathForElement(element.Parent);
xpathValue = BuildXPathForElement(element.Parent!);
}
else
{
Expand Down Expand Up @@ -571,7 +571,7 @@ private static TNode Copy<TNode>(TNode node, bool copyAttributeAnnotations)
{
XContainer temp = new XElement("temp");
temp.Add(node);
copy = temp.LastNode;
copy = temp.LastNode!;
temp.RemoveNodes();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ await TryCreatePersistentStorageAsync(solutionKey, workingFolderPath).ConfigureA
// this was not a normal exception that we expected during DB open.
// Report this so we can try to address whatever is causing this.
FatalError.ReportAndCatch(ex);
IOUtilities.PerformIO(() => Directory.Delete(Path.GetDirectoryName(databaseFilePath), recursive: true));
IOUtilities.PerformIO(() => Directory.Delete(Path.GetDirectoryName(databaseFilePath)!, recursive: true));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public void Dispose()
private static void EnsureDirectory(string databaseFilePath)
{
var directory = Path.GetDirectoryName(databaseFilePath);
Contract.ThrowIfNull(directory);

if (Directory.Exists(directory))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public static ISet<SyntaxKind> GetPrecedingModifiers(
return result;
}

public static TypeDeclarationSyntax GetContainingTypeDeclaration(
public static TypeDeclarationSyntax? GetContainingTypeDeclaration(
this SyntaxTree syntaxTree, int position, CancellationToken cancellationToken)
{
return syntaxTree.GetContainingTypeDeclarations(position, cancellationToken).FirstOrDefault();
}

public static BaseTypeDeclarationSyntax GetContainingTypeOrEnumDeclaration(
public static BaseTypeDeclarationSyntax? GetContainingTypeOrEnumDeclaration(
this SyntaxTree syntaxTree, int position, CancellationToken cancellationToken)
{
return syntaxTree.GetContainingTypeOrEnumDeclarations(position, cancellationToken).FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public XElement ToXElement() =>
new XAttribute(nameof(Value), GetValueForSerialization()),
new XAttribute(nameof(DiagnosticSeverity), Notification.Severity.ToDiagnosticSeverity() ?? DiagnosticSeverity.Hidden));

private object? GetValueForSerialization()
private object GetValueForSerialization()
{
if (typeof(T) == typeof(string))
{
return Value;
return Value!;
}
else if (typeof(T) == typeof(bool))
{
return Value;
return Value!;
}
else if (IsZeroOrOneValueOfEnum())
{
Expand Down Expand Up @@ -133,7 +133,7 @@ public static CodeStyleOption2<T> FromXElement(XElement element)
var typeAttribute = element.Attribute("Type");
var valueAttribute = element.Attribute(nameof(Value));
var severityAttribute = element.Attribute(nameof(DiagnosticSeverity));
var version = (int)element.Attribute(nameof(SerializationVersion));
var version = (int?)element.Attribute(nameof(SerializationVersion));

if (typeAttribute == null || valueAttribute == null || severityAttribute == null)
{
Expand Down
Loading

0 comments on commit 0aaa2eb

Please sign in to comment.