Repository navigation
Description
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
- No static field in
.Generatorshas a Metalama code-model type (INamedType,IType,IMethod,IExpression,AttributeConstructionand the like), checked by a conventions test in the manner ofCodeModelLookupTests. The test is shown to fail with the seven fields restored. - The generated code is unchanged: the generated-surface snapshot and the woven probes pass as before.
- If Metalama ships a detector for the pattern (#2307), the build uses it as well. (2026-10-28: Report non-durable or mutable data in static fields of compile-time code metalama/Metalama#2307 is still open; PR fix: CodeAnnotations builds its attribute constructions on use, not in static fields #102 lands the rest.)
Activity
CaffeinatedCoder commentedon Oct 28, 2026
Done in PR #102, merged into release/2.3.0.