Skip to content

Generators: CodeAnnotations keeps Metalama attribute constructions in static fields #90

Closed

Description

@CaffeinatedCoder

CodeAnnotations (src/CodoMetis.TypeKit.Generators/CodeAnnotations.cs) holds seven static readonly AttributeConstruction fields (AggressiveInlining, CallerArgumentExpressionOfValue, CompilerGenerated, DebuggerHidden, DebuggerStepThrough, EditorNonBrowsable, TranslatedAsWrappedValue), referenced on 78 lines in the aspects, as in AddAttribute(CodeAnnotations.CompilerGenerated).

Gaël Fraiteur of Metalama pointed this out when he reviewed the project in the Show and tell discussion (discussion 2305, his comment). An AttributeConstruction holds compilation-context-sensitive data, and a static field outlives the compilation. Static fields in compile-time code should hold durable, immutable data only. At design time, where the IDE keeps its process across edits, he expects this to cause a limited memory leak. He opened metalama/Metalama#2307 to detect the pattern. Thank you for the careful read.

No failure has been observed and the effect has not been measured here, so the severity is provisional.

Proposal

Make each of the seven a static property that builds the construction where it is used, for example public static AttributeConstruction CompilerGenerated => AttributeConstruction.Create(typeof(CompilerGeneratedAttribute));. The 78 references do not change.

The other static fields in .Generators hold no code-model objects: the DiagnosticDefinitions (ids and format strings), two tables of strings and Types, and one string list.

Done when

Activity

  1. added this to the 2.3.0 milestone on Oct 28, 2026
  2. added
    bug
    severity/medium
    package/generators
    on Oct 28, 2026
  3. CaffeinatedCoder commented on Oct 28, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Done in PR #102, merged into release/2.3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpackage/generatorsseverity/medium

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions