Skip to content

Generated type expressions still lose keyword identifier escapes #7674

Closed

Description

@thomhurst

Generated C# type expressions outside attribute constructor names still use a display format that omits keyword escapes. For example, a type in namespace @event can be emitted as typeof(global::event.Payload), which is invalid C#. Valid keyword identifiers should remain escaped wherever the source generator emits C# expressions.

Reproduction

Add a generator regression case with namespace @event { public class Payload {} } and an attribute with a System.Type named argument set to typeof(@event.Payload). Exercise the metadata-backed attribute writer and compile the generated initializer.

At main commit 885ee5f, src/TUnit.Core.SourceGenerator/CodeGenerators/Formatting/TypedConstantFormatter.cs:49 emits typeof({type.GloballyQualified()}). TypeExtensions.GloballyQualified uses DisplayFormats.FullyQualifiedGenericWithGlobalPrefix, which omits EscapeKeywordIdentifiers. Other expression emitters, including InstanceFactoryGenerator, use the same helper. This is a source inspection finding; this additional reproduction has not been executed.

Expected and actual

Generated expressions should preserve @event; the current formatter drops the escape. Keep raw runtime reflection names unchanged.

Proposal

Audit C# expression emission sites and add compilation regressions for keyword namespaces and type names, including attribute type values. Distinguish expression formatting from runtime-name formatting rather than changing every raw-name consumer.

Follow-up to the non-blocking Claude review on #7669; that PR fixes attribute constructor names only. Please preserve keyword escapes in the remaining generated C# type expressions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low: cleanup, minor enhancement, or rare edge casebugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions