Skip to content
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

Do not lift type parameters in extract method declared within the selected region #76724

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -9092,4 +9092,35 @@ private static bool NewMethod(int v)
""",
options: NoBraces());
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/22597")]
public async Task TestFullyExtractedTypeParameter()
{
await TestInRegularAndScriptAsync(
"""
class C
{
private void Test()
{
[|void Goo<T>(T bar) => Console.WriteLine(bar);
Goo(3);|]
}
}
""",
"""
class C
{
private void Test()
{
{|Rename:NewMethod|}();
}

private static void NewMethod()
{
void Goo<T>(T bar) => Console.WriteLine(bar);
Goo(3);
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,13 @@ private static bool IsInteractiveSynthesizedParameter(ISymbol localOrParameter)
parameter.ContainingSymbol.ContainingType.IsScriptClass;
}

private static void AddTypeParametersToMap(IEnumerable<ITypeParameterSymbol> typeParameters, IDictionary<int, ITypeParameterSymbol> sortedMap)
private void AddTypeParametersToMap(IEnumerable<ITypeParameterSymbol> typeParameters, IDictionary<int, ITypeParameterSymbol> sortedMap)
{
foreach (var typeParameter in typeParameters)
{
AddTypeParameterToMap(typeParameter, sortedMap);
}
}

private static void AddTypeParameterToMap(ITypeParameterSymbol typeParameter, IDictionary<int, ITypeParameterSymbol> sortedMap)
private void AddTypeParameterToMap(ITypeParameterSymbol typeParameter, IDictionary<int, ITypeParameterSymbol> sortedMap)
{
if (typeParameter == null ||
typeParameter.DeclaringMethod == null ||
Expand All @@ -732,6 +730,10 @@ private static void AddTypeParameterToMap(ITypeParameterSymbol typeParameter, ID
return;
}

var selectionSpan = this.SelectionResult.FinalSpan;
if (typeParameter.Locations is not [var location] || selectionSpan.Contains(location.SourceSpan))
return;

sortedMap[typeParameter.Ordinal] = typeParameter;
}

Expand All @@ -741,28 +743,19 @@ private void AppendMethodTypeVariableFromDataFlowAnalysis(
{
foreach (var symbol in variableInfoMap.Keys)
{
switch (symbol)
var type = symbol switch
{
case IParameterSymbol parameter:
AddTypeParametersToMap(TypeParameterCollector.Collect(parameter.Type), sortedMap);
continue;

case ILocalSymbol local:
AddTypeParametersToMap(TypeParameterCollector.Collect(local.Type), sortedMap);
continue;

case IRangeVariableSymbol rangeVariable:
var type = GetRangeVariableType(rangeVariable);
AddTypeParametersToMap(TypeParameterCollector.Collect(type), sortedMap);
continue;
IParameterSymbol parameter => parameter.Type,
ILocalSymbol local => local.Type,
IRangeVariableSymbol rangeVariable => GetRangeVariableType(rangeVariable),
_ => throw ExceptionUtilities.UnexpectedValue(symbol),
};

default:
throw ExceptionUtilities.UnexpectedValue(symbol);
}
AddTypeParametersToMap(TypeParameterCollector.Collect(type), sortedMap);
}
}

private static void AppendMethodTypeParameterFromConstraint(SortedDictionary<int, ITypeParameterSymbol> sortedMap)
private void AppendMethodTypeParameterFromConstraint(SortedDictionary<int, ITypeParameterSymbol> sortedMap)
{
var typeParametersInConstraint = new List<ITypeParameterSymbol>();

Expand All @@ -771,9 +764,7 @@ private static void AppendMethodTypeParameterFromConstraint(SortedDictionary<int
{
var constraintTypes = typeParameter.ConstraintTypes;
if (constraintTypes.IsDefaultOrEmpty)
{
continue;
}

foreach (var type in constraintTypes)
{
Expand All @@ -784,21 +775,13 @@ private static void AppendMethodTypeParameterFromConstraint(SortedDictionary<int

// pick up only valid type parameter and add them to the map
foreach (var typeParameter in typeParametersInConstraint)
{
AddTypeParameterToMap(typeParameter, sortedMap);
}
}

private static void AppendMethodTypeParameterUsedDirectly(MultiDictionary<ISymbol, SyntaxToken> symbolMap, IDictionary<int, ITypeParameterSymbol> sortedMap)
private void AppendMethodTypeParameterUsedDirectly(MultiDictionary<ISymbol, SyntaxToken> symbolMap, IDictionary<int, ITypeParameterSymbol> sortedMap)
{
foreach (var typeParameter in symbolMap.Keys.OfType<ITypeParameterSymbol>())
{
if (typeParameter.DeclaringMethod != null &&
!sortedMap.ContainsKey(typeParameter.Ordinal))
{
sortedMap[typeParameter.Ordinal] = typeParameter;
}
}
AddTypeParameterToMap(typeParameter, sortedMap);
Copy link
Member Author

Choose a reason for hiding this comment

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

above checks are handled in this method.

}

private ImmutableArray<ITypeParameterSymbol> GetMethodTypeParametersInConstraintList(
Expand All @@ -816,7 +799,7 @@ private ImmutableArray<ITypeParameterSymbol> GetMethodTypeParametersInConstraint
return [.. sortedMap.Values];
}

private static void AppendTypeParametersInConstraintsUsedByConstructedTypeWithItsOwnConstraints(SortedDictionary<int, ITypeParameterSymbol> sortedMap)
private void AppendTypeParametersInConstraintsUsedByConstructedTypeWithItsOwnConstraints(SortedDictionary<int, ITypeParameterSymbol> sortedMap)
{
using var _1 = PooledHashSet<ITypeSymbol>.GetInstance(out var visited);
using var _2 = PooledHashSet<ITypeParameterSymbol>.GetInstance(out var candidates);
Expand Down Expand Up @@ -877,7 +860,7 @@ private static void AddTypeParametersInConstraintsUsedByConstructedTypeWithItsOw
}
}

private static ImmutableArray<ITypeParameterSymbol> GetMethodTypeParametersInDeclaration(ITypeSymbol returnType, SortedDictionary<int, ITypeParameterSymbol> sortedMap)
private ImmutableArray<ITypeParameterSymbol> GetMethodTypeParametersInDeclaration(ITypeSymbol returnType, SortedDictionary<int, ITypeParameterSymbol> sortedMap)
{
// add return type to the map
AddTypeParametersToMap(TypeParameterCollector.Collect(returnType), sortedMap);
Expand Down
Loading