-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The search for entry point uses There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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() | ||
{ | ||
|
@@ -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] | ||
|
@@ -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] | ||
|
Uh oh!
There was an error while loading. Please reload this page.