Skip to content

Fix CS0315 for non-IUtf8SpanFormattable types (bool, enum, custom structs)#16

Merged
DJGosnell merged 3 commits intomasterfrom
fix/non-formattable-type-emission
Mar 5, 2026
Merged

Fix CS0315 for non-IUtf8SpanFormattable types (bool, enum, custom structs)#16
DJGosnell merged 3 commits intomasterfrom
fix/non-formattable-type-emission

Conversation

@DJGosnell
Copy link
Member

@DJGosnell DJGosnell commented Mar 5, 2026

Summary

  • Fixes CS0315 compilation error when using bool, enum, or custom struct parameters in [LogMessage] methods. The generator now resolves SerializationKind per parameter at classification time by checking IUtf8SpanFormattable on the type symbol, emitting the correct write path for each type.

Reason for Change

The source generator unconditionally emitted writer.WriteFormatted(value) for all non-string parameters, which has a generic constraint of IUtf8SpanFormattable. Types like bool, enums, and custom structs do not implement this interface, causing CS0315 in generated code.

Impact

  • bool parameters now emit zero-allocation UTF-8 literals: writer.Write(value ? "true"u8 : "false"u8)
  • enum and custom struct parameters fall back to writer.WriteString(value.ToString())
  • Types implementing IUtf8SpanFormattable (int, double, decimal, etc.) are unchanged
  • Format specifiers on non-formattable types now emit .ToString("spec") instead of failing
  • All generator model types (ParameterInfo, LogMethodInfo, TemplatePart, etc.) made internal — they were incorrectly public

Migration Steps

None required. This is a bug fix — previously broken code will now compile.

Performance Considerations

  • bool: improved — zero allocation via UTF-8 literals instead of ToString()
  • enum/custom structs: ToString() allocation is unavoidable without IUtf8SpanFormattable
  • All IUtf8SpanFormattable types: no change

Security Considerations

None.

Breaking Changes

  • Consumer-facing: None. Generator model types were public but not usable outside the analyzer — making them internal matches their intended usage.
  • Internal: ParameterInfo gained a SerializationKind property (additive).

DJGosnell and others added 3 commits March 5, 2026 14:48
…ucts)

Resolve SerializationKind per parameter at classification time by checking
IUtf8SpanFormattable on the type symbol. Bool emits zero-alloc UTF-8
literals, other non-formattable types fall back to WriteString(ToString()).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
All Models/ types and SerializationKind are generator-internal and should
not be part of the public API surface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@DJGosnell DJGosnell merged commit b9492cc into master Mar 5, 2026
1 check passed
@DJGosnell DJGosnell deleted the fix/non-formattable-type-emission branch March 5, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant