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

Support converting to collection expression when overloads would not change #75264

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -41,5 +41,12 @@ protected override bool AreCollectionExpressionsSupported(Compilation compilatio
=> compilation.LanguageVersion().SupportsCollectionExpressions();

protected override bool CanUseCollectionExpression(SemanticModel semanticModel, BaseObjectCreationExpressionSyntax objectCreationExpression, INamedTypeSymbol? expressionType, bool allowSemanticsChange, CancellationToken cancellationToken, out bool changesSemantics)
=> UseCollectionExpressionHelpers.CanReplaceWithCollectionExpression(semanticModel, objectCreationExpression, expressionType, isSingletonInstance: false, allowSemanticsChange, skipVerificationForReplacedNode: true, cancellationToken, out changesSemantics);
{
// Synthesize the final collection expression we would replace this object-creation with. That will allow us to
// determine if we end up calling the right overload in cases of overloaded methods.
var replacement = UseCollectionExpressionHelpers.CreateReplacementCollectionExpressionForAnalysis(objectCreationExpression.Initializer);

return UseCollectionExpressionHelpers.CanReplaceWithCollectionExpression(
semanticModel, objectCreationExpression, replacement, expressionType, isSingletonInstance: false, allowSemanticsChange, skipVerificationForReplacedNode: true, cancellationToken, out changesSemantics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5719,4 +5719,58 @@ void M()
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/73362")]
public async Task TestWithOverloadResolution1()
{
await new VerifyCS.Test
{
TestCode = """
using System.Collections.Generic;

class C
{
public void Test(Class1 param1, Class1 param2)
{
MethodTakingEnumerable([|new|] List<Class1> { param1, param2 });
}

public void MethodTakingEnumerable(IEnumerable<Class1> param)
{
}

public void MethodTakingEnumerable(IEnumerable<Class2> param)
{
}

public class Class1 { }
public class Class2 { }
}
""",
FixedCode = """
using System.Collections.Generic;

class C
{
public void Test(Class1 param1, Class1 param2)
{
MethodTakingEnumerable([param1, param2]);
}

public void MethodTakingEnumerable(IEnumerable<Class1> param)
{
}

public void MethodTakingEnumerable(IEnumerable<Class2> param)
{
}

public class Class1 { }
public class Class2 { }
}
""",
LanguageVersion = LanguageVersion.CSharp12,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}
}
Loading