Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static ExpressionSyntax GenerateTupleDeclaration(ITypeSymbol typeSymbol,
var elements = ((INamedTypeSymbol)typeSymbol).TupleElements;
Debug.Assert(elements.Length == parensDesignation.Variables.Count);

using var builderDisposer = ArrayBuilder<SyntaxNode>.GetInstance(elements.Length, out var builder);
using var builderDisposer = ArrayBuilder<ArgumentSyntax>.GetInstance(elements.Length, out var builder);
for (var i = 0; i < elements.Length; i++)
{
var designation = parensDesignation.Variables[i];
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,24 @@ public virtual SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)

public virtual SyntaxList<TNode> VisitList<TNode>(SyntaxList<TNode> list) where TNode : SyntaxNode
{
SyntaxListBuilder? alternate = null;
SyntaxListBuilder<TNode> alternate = default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change primarily related to the stated goal of the PR? If not, please consider separating it into its own PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is related. SyntaxListBuilder produced a SyntaxList<SyntaxNode> which needs an explicit downcast. SyntaxListBuilder<TNode> produces SyntaxList<TNode> and removes the need for a downcast.

for (int i = 0, n = list.Count; i < n; i++)
{
var item = list[i];
var visited = this.VisitListElement(item);
if (item != visited && alternate == null)
if (item != visited && alternate.IsNull)
{
alternate = new SyntaxListBuilder(n);
alternate = new SyntaxListBuilder<TNode>(n);
alternate.AddRange(list, 0, i);
}

if (alternate != null && visited != null && !visited.IsKind(SyntaxKind.None))
if (!alternate.IsNull && visited != null && !visited.IsKind(SyntaxKind.None))
{
alternate.Add(visited);
}
}

if (alternate != null)
if (!alternate.IsNull)
{
return alternate.ToList();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*REMOVED*override abstract Microsoft.CodeAnalysis.Diagnostic.Equals(object? obj) -> bool
*REMOVED*static Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode>.implicit operator Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>(Microsoft.CodeAnalysis.SeparatedSyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>
*REMOVED*static Microsoft.CodeAnalysis.SyntaxList<TNode>.implicit operator Microsoft.CodeAnalysis.SyntaxList<TNode!>(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SyntaxList<TNode!>
abstract Microsoft.CodeAnalysis.SymbolVisitor<TArgument, TResult>.DefaultResult.get -> TResult
Microsoft.CodeAnalysis.Compilation.GetTypesByMetadataName(string! fullyQualifiedMetadataName) -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.INamedTypeSymbol!>
Microsoft.CodeAnalysis.IFieldSymbol.IsRequired.get -> bool
Expand Down Expand Up @@ -58,6 +60,10 @@ override sealed Microsoft.CodeAnalysis.Diagnostic.Equals(object? obj) -> bool
*REMOVED*static Microsoft.CodeAnalysis.SyntaxNodeExtensions.ReplaceSyntax<TRoot>(this TRoot! root, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxNode!>! nodes, System.Func<Microsoft.CodeAnalysis.SyntaxNode!, Microsoft.CodeAnalysis.SyntaxNode!, Microsoft.CodeAnalysis.SyntaxNode!>! computeReplacementNode, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxToken>! tokens, System.Func<Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken>! computeReplacementToken, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxTrivia>! trivia, System.Func<Microsoft.CodeAnalysis.SyntaxTrivia, Microsoft.CodeAnalysis.SyntaxTrivia, Microsoft.CodeAnalysis.SyntaxTrivia>! computeReplacementTrivia) -> TRoot!
static Microsoft.CodeAnalysis.Emit.EditAndContinueMethodDebugInformation.Create(System.Collections.Immutable.ImmutableArray<byte> compressedSlotMap, System.Collections.Immutable.ImmutableArray<byte> compressedLambdaMap, System.Collections.Immutable.ImmutableArray<byte> compressedStateMachineStateMap) -> Microsoft.CodeAnalysis.Emit.EditAndContinueMethodDebugInformation
static Microsoft.CodeAnalysis.ModuleMetadata.CreateFromMetadata(System.IntPtr metadata, int size, System.IDisposable! owner, bool disposeOwner) -> Microsoft.CodeAnalysis.ModuleMetadata!
static Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode>.explicit operator Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>(Microsoft.CodeAnalysis.SeparatedSyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>
static Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode>.op_Implicit(Microsoft.CodeAnalysis.SeparatedSyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>
static Microsoft.CodeAnalysis.SyntaxList<TNode>.explicit operator Microsoft.CodeAnalysis.SyntaxList<TNode!>(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SyntaxList<TNode!>
static Microsoft.CodeAnalysis.SyntaxList<TNode>.op_Implicit(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SyntaxList<TNode!>
static Microsoft.CodeAnalysis.SyntaxNodeExtensions.ReplaceSyntax<TRoot>(this TRoot! root, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxNode!>? nodes, System.Func<Microsoft.CodeAnalysis.SyntaxNode!, Microsoft.CodeAnalysis.SyntaxNode!, Microsoft.CodeAnalysis.SyntaxNode!>? computeReplacementNode, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxToken>? tokens, System.Func<Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken>? computeReplacementToken, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxTrivia>? trivia, System.Func<Microsoft.CodeAnalysis.SyntaxTrivia, Microsoft.CodeAnalysis.SyntaxTrivia, Microsoft.CodeAnalysis.SyntaxTrivia>? computeReplacementTrivia) -> TRoot!
const Microsoft.CodeAnalysis.WellKnownMemberNames.CheckedDecrementOperatorName = "op_CheckedDecrement" -> string!
const Microsoft.CodeAnalysis.WellKnownMemberNames.CheckedIncrementOperatorName = "op_CheckedIncrement" -> string!
Expand Down
10 changes: 9 additions & 1 deletion src/Compilers/Core/Portable/Syntax/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -600,7 +601,14 @@ public static implicit operator SeparatedSyntaxList<SyntaxNode>(SeparatedSyntaxL
return new SeparatedSyntaxList<SyntaxNode>(nodes._list);
}

public static implicit operator SeparatedSyntaxList<TNode>(SeparatedSyntaxList<SyntaxNode> nodes)
[Obsolete("This method is preserved for binary compatibility only. Use explicit cast instead.", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static SeparatedSyntaxList<TNode> op_Implicit(SeparatedSyntaxList<SyntaxNode> nodes)
{
return new SeparatedSyntaxList<TNode>(nodes._list);
}

public static explicit operator SeparatedSyntaxList<TNode>(SeparatedSyntaxList<SyntaxNode> nodes)
{
return new SeparatedSyntaxList<TNode>(nodes._list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public bool Any(int kind)

public SyntaxList<TNode> ToList()
{
return _builder.ToList();
return (SyntaxList<TNode>)_builder.ToList();
}

public static implicit operator SyntaxListBuilder?(SyntaxListBuilder<TNode> builder)
Expand Down
10 changes: 9 additions & 1 deletion src/Compilers/Core/Portable/Syntax/SyntaxList`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Syntax;
Expand Down Expand Up @@ -450,7 +451,9 @@ public override int GetHashCode()
return _node?.GetHashCode() ?? 0;
}

public static implicit operator SyntaxList<TNode>(SyntaxList<SyntaxNode> nodes)
[Obsolete("This method is preserved for binary compatibility only. Use explicit cast instead.", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static SyntaxList<TNode> op_Implicit(SyntaxList<SyntaxNode> nodes)
{
return new SyntaxList<TNode>(nodes._node);
}
Expand All @@ -460,6 +463,11 @@ public static implicit operator SyntaxList<SyntaxNode>(SyntaxList<TNode> nodes)
return new SyntaxList<SyntaxNode>(nodes.Node);
}

public static explicit operator SyntaxList<TNode>(SyntaxList<SyntaxNode> nodes)
{
return new SyntaxList<TNode>(nodes._node);
}

/// <summary>
/// The index of the node in this list, or -1 if the node is not in the list.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,17 +660,18 @@ protected override T TransferLeadingWhitespaceTrivia<T>(T newArgument, SyntaxNod
return newArgument;
}

private async Task<SeparatedSyntaxList<SyntaxNode>> AddNewArgumentsToListAsync(
private async Task<SeparatedSyntaxList<TArgumentSyntax>> AddNewArgumentsToListAsync<TArgumentSyntax>(
ISymbol declarationSymbol,
SeparatedSyntaxList<SyntaxNode> newArguments,
SeparatedSyntaxList<SyntaxNode> originalArguments,
SeparatedSyntaxList<TArgumentSyntax> newArguments,
SeparatedSyntaxList<TArgumentSyntax> originalArguments,
SignatureChange signaturePermutation,
bool isReducedExtensionMethod,
bool isParamsArrayExpanded,
bool generateAttributeArguments,
Document document,
int position,
CancellationToken cancellationToken)
where TArgumentSyntax : SyntaxNode
{
var newArgumentList = await AddNewArgumentsToListAsync(
declarationSymbol, newArguments,
Expand Down Expand Up @@ -860,25 +861,25 @@ public override async Task<ImmutableArray<ISymbol>> DetermineCascadedSymbolsFrom
protected override IEnumerable<AbstractFormattingRule> GetFormattingRules(Document document)
=> Formatter.GetDefaultFormattingRules(document).Concat(new ChangeSignatureFormattingRule());

protected override SyntaxNode AddNameToArgument(SyntaxNode newArgument, string name)
protected override TArgumentSyntax AddNameToArgument<TArgumentSyntax>(TArgumentSyntax newArgument, string name)
{
return newArgument switch
{
ArgumentSyntax a => a.WithNameColon(NameColon(name)),
AttributeArgumentSyntax a => a.WithNameColon(NameColon(name)),
ArgumentSyntax a => (TArgumentSyntax)(SyntaxNode)a.WithNameColon(NameColon(name)),
AttributeArgumentSyntax a => (TArgumentSyntax)(SyntaxNode)a.WithNameColon(NameColon(name)),
_ => throw ExceptionUtilities.UnexpectedValue(newArgument.Kind())
};
}

protected override SyntaxNode CreateExplicitParamsArrayFromIndividualArguments(SeparatedSyntaxList<SyntaxNode> newArguments, int indexInExistingList, IParameterSymbol parameterSymbol)
protected override TArgumentSyntax CreateExplicitParamsArrayFromIndividualArguments<TArgumentSyntax>(SeparatedSyntaxList<TArgumentSyntax> newArguments, int indexInExistingList, IParameterSymbol parameterSymbol)
{
RoslynDebug.Assert(parameterSymbol.IsParams);

// These arguments are part of a params array, and should not have any modifiers, making it okay to just use their expressions.
var listOfArguments = SeparatedList(newArguments.Skip(indexInExistingList).Select(a => ((ArgumentSyntax)a).Expression), newArguments.GetSeparators().Skip(indexInExistingList));
var listOfArguments = SeparatedList(newArguments.Skip(indexInExistingList).Select(a => ((ArgumentSyntax)(SyntaxNode)a).Expression), newArguments.GetSeparators().Skip(indexInExistingList));
var initializerExpression = InitializerExpression(SyntaxKind.ArrayInitializerExpression, listOfArguments);
var objectCreation = ArrayCreationExpression((ArrayTypeSyntax)parameterSymbol.Type.GenerateTypeSyntax(), initializerExpression);
return Argument(objectCreation);
return (TArgumentSyntax)(SyntaxNode)Argument(objectCreation);
}

protected override bool SupportsOptionalAndParamsArrayParametersSimultaneously()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ internal partial class CSharpIntroduceParameterCodeRefactoringProvider : Abstrac
ExpressionSyntax,
InvocationExpressionSyntax,
ObjectCreationExpressionSyntax,
IdentifierNameSyntax>
IdentifierNameSyntax,
ArgumentSyntax>
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
Expand All @@ -39,7 +40,7 @@ protected override bool IsDestructor(IMethodSymbol methodSymbol)
return false;
}

protected override SyntaxNode UpdateArgumentListSyntax(SyntaxNode argumentList, SeparatedSyntaxList<SyntaxNode> arguments)
protected override SyntaxNode UpdateArgumentListSyntax(SyntaxNode argumentList, SeparatedSyntaxList<ArgumentSyntax> arguments)
=> ((ArgumentListSyntax)argumentList).WithArguments(arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ public abstract Task<SyntaxNode> ChangeSignatureAsync(
/// For some Foo(int x, params int[] p), this helps convert the "1, 2, 3" in Foo(0, 1, 2, 3)
/// to "new int[] { 1, 2, 3 }" in Foo(0, new int[] { 1, 2, 3 });
/// </summary>
protected abstract SyntaxNode CreateExplicitParamsArrayFromIndividualArguments(SeparatedSyntaxList<SyntaxNode> newArguments, int startingIndex, IParameterSymbol parameterSymbol);
protected abstract TArgumentSyntax CreateExplicitParamsArrayFromIndividualArguments<TArgumentSyntax>(SeparatedSyntaxList<TArgumentSyntax> newArguments, int startingIndex, IParameterSymbol parameterSymbol)
where TArgumentSyntax : SyntaxNode;

protected abstract SyntaxNode AddNameToArgument(SyntaxNode argument, string name);
protected abstract TArgumentSyntax AddNameToArgument<TArgumentSyntax>(TArgumentSyntax argument, string name)
where TArgumentSyntax : SyntaxNode;

/// <summary>
/// Only some languages support:
Expand Down Expand Up @@ -749,18 +751,19 @@ protected ImmutableArray<SyntaxToken> GetSeparators<T>(SeparatedSyntaxList<T> ar
return separators.ToImmutable();
}

protected virtual async Task<SeparatedSyntaxList<SyntaxNode>> AddNewArgumentsToListAsync(
protected virtual async Task<SeparatedSyntaxList<TArgumentSyntax>> AddNewArgumentsToListAsync<TArgumentSyntax>(
ISymbol declarationSymbol,
SeparatedSyntaxList<SyntaxNode> newArguments,
SeparatedSyntaxList<TArgumentSyntax> newArguments,
SignatureChange signaturePermutation,
bool isReducedExtensionMethod,
bool isParamsArrayExpanded,
bool generateAttributeArguments,
Document document,
int position,
CancellationToken cancellationToken)
where TArgumentSyntax : SyntaxNode
{
var fullList = ArrayBuilder<SyntaxNode>.GetInstance();
var fullList = ArrayBuilder<TArgumentSyntax>.GetInstance();
var separators = ArrayBuilder<SyntaxToken>.GetInstance();

var updatedParameters = signaturePermutation.UpdatedConfiguration.ToListOfParameters();
Expand Down Expand Up @@ -814,10 +817,10 @@ protected virtual async Task<SeparatedSyntaxList<SyntaxNode>> AddNewArgumentsToL
// TODO: Need to be able to specify which kind of attribute argument it is to the SyntaxGenerator.
// https://github.com/dotnet/roslyn/issues/43354
var argument = generateAttributeArguments ?
Generator.AttributeArgument(
(TArgumentSyntax)Generator.AttributeArgument(
name: seenNamedArguments || addedParameter.CallSiteKind == CallSiteKind.ValueWithName ? addedParameter.Name : null,
expression: expression) :
Generator.Argument(
(TArgumentSyntax)Generator.Argument(
name: seenNamedArguments || addedParameter.CallSiteKind == CallSiteKind.ValueWithName ? addedParameter.Name : null,
refKind: RefKind.None,
expression: expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return;
}

if (!IsArgumentListCorrect(syntaxFactsService.GetArgumentsOfInvocationExpression(invocationSyntax), invocationSymbol, allInvocationMethods, semanticModel, syntaxFactsService, cancellationToken))
if (!IsArgumentListCorrect((SeparatedSyntaxList<TArgumentSyntax>)syntaxFactsService.GetArgumentsOfInvocationExpression(invocationSyntax), invocationSymbol, allInvocationMethods, semanticModel, syntaxFactsService, cancellationToken))
{
return;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ static bool IsValidPlaceholderToInterpolatedString(
TLiteralExpressionSyntax, TArgumentListExpressionSyntax, TInterpolationSyntax> thisInstance,
CancellationToken cancellationToken)
{
var arguments = syntaxFactsService.GetArgumentsOfInvocationExpression(invocation);
var arguments = (SeparatedSyntaxList<TArgumentSyntax>)syntaxFactsService.GetArgumentsOfInvocationExpression(invocation);
if (arguments.Count >= 2)
{
if (syntaxFactsService.GetExpressionOfArgument(GetFormatArgument(arguments, syntaxFactsService)) is TLiteralExpressionSyntax firstArgumentExpression &&
Expand Down Expand Up @@ -212,7 +212,7 @@ private async Task<Document> CreateInterpolatedStringAsync(
CancellationToken cancellationToken)
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var arguments = syntaxFactsService.GetArgumentsOfInvocationExpression(invocation);
var arguments = (SeparatedSyntaxList<TArgumentSyntax>)syntaxFactsService.GetArgumentsOfInvocationExpression(invocation);
var literalExpression = (TLiteralExpressionSyntax?)syntaxFactsService.GetExpressionOfArgument(GetFormatArgument(arguments, syntaxFactsService));
Contract.ThrowIfNull(literalExpression);
var text = literalExpression.GetFirstToken().ToString();
Expand Down
Loading