Skip to content

Comments

Update logging source generator to support generic methods#7331

Open
Copilot wants to merge 7 commits intomainfrom
copilot/update-telemetry-logging-generator
Open

Update logging source generator to support generic methods#7331
Copilot wants to merge 7 commits intomainfrom
copilot/update-telemetry-logging-generator

Conversation

Copy link
Contributor

Copilot AI commented Feb 23, 2026

Mirrors dotnet/runtime#124638. LOGGEN007 previously rejected all generic logging methods unconditionally. The restriction is narrowed to only block allows ref struct constraints, which can't be stored as fields in the generated state struct.

Changes

  • Parser: Remove Arity > 0 → LOGGEN007. Populate LoggingMethod.TypeParameters from IMethodSymbol.TypeParameters, serializing all constraint kinds. Detect allows ref struct via reflection on ITypeParameterSymbol.AllowsRefLikeType (Roslyn 4.9+; no-ops on older Roslyn).
  • Diagnostic: Rename LoggingMethodIsGenericLoggingMethodHasAllowsRefStructConstraint with updated message; update docs/list-of-diagnostics.md.
  • Model: Add LoggingMethodTypeParameter (Name, Constraints); add TypeParameters list to LoggingMethod.
  • Emitter: Emit <T1, T2, ...> and properly indented where T : ... clauses on the generated partial method signature. Use Out(char) for single-character output.
  • Tests: MethodGeneric now expects success; add MethodGenericWithConstraints, MethodGenericMultipleTypeParams, MethodGenericWithAllowsRefStructConstraint (skips on Roslyn < 4.9), and GenericTestExtensions.cs for emitter validation.

Example

// Previously LOGGEN007 — now generates correct code
[LoggerMessage(0, LogLevel.Trace, "Received {Code} on {Channel}")]
public static partial void PacketReceived<TCode>(
    ILogger logger, TCode code, ConnectionChannel channel)
    where TCode : struct, Enum;

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits February 23, 2026 10:44
…gingMethodHasAllowsRefStructConstraint

Co-authored-by: svick <287848+svick@users.noreply.github.com>
Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title [WIP] Update telemetry logging source generator for recent changes Support generic methods in logging source generator (lift LOGGEN007 restriction) Feb 23, 2026
Copilot AI requested a review from svick February 23, 2026 10:46
Copilot stopped work on behalf of svick due to an error February 23, 2026 12:40
Copilot AI and others added 2 commits February 23, 2026 12:52
…aints code

Co-authored-by: svick <287848+svick@users.noreply.github.com>
…StructConstraint)

Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title Support generic methods in logging source generator (lift LOGGEN007 restriction) Lift generic method restriction in logging source generator (LOGGEN007 → allows ref struct only) Feb 23, 2026
@svick svick marked this pull request as ready for review February 23, 2026 15:09
@svick svick requested a review from a team as a code owner February 23, 2026 15:09
Copilot AI review requested due to automatic review settings February 23, 2026 15:09
@svick
Copy link
Member

svick commented Feb 23, 2026

cc: @evgenyfedorov2

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Microsoft.Gen.Logging source generator to allow generic logging methods, while keeping LOGGEN007 only for the C# 13 allows ref struct anti-constraint (which can’t be represented safely in the generated state).

Changes:

  • Allow generic logging methods and model/serialize method type parameters + constraints during parsing.
  • Rename LOGGEN007 descriptor/message to target only allows ref struct.
  • Emit generic type parameters and where constraints on the generated partial method signatures; add test coverage for allowed and rejected cases.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/Generators/Microsoft.Gen.Logging/Unit/ParserTests.LogMethod.cs Adds tests for generic logging methods, constraints, and allows ref struct rejection (skipping on older Roslyn).
test/Generators/Microsoft.Gen.Logging/TestClasses/GenericTestExtensions.cs Adds representative generic logging methods for emitter/regression coverage.
src/Generators/Microsoft.Gen.Logging/Parsing/Resources.resx Updates LOGGEN007 title/message to “allows ref struct” constraint wording.
src/Generators/Microsoft.Gen.Logging/Parsing/Resources.Designer.cs Regenerates strongly-typed resource accessors for the renamed resource keys.
src/Generators/Microsoft.Gen.Logging/Parsing/Parser.cs Removes generic-method rejection, adds type parameter capture + constraint formatting, and detects allows ref struct via reflection.
src/Generators/Microsoft.Gen.Logging/Parsing/DiagDescriptors.cs Renames LOGGEN007 descriptor to LoggingMethodHasAllowsRefStructConstraint.
src/Generators/Microsoft.Gen.Logging/Model/LoggingMethodTypeParameter.cs Introduces a model type for method type parameters + constraint text.
src/Generators/Microsoft.Gen.Logging/Model/LoggingMethod.cs Adds TypeParameters collection to the logging method model.
src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Utils.cs Adds helper emission for <T...> and where T : ... clauses.
src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs Emits generic parameters/constraints in the generated partial method signature.
Files not reviewed (1)
  • src/Generators/Microsoft.Gen.Logging/Parsing/Resources.Designer.cs: Language not supported

if (tp.Constraints is not null)
{
OutLn();
Out($" where {tp.Name} : {tp.Constraints}");
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

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

GenTypeConstraints hardcodes 4 spaces (" where ...") instead of using the current indentation level. This will misalign where clauses when the generated method is inside nested types (indent > 1). After OutLn(), call OutIndent() and then write where ... without hardcoded spaces (or otherwise compute indentation from the current indent level).

Suggested change
Out($" where {tp.Name} : {tp.Constraints}");
OutIndent();
Out($"where {tp.Name} : {tp.Constraints}");

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Fix this. Type constraints should be indented one more level than the surrounding code, since this is the common formatting convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 8ad114f. GenTypeConstraints now uses Indent()/OutIndent()/Unindent() so the where clause is indented one extra level beyond the method signature, regardless of nesting depth.

Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI requested a review from a team as a code owner February 23, 2026 15:28
Copilot AI requested a review from svick February 23, 2026 15:29
…clause alignment

Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title Lift generic method restriction in logging source generator (LOGGEN007 → allows ref struct only) Update logging source generator to support generic methods (per dotnet/runtime#124638) Feb 23, 2026
Copilot AI requested a review from svick February 23, 2026 15:46
@svick svick changed the title Update logging source generator to support generic methods (per dotnet/runtime#124638) Update logging source generator to support generic methods Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants