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

Simplify PublicTopLevelProgramGenerator #59601

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -17,25 +17,27 @@ public partial class Program { }

public void Initialize(IncrementalGeneratorInitializationContext context)
{
var internalGeneratedProgramClass = context.CompilationProvider
var internalGeneratedProgramClass = context.CompilationProvider.Select(static (compilation, cancellationToken) =>
// Get the entry point associated with the compilation, this maps to the Main method definition
.Select(static (compilation, cancellationToken) => compilation.GetEntryPoint(cancellationToken))
// Get the containing symbol of the entry point, this maps to the Program class
.Select(static (symbol, _) => symbol?.ContainingSymbol)
// If the program class is already public, we don't need to generate anything.
.Select(static (symbol, _) => symbol?.DeclaredAccessibility == Accessibility.Public ? null : symbol)
// If the discovered `Program` type is not a class then its not
// generated and has been defined in source, so we can skip it
.Select(static (symbol, _) => symbol is INamedTypeSymbol { TypeKind: TypeKind.Class } ? symbol : null)
// If there are multiple partial declarations, then do nothing since we don't want
// to trample on visibility explicitly set by the user
.Select(static (symbol, _) => symbol is { DeclaringSyntaxReferences: { Length: 1 } declaringSyntaxReferences } ? declaringSyntaxReferences.Single() : null)
compilation.GetEntryPoint(cancellationToken)?.ContainingSymbol is INamedTypeSymbol
{
// If the discovered `Program` type is not a class then its not
// generated and has been defined in source, so we can skip it
// If the program class is already public, we don't need to generate anything.
DeclaredAccessibility: Accessibility.Public,
TypeKind: TypeKind.Class,
// If there are multiple partial declarations, then do nothing since we don't want
// to trample on visibility explicitly set by the user
DeclaringSyntaxReferences: { Length: 1 } declaringSyntaxReferences
} &&
// If the `Program` class is already declared in user code, we don't need to generate anything.
.Select(static (declaringSyntaxReference, cancellationToken) => declaringSyntaxReference?.GetSyntax(cancellationToken) is ClassDeclarationSyntax ? null : declaringSyntaxReference);
declaringSyntaxReferences.Single().GetSyntax(cancellationToken) is not ClassDeclarationSyntax
);

context.RegisterSourceOutput(internalGeneratedProgramClass, (context, result) =>
{
if (result is not null)
if (result)
{
context.AddSource("PublicTopLevelProgram.Generated.g.cs", PublicPartialProgramClassSource);
}
Expand Down
Loading