Commit e1c62a5
Logging source generator: support generic methods (lift SYSLIB1011) (#124638)
Generic `[LoggerMessage]` methods were unconditionally rejected with
`SYSLIB1011`. Methods with type parameters are fully supportable via the
struct-based code generation path. The only remaining restriction is
`allows ref struct` (C# 13), since the generated struct stores params in
fields and cannot hold ref-struct type arguments.
## Description
### Parser (`LoggerMessageGenerator.Parser.cs`)
- Removed the `method.Arity > 0` → `SYSLIB1011` check
- Added `LoggerMethodTypeParameter` / `LoggerMethodTypeParameterSpec`
record types to carry type parameter names + constraints through the
pipeline
- `GetTypeParameterConstraints` serializes all constraint kinds:
`class`/`class?`, `struct`, `unmanaged`, `notnull`, base/interface types
(incl. nullable), `new()`
- Preserves all `FullyQualifiedFormat.MiscellaneousOptions`
(`EscapeKeywordIdentifiers | UseSpecialTypes`) when adding
`IncludeNullableReferenceTypeModifier` — fixes a pre-existing bug on the
same call for parameter types
- Skips `IErrorTypeSymbol` constraint types (unresolvable in generated
code)
- `allows ref struct` detected via `Func<ITypeParameterSymbol, bool>`
delegate compiled from `ITypeParameterSymbol.AllowsRefLikeType` via
`Delegate.CreateDelegate` (compiles against all supported Roslyn
versions; no per-call boxing) — emits `SYSLIB1011`
- Renamed `DiagnosticDescriptors.LoggingMethodIsGeneric` →
`LoggingMethodHasAllowsRefStructConstraint`; updated message + all XLF
files (only changed entry marked `needs-translation`)
### Emitter (`LoggerMessageGenerator.Emitter.cs`)
- `UseLoggerMessageDefine` excludes generic methods — the `Define` path
caches a static delegate and cannot capture method type parameters
- `GenStruct` emits the state struct as generic (`__M1Struct<T>`) with
matching `where` constraints
- `GenLogMethod` emits `<T, U>` and `where` clauses on the partial
method signature
- `GenHolder`/`Format` references parameterized with concrete type
arguments
- Replaced `GetTypeParameterList`/`GetTypeConstraints`
(string-returning, allocating) with
`GenTypeParameterList`/`GenTypeConstraints` writing directly to
`_builder`
- Removed `using System.Linq`; `IndexOf('{')` replaces `Contains('{')`
(no allocating LINQ path on netstandard2.0)
### Incremental cache (`LoggerMessageGenerator.Roslyn4.0.cs`)
- `FromSpec` round-trips `TypeParameters` through the incremental cache
### Docs
- `docs/project/list-of-diagnostics.md` updated: SYSLIB1011 now
describes the `allows ref struct` restriction
- External learn.microsoft.com docs tracked in
[dotnet/docs#51840](dotnet/docs#51840)
## Example
```csharp
// Previously: SYSLIB1011. Now: works, no boxing of TCode.
[LoggerMessage(0, LogLevel.Trace, "Received {Code} ({Length} bytes)")]
public static partial void PacketReceived<TCode>(ILogger logger, TCode code, int length)
where TCode : struct, Enum;
```
All constraint forms supported: `class`, `class?`, `struct`,
`unmanaged`, `notnull`, base type, interface, nullable interface,
`new()`. `allows ref struct` emits SYSLIB1011.
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Logging source generator should support generic methods
(`SYSLIB1011` should be lifted)</issue_title>
> <issue_description>From
[this](https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator#log-method-constraints)
section:
>
> > Logging methods cannot be generic.
>
> This seems to be an unnecessary restriction that also works against
the whole notion of high-performance logging.
>
> I would like to be able to write a logging method like so:
>
> ```csharp
> [LoggerMessage(0, LogLevel.Trace, "C -> S {EndPoint}: {Channel}:{Code}
({Length} bytes)")]
> public static partial void PacketReceived<TCode>(ILogger logger,
IPEndPoint endPoint, ConnectionChannel channel, TCode code, int length)
> where TCode : struct, Enum;
> ```
>
> Here, the type argument for `TCode` depends on the value of `Channel`.
Of course, I could make the logging method non-generic and accept an
`Enum`-typed value, but then I get unconditional boxing... certainly not
what I want when logging in a fairly hot path. Alternatively, I could
duplicate the method for as many `TCode`s as I have (which is what I'll
do for now), but this is not great either, for all the expected
maintenance reasons.
>
> I think this restriction should just be lifted. Maybe there's a good
reason for it, but if there is, it's not obvious to
me.</issue_description>
>
> <agent_instructions>Implement and test this. Make sure that all of
these are supported: multiple type parameters, multiple constraints,
various kinds of constraints.</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@</author><body>
> Tagging subscribers to this area: @dotnet/area-extensions-logging
> See info in
[area-owners.md](https://github.com/dotnet/runtime/blob/main/docs/area-owners.md)
if you want to be subscribed.
> <details>
> <summary>Issue Details</summary>
> <hr />
>
> From
[this](https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator#log-method-constraints)
section:
>
> > Logging methods cannot be generic.
>
> This seems to be an unnecessary restriction that also works against
the whole notion of high-performance logging.
>
> I would like to be able to write a logging method like so:
>
> ```csharp
> [LoggerMessage(0, LogLevel.Trace, "C -> S {EndPoint}: {Channel}:{Code}
({Length} bytes)")]
> public static partial void PacketReceived<TCode>(ILogger logger,
IPEndPoint endPoint, ConnectionChannel channel, TCode code, int length)
> where TCode : struct, Enum;
> ```
>
> Here, the type argument for `TCode` depends on the value of `Channel`.
Of course, I could make the logging method non-generic and accept an
`Enum`-typed value, but then I get unconditional boxing... certainly not
what I want when logging in a fairly hot path. Alternatively, I could
duplicate the method for as many `TCode`s as I have (which is what I'll
do for now), but this is not great either, for all the expected
maintenance reasons.
>
> I think this restriction should just be lifted. Maybe there's a good
reason for it, but if there is, it's not obvious to me.
>
> <table>
> <tr>
> <th align="left">Author:</th>
> <td>alexrp</td>
> </tr>
> <tr>
> <th align="left">Assignees:</th>
> <td>-</td>
> </tr>
> <tr>
> <th align="left">Labels:</th>
> <td>
>
> `untriaged`, `area-Extensions-Logging`
>
> </td>
> </tr>
> <tr>
> <th align="left">Milestone:</th>
> <td>-</td>
> </tr>
> </table>
> </details></body></comment_new>
> <comment_new><author>@cincuranet</author><body>
> Triage: Niche scenario, but would be nice to have it one
day.</body></comment_new>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes #90589
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: svick <287848+svick@users.noreply.github.com>
Co-authored-by: Petr Onderka <petronderka@microsoft.com>
Co-authored-by: tarekgh <10833894+tarekgh@users.noreply.github.com>1 parent b5fef80 commit e1c62a5
24 files changed
Lines changed: 531 additions & 77 deletions
File tree
- docs/project
- src/libraries/Microsoft.Extensions.Logging.Abstractions
- gen
- Resources
- xlf
- tests/Microsoft.Extensions.Logging.Generators.Tests
- Baselines
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | | - | |
87 | | - | |
| 86 | + | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| |||
Lines changed: 61 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
60 | 59 | | |
61 | 60 | | |
62 | 61 | | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
154 | 158 | | |
155 | 159 | | |
156 | 160 | | |
| |||
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
178 | | - | |
| 182 | + | |
179 | 183 | | |
180 | 184 | | |
181 | 185 | | |
| |||
185 | 189 | | |
186 | 190 | | |
187 | 191 | | |
188 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
189 | 195 | | |
190 | 196 | | |
191 | 197 | | |
| |||
369 | 375 | | |
370 | 376 | | |
371 | 377 | | |
372 | | - | |
373 | | - | |
374 | | - | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
375 | 381 | | |
376 | 382 | | |
377 | 383 | | |
| |||
385 | 391 | | |
386 | 392 | | |
387 | 393 | | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
388 | 432 | | |
389 | 433 | | |
390 | 434 | | |
| |||
414 | 458 | | |
415 | 459 | | |
416 | 460 | | |
417 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
418 | 464 | | |
419 | 465 | | |
420 | 466 | | |
421 | | - | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
422 | 470 | | |
423 | 471 | | |
424 | 472 | | |
| |||
448 | 496 | | |
449 | 497 | | |
450 | 498 | | |
451 | | - | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
452 | 502 | | |
453 | 503 | | |
454 | 504 | | |
| |||
Lines changed: 114 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
25 | 32 | | |
26 | 33 | | |
27 | 34 | | |
| |||
239 | 246 | | |
240 | 247 | | |
241 | 248 | | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
242 | 258 | | |
243 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
244 | 272 | | |
245 | 273 | | |
246 | 274 | | |
| |||
263 | 291 | | |
264 | 292 | | |
265 | 293 | | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | 294 | | |
274 | 295 | | |
275 | 296 | | |
| |||
400 | 421 | | |
401 | 422 | | |
402 | 423 | | |
| 424 | + | |
403 | 425 | | |
404 | 426 | | |
405 | 427 | | |
| |||
745 | 767 | | |
746 | 768 | | |
747 | 769 | | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
748 | 812 | | |
749 | 813 | | |
750 | 814 | | |
| |||
949 | 1013 | | |
950 | 1014 | | |
951 | 1015 | | |
| 1016 | + | |
952 | 1017 | | |
953 | 1018 | | |
954 | 1019 | | |
| |||
966 | 1031 | | |
967 | 1032 | | |
968 | 1033 | | |
| 1034 | + | |
969 | 1035 | | |
970 | 1036 | | |
971 | 1037 | | |
| |||
988 | 1054 | | |
989 | 1055 | | |
990 | 1056 | | |
| 1057 | + | |
991 | 1058 | | |
992 | 1059 | | |
993 | 1060 | | |
| |||
1007 | 1074 | | |
1008 | 1075 | | |
1009 | 1076 | | |
| 1077 | + | |
1010 | 1078 | | |
1011 | 1079 | | |
1012 | 1080 | | |
| |||
1025 | 1093 | | |
1026 | 1094 | | |
1027 | 1095 | | |
| 1096 | + | |
1028 | 1097 | | |
1029 | 1098 | | |
1030 | 1099 | | |
| |||
1133 | 1202 | | |
1134 | 1203 | | |
1135 | 1204 | | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
1136 | 1243 | | |
1137 | 1244 | | |
1138 | 1245 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
262 | 271 | | |
263 | 272 | | |
264 | 273 | | |
| |||
0 commit comments