Skip to content
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 @@ -353,15 +353,33 @@ private PEParameterSymbol(
bool hasNameInMetadata = !string.IsNullOrEmpty(_name);
if (!hasNameInMetadata)
{
// As was done historically, if the parameter doesn't have a name, we give it the name "value".
_name = "value";
if (isExtensionMarkerParameter(containingSymbol, ordinal))
{
_name = "";
}
else
{
// As was done historically, if the parameter doesn't have a name, we give it the name "value".
_name = "value";
}
}

_packedFlags = new PackedFlags(refKind, attributesAreComplete: handle.IsNil, hasNameInMetadata: hasNameInMetadata, scope, hasUnscopedRefAttribute);

Debug.Assert(refKind == this.RefKind);
Debug.Assert(hasNameInMetadata == this.HasNameInMetadata);
Debug.Assert(_name is not null);

static bool isExtensionMarkerParameter(Symbol containingSymbol, int ordinal)
{
if (containingSymbol.MetadataName != WellKnownMemberNames.ExtensionMarkerMethodName)
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 18, 2025

Choose a reason for hiding this comment

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

containingSymbol.MetadataName != WellKnownMemberNames.ExtensionMarkerMethodName

We might also check for HasSpecialName and IsStatic and that containing symbol is a method. #ByDesign

Copy link
Member Author

Choose a reason for hiding this comment

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

The further optimization doesn't seem worthwhile. The method name check already does most of that work, and the remaining checks are built into GetMarkerMethodSymbol()

{
return false;
}

var markerMethod = ((PENamedTypeSymbol)containingSymbol.ContainingType).GetMarkerMethodSymbol();
return object.ReferenceEquals(markerMethod, containingSymbol) && ordinal == 0;
}
}

private bool HasNameInMetadata
Expand Down
48 changes: 46 additions & 2 deletions src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4575,6 +4575,50 @@ private class C { }
Diagnostic(ErrorCode.ERR_BadVisIndexerParam, "P").WithArguments("Extensions.extension(Extensions.C).P", "Extensions.C").WithLocation(6, 27));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81251")]
public void ReceiverParameter_Name()
{
// Unnamed extension parameter should not default to "value" as its name when round-tripped
var libSrc = """
using System;

public static class ArrayEx
{
extension(Array)
{
public static T[] Init<T>(T value)
=> throw null;
}
}
""";
var libComp = CreateCompilation(libSrc);
libComp.VerifyDiagnostics();

var src = """
using System;

Array.Init(value: 10);
""";
var comp = CreateCompilation([src, libSrc]);
comp.VerifyEmitDiagnostics();
validate(comp);

comp = CreateCompilation(src, references: [libComp.EmitToImageReference()]);
comp.VerifyEmitDiagnostics();
validate(comp);

comp = CreateCompilation(src, references: [libComp.ToMetadataReference()]);
comp.VerifyEmitDiagnostics();
validate(comp);

void validate(CSharpCompilation comp)
{
var extension = comp.GlobalNamespace.GetTypeMember("ArrayEx").GetTypeMembers("").Single();
Assert.True(extension.IsExtension);
Assert.Equal("", extension.ExtensionParameter.Name);
}
}

[Fact]
public void InconsistentTypeAccessibility_01()
{
Expand Down Expand Up @@ -9085,7 +9129,7 @@ public static string M(object o, string s, int x)
AssertEx.Equal("Extensions.extension(object).M(object, string)", m1.ToDisplayString());
AssertEx.Equal([], m1.GetAttributes());

AssertEx.Equal("System.Object value", extensions[1].ExtensionParameter.ToTestDisplayString());
AssertEx.Equal("System.Object", extensions[1].ExtensionParameter.ToTestDisplayString());
AssertEx.Equal("<M>$C43E2675C7BBF9284AF22FB8A9BF0280", extensions[1].MetadataName);
Symbol m2 = extensions[1].GetMembers().Single();
AssertEx.Equal("Extensions.extension(object).M(object, string, int)", m2.ToDisplayString());
Expand Down Expand Up @@ -29798,7 +29842,7 @@ public static partial class C
var container = m.GlobalNamespace.GetTypeMember("C");
var extension = container.GetTypeMembers().Single();

AssertEx.Equal("System.Object value", extension.ExtensionParameter.ToTestDisplayString());
AssertEx.Equal("System.Object", extension.ExtensionParameter.ToTestDisplayString());
AssertEx.Equal("<M>$C43E2675C7BBF9284AF22FB8A9BF0280", extension.MetadataName);

var methods = extension.GetMembers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24749,7 +24749,7 @@ static void validate(ModuleSymbol module)
var extension = module.GlobalNamespace.GetMember<NamedTypeSymbol>("E").GetTypeMembers().Single();
Assert.Equal(["AAttribute", "BAttribute"], extension.TypeParameters[0].GetAttributes().Select(a => a.ToString()));
Assert.Equal(["AAttribute", "BAttribute"], extension.ExtensionParameter.GetAttributes().Select(a => a.ToString()));
Assert.Equal(module is SourceModuleSymbol ? "" : "value", extension.ExtensionParameter.Name);
Assert.Equal("", extension.ExtensionParameter.Name);
}
}

Expand Down
Loading