Update logging source generator to support generic methods#7331
Update logging source generator to support generic methods#7331
Conversation
…gingMethodHasAllowsRefStructConstraint Co-authored-by: svick <287848+svick@users.noreply.github.com>
Co-authored-by: svick <287848+svick@users.noreply.github.com>
…aints code Co-authored-by: svick <287848+svick@users.noreply.github.com>
…StructConstraint) Co-authored-by: svick <287848+svick@users.noreply.github.com>
allows ref struct only)
|
cc: @evgenyfedorov2 |
There was a problem hiding this comment.
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
whereconstraints 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}"); |
There was a problem hiding this comment.
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).
| Out($" where {tp.Name} : {tp.Constraints}"); | |
| OutIndent(); | |
| Out($"where {tp.Name} : {tp.Constraints}"); |
There was a problem hiding this comment.
@copilot Fix this. Type constraints should be indented one more level than the surrounding code, since this is the common formatting convention.
There was a problem hiding this comment.
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>
…clause alignment Co-authored-by: svick <287848+svick@users.noreply.github.com>
allows ref struct only)
Mirrors dotnet/runtime#124638. LOGGEN007 previously rejected all generic logging methods unconditionally. The restriction is narrowed to only block
allows ref structconstraints, which can't be stored as fields in the generated state struct.Changes
Arity > 0→ LOGGEN007. PopulateLoggingMethod.TypeParametersfromIMethodSymbol.TypeParameters, serializing all constraint kinds. Detectallows ref structvia reflection onITypeParameterSymbol.AllowsRefLikeType(Roslyn 4.9+; no-ops on older Roslyn).LoggingMethodIsGeneric→LoggingMethodHasAllowsRefStructConstraintwith updated message; updatedocs/list-of-diagnostics.md.LoggingMethodTypeParameter(Name,Constraints); addTypeParameterslist toLoggingMethod.<T1, T2, ...>and properly indentedwhere T : ...clauses on the generated partial method signature. UseOut(char)for single-character output.MethodGenericnow expects success; addMethodGenericWithConstraints,MethodGenericMultipleTypeParams,MethodGenericWithAllowsRefStructConstraint(skips on Roslyn < 4.9), andGenericTestExtensions.csfor emitter validation.Example
🔒 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.