Skip to content

Commit 3863d2e

Browse files
author
Julien Couvreur
committed
Extensions: fix issue with GetParameterOrExtensionParameter
1 parent 24ab192 commit 3863d2e

File tree

2 files changed

+209
-3
lines changed

2 files changed

+209
-3
lines changed

src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,19 +2091,19 @@ private void RemoveWorseMembers<TMember>(ArrayBuilder<MemberResolutionResult<TMe
20912091
private static ParameterSymbol GetParameterOrExtensionParameter<TMember>(int argIndex, MemberAnalysisResult result, ImmutableArray<ParameterSymbol> parameters, TMember member)
20922092
where TMember : Symbol
20932093
{
2094+
int paramIndex = result.ParameterFromArgument(argIndex);
20942095
if (member.GetIsNewExtensionMember())
20952096
{
2096-
if (argIndex == 0)
2097+
if (paramIndex == 0)
20972098
{
20982099
ParameterSymbol? extensionParameter = member.ContainingType.ExtensionParameter;
20992100
Debug.Assert(extensionParameter is not null);
21002101
return extensionParameter;
21012102
}
21022103

2103-
argIndex--;
2104+
paramIndex--;
21042105
}
21052106

2106-
int paramIndex = result.ParameterFromArgument(argIndex);
21072107
return parameters[paramIndex];
21082108
}
21092109

src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests2.cs

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14361,5 +14361,211 @@ public static class E
1436114361
var extension = (SourceNamedTypeSymbol)comp.GetMember<NamedTypeSymbol>("E").GetTypeMembers().Single();
1436214362
AssertEx.Equal("extension([MyAttribute/*()*/] System.Int32)", extension.ComputeExtensionMarkerRawName());
1436314363
}
14364+
14365+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14366+
public void OverloadResolution_01()
14367+
{
14368+
var src = """
14369+
public static class E
14370+
{
14371+
extension(object @this)
14372+
{
14373+
public void M<T>(T x)
14374+
{
14375+
@this.M(x, default);
14376+
}
14377+
14378+
public void M(params System.ReadOnlySpan<object> span)
14379+
{
14380+
}
14381+
14382+
private void M<T>(T x, System.ReadOnlySpan<object> span)
14383+
{
14384+
}
14385+
}
14386+
}
14387+
""";
14388+
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net90);
14389+
comp.VerifyEmitDiagnostics();
14390+
}
14391+
14392+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14393+
public void OverloadResolution_02()
14394+
{
14395+
var src = """
14396+
new object().M(a: null);
14397+
14398+
public static class E
14399+
{
14400+
extension(object o1)
14401+
{
14402+
public void M(object a) { }
14403+
14404+
public void M(string a) { System.Console.Write("ran"); }
14405+
}
14406+
}
14407+
""";
14408+
var comp = CreateCompilation(src);
14409+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14410+
}
14411+
14412+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14413+
public void OverloadResolution_03()
14414+
{
14415+
var src = """
14416+
new object().M(a: null);
14417+
14418+
public static class E
14419+
{
14420+
extension(object o1)
14421+
{
14422+
public void M(object a) { }
14423+
}
14424+
14425+
public static void M(this object o, string a) { System.Console.Write("ran"); }
14426+
}
14427+
""";
14428+
var comp = CreateCompilation(src);
14429+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14430+
}
14431+
14432+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14433+
public void OverloadResolution_04()
14434+
{
14435+
var src = """
14436+
new object().M(a: null);
14437+
14438+
public static class E
14439+
{
14440+
extension(object o1)
14441+
{
14442+
public void M(string a) { System.Console.Write("ran"); }
14443+
}
14444+
14445+
public static void M(this object o, object a) { }
14446+
}
14447+
""";
14448+
var comp = CreateCompilation(src);
14449+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14450+
}
14451+
14452+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14453+
public void OverloadResolution_05()
14454+
{
14455+
var src = """
14456+
new object().M(a: null);
14457+
14458+
public static class E
14459+
{
14460+
extension(object o1)
14461+
{
14462+
public void M(object a) { }
14463+
public void M(params int[] a) { System.Console.Write("ran"); }
14464+
}
14465+
}
14466+
""";
14467+
var comp = CreateCompilation(src);
14468+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14469+
}
14470+
14471+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14472+
public void OverloadResolution_06()
14473+
{
14474+
var src = """
14475+
new object().M(1, 2);
14476+
14477+
public static class E
14478+
{
14479+
extension(object o1)
14480+
{
14481+
public void M(int x1, int x2) { System.Console.Write("ran"); }
14482+
public void M(params int[] a) { }
14483+
}
14484+
}
14485+
""";
14486+
var comp = CreateCompilation(src);
14487+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14488+
}
14489+
14490+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14491+
public void OverloadResolution_07()
14492+
{
14493+
var src = """
14494+
new object().M(a: 1, 2);
14495+
14496+
public static class E
14497+
{
14498+
extension(object o1)
14499+
{
14500+
public void M(int a, int b) { System.Console.Write("ran"); }
14501+
public void M(params int[] a) { }
14502+
}
14503+
}
14504+
""";
14505+
var comp = CreateCompilation(src);
14506+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14507+
}
14508+
14509+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14510+
public void OverloadResolution_08()
14511+
{
14512+
var src = """
14513+
new object().M(index: 0, 1, 2);
14514+
14515+
public static class E
14516+
{
14517+
extension(object o1)
14518+
{
14519+
public void M(int index, int a, int b) { System.Console.Write("ran"); }
14520+
public void M(int index, params int[] a) { }
14521+
}
14522+
}
14523+
""";
14524+
var comp = CreateCompilation(src);
14525+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14526+
}
14527+
14528+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14529+
public void OverloadResolution_09()
14530+
{
14531+
var src = """
14532+
object.M(index: 0, 1, 2);
14533+
14534+
public static class E
14535+
{
14536+
extension(object)
14537+
{
14538+
public static void M(int index, int a, int b) { System.Console.Write("ran"); }
14539+
public static void M(int index, params int[] a) { }
14540+
}
14541+
}
14542+
""";
14543+
var comp = CreateCompilation(src);
14544+
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
14545+
}
14546+
14547+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79193")]
14548+
public void OverloadResolution_10()
14549+
{
14550+
var src = """
14551+
1.M(2);
14552+
14553+
public static class E
14554+
{
14555+
extension(params int[] i)
14556+
{
14557+
public static void M() { }
14558+
}
14559+
}
14560+
""";
14561+
var comp = CreateCompilation(src);
14562+
comp.VerifyEmitDiagnostics(
14563+
// (5,15): error CS1670: params is not valid in this context
14564+
// extension(params int[] i)
14565+
Diagnostic(ErrorCode.ERR_IllegalParams, "params").WithLocation(5, 15),
14566+
// (1,3): error CS1501: No overload for method 'M' takes 1 arguments
14567+
// 1.M(2);
14568+
Diagnostic(ErrorCode.ERR_BadArgCount, "M").WithArguments("M", "1").WithLocation(1, 3));
14569+
}
1436414570
}
1436514571

0 commit comments

Comments
 (0)