| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | '<Type>' is loaded in the compiler process, but its static member '<Member>' is of type '<MemberType>', which is not durable. A static field of a compile-time type is shared by every aspect instance, every target and every compilation, and at design time it keeps its value for as long as the analysis process runs. The member must therefore not reference a compilation, a syntax tree, a symbol or a code model declaration. If its value references none of these objects, mark the member or its type with [Durable]. Path: <Path>. |
Cause
Compile-time code, such as aspects, fabrics, and types marked [CompileTime] or [RunTimeOrCompileTime], is loaded in the compiler process. A static field of such a type keeps its value for as long as the assembly is loaded. At design time, that's as long as the IDE's analysis process runs, while Roslyn creates a new compilation on almost every keystroke. A static member that references a compilation, a syntax tree, a Roslyn symbol, or a code model declaration therefore keeps an old compilation, and every syntax tree of the project, in memory.
An immutable value can still hold such a reference: a symbol, a syntax node, a code model type, or a delegate can't change, but it keeps alive everything it references. That's why Metalama checks the static members of compile-time code for durability in addition to immutability. A type is durable when it's safe to keep across compilations: intrinsic types such as int and string, immutable collections of durable types, IDurableRef, and types marked with [Durable]. Durability is opt-in: a type of your own isn't durable unless you mark it.
Metalama reports LAMA0892 on a static field or static automatic property of a compile-time or run-time-or-compile-time type when all of the following conditions hold:
- The member is read-only and its type is immutable. Otherwise, Metalama reports LAMA0889, LAMA0890, or LAMA0891 instead, and only one of these diagnostics is reported for a member.
- The member type isn't durable, and the member itself isn't marked
[Durable].
Constants, templates, and introduced members aren't checked. In a run-time-or-compile-time type, a member marked [RunTime] isn't checked either, because it doesn't exist in the compile-time assembly.
In the message, <Type> is the type that declares the member, <Member> is its name, and <MemberType> is its declared type. <Path> is the chain of members and types from <Type> to the type that isn't durable, followed by the reason in parentheses, for instance the type is not marked [Durable] or a delegate holds its target and everything its closure captured.
In the following example, the Log aspect keeps its default message format in a static field. The LogFormat type is immutable, but it isn't marked [Durable], so Metalama can't tell whether it references a compilation.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Utilities;
3using System;
4
5namespace Doc.LAMA0892.Error;
6
7// An immutable compile-time type, which isn't marked [Durable].
8[CompileTime]
9[ImmutableType]
10public sealed class LogFormat
11{
12 public LogFormat( string prefix )
13 {
14 this.Prefix = prefix;
15 }
16
17 public string Prefix { get; }
18}
19
20public class LogAttribute : OverrideMethodAspect
21{
22 // Warning: the static field outlives every compilation, but LogFormat isn't known to be durable.
Warning LAMA0892: 'LogAttribute' is loaded in the compiler process, but its static member '_defaultFormat' is of type 'LogFormat', which is not durable. A static field of a compile-time type is shared by every aspect instance, every target and every compilation, and at design time it keeps its value for as long as the analysis process runs. The member must therefore not reference a compilation, a syntax tree, a symbol or a code model declaration. Path: LogAttribute -> _defaultFormat -> LogFormat (the type is not marked [Durable]).
23 private static readonly LogFormat _defaultFormat = new( "Entering" );
24
25 public override dynamic? OverrideMethod()
26 {
27 Console.WriteLine( $"{_defaultFormat.Prefix} {meta.Target.Method}." );
28
29 return meta.Proceed();
30 }
31}
32
1namespace Doc.LAMA0892.Error;
2
3internal class Calculator
4{
5 [Log]
6 public int Add( int a, int b ) => a + b;
7}
8
1using System;
2
3namespace Doc.LAMA0892.Error;
4
5internal class Calculator
6{
7 [Log]
8 public int Add(int a, int b)
9 {
10 Console.WriteLine("Entering Calculator.Add(int, int).");
11 return a + b;
12 }
13}
14
Resolution
Choose the fix that matches what the member really holds:
- The type holds only durable data. If the type is yours, mark it with [Durable]. The Metalama analyzer then verifies that its instance members are durable in turn. If you don't own the type, declare it durable with the
MetalamaDurableTypeMSBuild item. - The member's declared type is broader than its value. For a delegate that captures nothing, or a member typed as an interface or
object, mark the member itself with[Durable]. This waives the check on the declared type, and the analyzer verifies instead that every value assigned to the member is durable. - The member references a compilation, a symbol, or a declaration. Don't keep this object in a static member. Store a durable identifier instead, such as an IDurableRef, and resolve it in the current compilation when you need it. Or compute the value in the aspect's
BuildAspectmethod or in the template, where it's tied to the current compilation.
In the following example, LogFormat holds only a string, so it's marked [Durable].
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Utilities;
3using System;
4
5namespace Doc.LAMA0892.Fixed;
6
7// Fixed: LogFormat holds only a string, so it's marked [Durable].
8[CompileTime]
9[ImmutableType]
10[Durable]
11public sealed class LogFormat
12{
13 public LogFormat( string prefix )
14 {
15 this.Prefix = prefix;
16 }
17
18 public string Prefix { get; }
19}
20
21public class LogAttribute : OverrideMethodAspect
22{
23 private static readonly LogFormat _defaultFormat = new( "Entering" );
24
25 public override dynamic? OverrideMethod()
26 {
27 Console.WriteLine( $"{_defaultFormat.Prefix} {meta.Target.Method}." );
28
29 return meta.Proceed();
30 }
31}
32
1namespace Doc.LAMA0892.Fixed;
2
3internal class Calculator
4{
5 [Log]
6 public int Add( int a, int b ) => a + b;
7}
8
1using System;
2
3namespace Doc.LAMA0892.Fixed;
4
5internal class Calculator
6{
7 [Log]
8 public int Add(int a, int b)
9 {
10 Console.WriteLine("Entering Calculator.Add(int, int).");
11 return a + b;
12 }
13}
14