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 call IsBetterCollectionExpressionConversion for identical collection types. #72706

Merged
merged 1 commit into from
Mar 25, 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 @@ -2332,13 +2332,16 @@ private BetterResult BetterFunctionMember<TMember>(
TypeSymbol t1 = m1LeastOverriddenParameters[^1].Type;
TypeSymbol t2 = m2LeastOverriddenParameters[^1].Type;

if (IsBetterParamsCollectionType(t1, t2, ref useSiteInfo))
if (!Conversions.HasIdentityConversion(t1, t2))
{
return BetterResult.Left;
}
if (IsBetterParamsCollectionType(t2, t1, ref useSiteInfo))
{
return BetterResult.Right;
if (IsBetterParamsCollectionType(t1, t2, ref useSiteInfo))
{
return BetterResult.Left;
}
if (IsBetterParamsCollectionType(t2, t1, ref useSiteInfo))
{
return BetterResult.Right;
}
}
}
}
Expand Down Expand Up @@ -2778,6 +2781,7 @@ private bool IsBetterCollectionExpressionConversion(
TypeSymbol t2, CollectionExpressionTypeKind kind2, TypeSymbol elementType2,
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
Debug.Assert(!Conversions.HasIdentityConversion(t1, t2));

// - T1 is System.ReadOnlySpan<E1>, and T2 is System.Span<E2>, and an implicit conversion exists from E1 to E2
if (kind1 is CollectionExpressionTypeKind.ReadOnlySpan &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5723,6 +5723,52 @@ static void Test(int y, params long[] x)
);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/72696")]
public void BetterOverload_04_Ambiguity()
{
var src = """
using System.Collections;
using System.Collections.Generic;

class MyCollection<T> : IEnumerable<T>
{
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw null;
IEnumerator IEnumerable.GetEnumerator() => throw null;
}

static class ExtensionsA
{
public static void Add<T>(this MyCollection<T> collection, params string[] args) { }
}

static class ExtensionsB
{
public static void Add<T>(this MyCollection<T> collection, params string[] args) { }
}

class Program
{
static void Main()
{
var x = new MyCollection<object>();
x.Add("");

var y = new MyCollection<object> { "" };
}
}
""";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics(
// (25,11): error CS0121: The call is ambiguous between the following methods or properties: 'ExtensionsA.Add<T>(MyCollection<T>, params string[])' and 'ExtensionsB.Add<T>(MyCollection<T>, params string[])'
// x.Add("");
Diagnostic(ErrorCode.ERR_AmbigCall, "Add").WithArguments("ExtensionsA.Add<T>(MyCollection<T>, params string[])", "ExtensionsB.Add<T>(MyCollection<T>, params string[])").WithLocation(25, 11),
// (27,44): error CS0121: The call is ambiguous between the following methods or properties: 'ExtensionsA.Add<T>(MyCollection<T>, params string[])' and 'ExtensionsB.Add<T>(MyCollection<T>, params string[])'
// var y = new MyCollection<object> { "" };
Diagnostic(ErrorCode.ERR_AmbigCall, @"""""").WithArguments("ExtensionsA.Add<T>(MyCollection<T>, params string[])", "ExtensionsB.Add<T>(MyCollection<T>, params string[])").WithLocation(27, 44)
);
}

[Fact]
public void GenericInference()
{
Expand Down
Loading