Skip to content

Extensions: Do not consider extension block methods as entry points, consider their implementation methods instead #78667

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

Merged
merged 3 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 23 additions & 1 deletion src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,14 @@ internal EntryPoint GetEntryPointAndDiagnostics(CancellationToken cancellationTo
return scriptClass.GetScriptEntryPoint();
}

var mainTypeOrNamespace = globalNamespace.GetNamespaceOrTypeByQualifiedName(mainTypeName.Split('.')).OfMinimalArity();
var nameParts = mainTypeName.Split('.');
if (nameParts.Any(n => string.IsNullOrWhiteSpace(n)))
{
diagnostics.Add(ErrorCode.ERR_BadCompilationOptionValue, NoLocation.Singleton, nameof(CSharpCompilationOptions.MainTypeName), mainTypeName);
return null;
}

var mainTypeOrNamespace = globalNamespace.GetNamespaceOrTypeByQualifiedName(nameParts).OfMinimalArity();
if (mainTypeOrNamespace is null)
{
diagnostics.Add(ErrorCode.ERR_MainClassNotFound, NoLocation.Singleton, mainTypeName);
Expand Down Expand Up @@ -2157,6 +2164,21 @@ private static void AddEntryPointCandidates(
ArrayBuilder<MethodSymbol> entryPointCandidates, IEnumerable<Symbol> members)
{
foreach (var member in members)
{
if (member.GetIsNewExtensionMember())
{
if (member is MethodSymbol method && method.TryGetCorrespondingExtensionImplementationMethod() is { } implementationMethod)
{
addIfCandidate(entryPointCandidates, implementationMethod);
Copy link
Contributor

@AlekseyTs AlekseyTs May 23, 2025

Choose a reason for hiding this comment

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

addIfCandidate(entryPointCandidates, implementationMethod);

It is not obvious why we need to do this. Wouldn't the implementation methods be enumerated as members of the enclosing class?

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 search for entry point uses GetSymbolsWithNameCore which relies on NameSymbolSearcher/AbstractSymbolSearcher which walks through the declaration tree (which doesn't include the implementation methods)

Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a comment explaining this.

}
}
else
{
addIfCandidate(entryPointCandidates, member);
}
}

static void addIfCandidate(ArrayBuilder<MethodSymbol> entryPointCandidates, Symbol member)
{
if (member is MethodSymbol method &&
method.IsEntryPointCandidate)
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ internal bool IsEntryPointCandidate
{
get
{
Debug.Assert(!this.GetIsNewExtensionMember());

if (this.IsPartialDefinition() &&
this.PartialImplementationPart is null)
{
Expand Down
131 changes: 120 additions & 11 deletions src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35517,16 +35517,36 @@ static class E
}
""";
var comp = CreateCompilation(source, options: TestOptions.DebugExe);
comp.VerifyEmitDiagnostics();
// Tracked by https://github.com/dotnet/roslyn/issues/76130
// FindEntryPoint/NameSymbolSearcher skip "E" since it has not declaration named "Main"
// CompileAndVerify(comp, expectedOutput: "ran");
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
Copy link
Contributor

@AlekseyTs AlekseyTs May 23, 2025

Choose a reason for hiding this comment

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

CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();

It looks like this behavior is in disagreement with the PR title: "Extensions: extension methods cannot be entry points"

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the title. The extension skeleton method cannot be considered, but the implementation method can (for portability from classic extension methods)

Copy link
Contributor

Choose a reason for hiding this comment

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

Updated the title. The extension skeleton method cannot be considered, but the implementation method can (for portability from classic extension methods)

It looks like implementation methods were not considered before this change. I think it is important to reflect that they are considered now in the commit title. Also, I would avoid using the "skeleton" term. Something like: "Do not consider extension block methods as entry points, consider their implementation methods instead"


comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E"));
comp.VerifyEmitDiagnostics();
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
}

[Fact]
public void Validation_EntryPoint_02()
{
string source = """
static class E
{
extension(int)
{
public static System.Action Main => throw null;
}
}
""";
var comp = CreateCompilation(source, options: TestOptions.DebugExe);
comp.VerifyEmitDiagnostics(
// error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Diagnostic(ErrorCode.ERR_NoEntryPoint).WithLocation(1, 1));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E"));
comp.VerifyDiagnostics(
// (1,14): error CS1558: 'E' does not have a suitable static 'Main' method
// static class E
Diagnostic(ErrorCode.ERR_NoMainInClass, "E").WithArguments("E").WithLocation(1, 14));
}

[Fact]
public void Validation_EntryPoint_03()
{
Expand All @@ -35535,14 +35555,19 @@ static class E
{
extension(int i)
{
public static void Main() { System.Console.Write("ran"); }
public static void Main() => throw null;
}
}
""";
var comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E.<>E__0"));
comp.VerifyEmitDiagnostics(
// error CS1555: Could not find 'E.<>E__0' specified for Main method
Diagnostic(ErrorCode.ERR_MainClassNotFound).WithArguments("E.<>E__0").WithLocation(1, 1));

Assert.Equal("<>E__0", comp.GetTypeByMetadataName("E").GetTypeMembers().Single().ExtensionName);

// Tracked by https://github.com/dotnet/roslyn/issues/76130 : we should find the unspeakable nested type
Assert.Null(comp.GetTypeByMetadataName("E+<>E__0"));
}

[Fact]
Expand All @@ -35557,13 +35582,97 @@ public static void Main() { }
}
}
""";
// Tracked by https://github.com/dotnet/roslyn/issues/76130
// FindEntryPoint should not attempt to find types with an empty name
var comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E."));
comp.VerifyEmitDiagnostics(
// (3,5): error CS1556: 'E.extension(int)' specified for Main method must be a non-generic class, record, struct, or interface
// extension(int i)
Diagnostic(ErrorCode.ERR_MainClassNotClass, "extension").WithArguments("E.extension(int)").WithLocation(3, 5));
// error CS7088: Invalid 'MainTypeName' value: 'E.'.
Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MainTypeName", "E.").WithLocation(1, 1));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E. "));
comp.VerifyEmitDiagnostics(
// error CS7088: Invalid 'MainTypeName' value: 'E. '.
Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MainTypeName", "E. ").WithLocation(1, 1));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName(""));
comp.VerifyEmitDiagnostics(
Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MainTypeName", "").WithLocation(1, 1));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName(" "));
comp.VerifyEmitDiagnostics(
// error CS7088: Invalid 'MainTypeName' value: ' '.
Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MainTypeName", " ").WithLocation(1, 1));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName(".A"));
comp.VerifyEmitDiagnostics(
// error CS7088: Invalid 'MainTypeName' value: '.A'.
Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MainTypeName", ".A").WithLocation(1, 1));
}

[Fact]
public void Validation_EntryPoint_05()
{
string source = """
static class E
{
extension(int i)
{
public static async System.Threading.Tasks.Task<int> Main() { System.Console.Write("ran"); await System.Threading.Tasks.Task.Yield(); return 0; }
}
}
""";
var comp = CreateCompilation(source, options: TestOptions.DebugExe);
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E"));
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
}

[Fact]
public void Validation_EntryPoint_06()
{
string source = """
static class E
{
extension(string[] args)
{
public void Main() { System.Console.Write("ran"); }
}
}
""";
var comp = CreateCompilation(source, options: TestOptions.DebugExe);
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E"));
CompileAndVerify(comp, expectedOutput: "ran").VerifyDiagnostics();
}

[Fact]
public void Validation_EntryPoint_07()
{
string source = """
static class E
{
extension(int i)
{
public void Main() => throw null;
}
}
""";
var comp = CreateCompilation(source, options: TestOptions.DebugExe);
comp.VerifyEmitDiagnostics(
// error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Diagnostic(ErrorCode.ERR_NoEntryPoint).WithLocation(1, 1),
// (5,21): warning CS0028: 'E.Main(int)' has the wrong signature to be an entry point
// public void Main() => throw null;
Diagnostic(ErrorCode.WRN_InvalidMainSig, "Main").WithArguments("E.Main(int)").WithLocation(5, 21));

comp = CreateCompilation(source, options: TestOptions.ReleaseExe.WithMainTypeName("E"));
comp.VerifyEmitDiagnostics(
// (1,14): error CS1558: 'E' does not have a suitable static 'Main' method
// static class E
Diagnostic(ErrorCode.ERR_NoMainInClass, "E").WithArguments("E").WithLocation(1, 14),
// (5,21): warning CS0028: 'E.Main(int)' has the wrong signature to be an entry point
// public void Main() => throw null;
Diagnostic(ErrorCode.WRN_InvalidMainSig, "Main").WithArguments("E.Main(int)").WithLocation(5, 21));
}

[Fact]
Expand Down