Skip to content

Commit

Permalink
Enable nullable for classes under Rename folder (#62453)
Browse files Browse the repository at this point in the history
* Enable nullable

* Clean up

* Enable other nullable

* Revert "Enable nullable"

This reverts commit 0cebdff.

* Use IsSuccessful flag to handle different cases
  • Loading branch information
Cosifne committed Jul 9, 2022
1 parent 515695e commit 7fc34f9
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public async Task<IInlineRenameReplacementInfo> GetReplacementsAsync(string repl
var conflicts = await _renameLocationSet.ResolveConflictsAsync(
_renameInfo.GetFinalSymbolName(replacementText), nonConflictSymbols: null, cancellationToken: cancellationToken).ConfigureAwait(false);

Contract.ThrowIfTrue(conflicts.ErrorMessage != null);

return new InlineRenameReplacementInfo(conflicts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ private class InlineRenameReplacementInfo : IInlineRenameReplacementInfo
private readonly ConflictResolution _conflicts;

public InlineRenameReplacementInfo(ConflictResolution conflicts)
=> _conflicts = conflicts;
{
Contract.ThrowIfFalse(conflicts.IsSuccessful);
_conflicts = conflicts;
}

public IEnumerable<DocumentId> DocumentIds => _conflicts.DocumentIds;

public Solution NewSolution => _conflicts.NewSolution;
public Solution NewSolution => _conflicts.NewSolution!;

public bool ReplacementTextValid => _conflicts.ReplacementTextValid;
public bool ReplacementTextValid => _conflicts.ReplacementTextValid!.Value;

public IEnumerable<InlineRenameReplacement> GetReplacements(DocumentId documentId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<IInlineRenameInfo> GetRenameInfoAsync(Document document, int p
return new FailureInlineRenameInfo(EditorFeaturesResources.You_must_rename_an_identifier);

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var semanticFacts = document.GetLanguageService<ISemanticFactsService>();
var semanticFacts = document.GetRequiredLanguageService<ISemanticFactsService>();

var tokenRenameInfo = RenameUtilities.GetTokenRenameInfo(semanticFacts, semanticModel, triggerToken, cancellationToken);

Expand Down
1 change: 1 addition & 0 deletions src/EditorFeatures/Test2/Rename/RenameEngineResult.vb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
Dim result = GetConflictResolution(renameTo, workspace.CurrentSolution, symbol, renameOptions, host)

If expectFailure Then
Assert.False(result.IsSuccessful)
Assert.NotNull(result.ErrorMessage)
Return engineResult
Else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private async Task<Solution> EncapsulateFieldsInCurrentProcessAsync(Document doc
var resolution = await initialLocations.Filter(filter).ResolveConflictsAsync(
finalName, nonConflictSymbols: null, cancellationToken).ConfigureAwait(false);

Contract.ThrowIfTrue(resolution.ErrorMessage != null);
Contract.ThrowIfFalse(resolution.IsSuccessful);

return resolution.NewSolution;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private async Task<Solution> ProcessResultAsync(CodeFixContext context, Diagnost
nonConflictSymbols: ImmutableHashSet.Create<ISymbol>(propertySymbol),
cancellationToken).ConfigureAwait(false);

Contract.ThrowIfTrue(resolution.ErrorMessage != null);
Contract.ThrowIfFalse(resolution.IsSuccessful);

solution = resolution.NewSolution;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp
{
internal static class OmniSharpRenamer
{
public readonly record struct RenameResult(Solution Solution, string? ErrorMessage);
public readonly record struct RenameResult(Solution? Solution, string? ErrorMessage);

public static async Task<RenameResult> RenameSymbolAsync(
Solution solution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Runtime.Serialization;
using Microsoft.CodeAnalysis.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public async Task<MutableConflictResolution> ResolveConflictsAsync()
{
var definitionLocations = _renameLocationSet.Symbol.Locations;
var definitionDocuments = definitionLocations
.Select(l => conflictResolution.OldSolution.GetDocument(l.SourceTree))
.Select(l => conflictResolution.OldSolution.GetRequiredDocument(l.SourceTree))
.Distinct();

if (definitionDocuments.Count() == 1 && _replacementTextValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,28 @@ internal static partial class ConflictResolver
ImmutableHashSet<ISymbol>? nonConflictSymbols,
CancellationToken cancellationToken)
{
// when someone e.g. renames a symbol from metadata through the API (IDE blocks this), we need to return
var renameSymbolDeclarationLocation = renameLocationSet.Symbol.Locations.Where(loc => loc.IsInSource).FirstOrDefault();
if (renameSymbolDeclarationLocation == null)
{
// Symbol "{0}" is not from source.
return new ConflictResolution(string.Format(WorkspacesResources.Symbol_0_is_not_from_source, renameLocationSet.Symbol.Name));
}

var resolution = await ResolveMutableConflictsAsync(
renameLocationSet, replacementText, nonConflictSymbols, cancellationToken).ConfigureAwait(false);
renameLocationSet, renameSymbolDeclarationLocation, replacementText, nonConflictSymbols, cancellationToken).ConfigureAwait(false);

return resolution.ToConflictResolution();
}

private static Task<MutableConflictResolution> ResolveMutableConflictsAsync(
RenameLocations renameLocationSet,
Location renameSymbolDeclarationLocation,
string replacementText,
ImmutableHashSet<ISymbol>? nonConflictSymbols,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

// when someone e.g. renames a symbol from metadata through the API (IDE blocks this), we need to return
var renameSymbolDeclarationLocation = renameLocationSet.Symbol.Locations.Where(loc => loc.IsInSource).FirstOrDefault();
if (renameSymbolDeclarationLocation == null)
{
// Symbol "{0}" is not from source.
return Task.FromResult(new MutableConflictResolution(string.Format(WorkspacesResources.Symbol_0_is_not_from_source, renameLocationSet.Symbol.Name)));
}

var session = new Session(
renameLocationSet, renameSymbolDeclarationLocation,
replacementText, nonConflictSymbols, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Generic;

namespace Microsoft.CodeAnalysis.Rename.ConflictEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -22,7 +20,7 @@ public static ImmutableArray<Location> GetMembersWithConflictingSignatures(IMeth
.Where(m => !m.Equals(renamedMethod) && m.Arity == renamedMethod.Arity);

return GetConflictLocations(renamedMethod, potentiallyConflictingMethods, isMethod: true,
(method) => GetAllSignatures((method as IMethodSymbol).Parameters, trimOptionalParameters));
method => GetAllSignatures(((IMethodSymbol)method).Parameters, trimOptionalParameters));
}

public static ImmutableArray<Location> GetMembersWithConflictingSignatures(IPropertySymbol renamedProperty, bool trimOptionalParameters)
Expand All @@ -33,7 +31,7 @@ public static ImmutableArray<Location> GetMembersWithConflictingSignatures(IProp
.Where(m => !m.Equals(renamedProperty) && m.Parameters.Length == renamedProperty.Parameters.Length);

return GetConflictLocations(renamedProperty, potentiallyConflictingProperties, isMethod: false,
(property) => GetAllSignatures((property as IPropertySymbol).Parameters, trimOptionalParameters));
property => GetAllSignatures(((IPropertySymbol)property).Parameters, trimOptionalParameters));
}

private static ImmutableArray<Location> GetConflictLocations(ISymbol renamedMember,
Expand All @@ -57,10 +55,8 @@ public static ImmutableArray<Location> GetMembersWithConflictingSignatures(IProp
{
if (signatureToConflictingMember.TryGetValue(signature, out var conflictingSymbol))
{
if (isMethod)
if (isMethod && conflictingSymbol is IMethodSymbol conflictingMethod && renamedMember is IMethodSymbol renamedMethod)
{
var conflictingMethod = conflictingSymbol as IMethodSymbol;
var renamedMethod = renamedMember as IMethodSymbol;
if (!(conflictingMethod.PartialDefinitionPart != null && Equals(conflictingMethod.PartialDefinitionPart, renamedMethod)) &&
!(conflictingMethod.PartialImplementationPart != null && Equals(conflictingMethod.PartialImplementationPart, renamedMethod)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -22,8 +20,6 @@ namespace Microsoft.CodeAnalysis.Rename.ConflictEngine
/// </summary>
internal sealed class MutableConflictResolution
{
public readonly string ErrorMessage;

// Used to map spans from oldSolution to the newSolution
private readonly RenamedSpansTracker _renamedSpansTracker;

Expand Down Expand Up @@ -53,9 +49,6 @@ internal sealed class MutableConflictResolution

private (DocumentId documentId, string newName) _renamedDocument;

public MutableConflictResolution(string errorMessage)
=> ErrorMessage = errorMessage;

public MutableConflictResolution(
Solution oldSolution,
RenamedSpansTracker renamedSpansTracker,
Expand Down Expand Up @@ -89,8 +82,8 @@ internal void UpdateCurrentSolution(Solution solution)
{
if (_renamedSpansTracker.IsDocumentChanged(documentId))
{
var document = CurrentSolution.GetDocument(documentId);
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var document = CurrentSolution.GetRequiredDocument(documentId);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

// For the computeReplacementToken and computeReplacementNode functions, use
// the "updated" node to maintain any annotation removals from descendants.
Expand Down Expand Up @@ -161,9 +154,6 @@ internal void AddOrReplaceRelatedLocation(RelatedLocation location)

public ConflictResolution ToConflictResolution()
{
if (ErrorMessage != null)
return new ConflictResolution(ErrorMessage);

var documentIds = _renamedSpansTracker.DocumentIds.Concat(
this.RelatedLocations.Select(l => l.DocumentId)).Distinct().ToImmutableArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -52,7 +50,7 @@ public RelatedLocation(TextSpan conflictCheckSpan, DocumentId documentId, Relate
public RelatedLocation WithType(RelatedLocationType type)
=> new(ConflictCheckSpan, DocumentId, type, IsReference, ComplexifiedTargetSpan);

public override bool Equals(object obj)
public override bool Equals(object? obj)
=> obj is RelatedLocation location && Equals(location);

public bool Equals(RelatedLocation other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;

namespace Microsoft.CodeAnalysis.Rename.ConflictEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -12,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -48,7 +47,7 @@ internal void AddComplexifiedSpan(DocumentId documentId, TextSpan oldSpan, TextS
_documentToComplexifiedSpansMap[documentId] = spans;
}

spans.Add(new MutableComplexifiedSpan() { OriginalSpan = oldSpan, NewSpan = newSpan, ModifiedSubSpans = modifiedSubSpans });
spans.Add(new MutableComplexifiedSpan(originalSpan: oldSpan, newSpan: newSpan, modifiedSubSpans: modifiedSubSpans));
}

// Given a position in the old solution, we get back the new adjusted position
Expand Down Expand Up @@ -124,6 +123,14 @@ private class MutableComplexifiedSpan
public TextSpan OriginalSpan;
public TextSpan NewSpan;
public List<(TextSpan oldSpan, TextSpan newSpan)> ModifiedSubSpans;

public MutableComplexifiedSpan(
TextSpan originalSpan, TextSpan newSpan, List<(TextSpan oldSpan, TextSpan newSpan)> modifiedSubSpans)
{
OriginalSpan = originalSpan;
NewSpan = newSpan;
ModifiedSubSpans = modifiedSubSpans;
}
}

internal void ClearDocuments(IEnumerable<DocumentId> conflictLocationDocumentIds)
Expand All @@ -149,7 +156,7 @@ internal async Task<Solution> SimplifyAsync(Solution solution, IEnumerable<Docum
{
if (this.IsDocumentChanged(documentId))
{
var document = solution.GetDocument(documentId);
var document = solution.GetRequiredDocument(documentId);

if (replacementTextValid)
{
Expand All @@ -171,11 +178,11 @@ internal async Task<Solution> SimplifyAsync(Solution solution, IEnumerable<Docum
complexifiedSpans.Clear();
}

var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

// First, get all the complexified statements
var nodeAnnotations = renameAnnotations.GetAnnotatedNodesAndTokens<RenameNodeSimplificationAnnotation>(root)
.Select(x => Tuple.Create(renameAnnotations.GetAnnotations<RenameNodeSimplificationAnnotation>(x).First(), (SyntaxNode)x));
.Select(x => Tuple.Create(renameAnnotations.GetAnnotations<RenameNodeSimplificationAnnotation>(x).First(), (SyntaxNode)x!));

var modifiedTokensInComplexifiedStatements = new HashSet<SyntaxToken>();
foreach (var annotationAndNode in nodeAnnotations)
Expand Down
Loading

0 comments on commit 7fc34f9

Please sign in to comment.