Open sandboxFocusImprove this doc

LAMA0889: A static field of a compile-time type must be read-only

Severity Warning
Reported by Metalama.Framework
Message '<Type>' is a compile-time type, but the static member '<Member>' is not read-only. 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. Add the 'readonly' modifier to the field, remove the setter of the property, or remove the static event.

Cause

Compile-time code is loaded into the compiler process, and at design time into the IDE's analysis process, which lives much longer than one compilation. A static field of a compile-time type therefore isn't per target or per build: it's shared by every aspect instance, every target declaration, and every compilation that the process handles. A value that you assign to it in one compilation is still there in the next one, and concurrent compilations see each other's writes. The result then depends on the order in which Metalama processes the code, which you don't control.

That's why Metalama requires the static state of compile-time code to be read-only and immutable. Metalama reports LAMA0889 on the declaration of a static member of a compile-time type that can be assigned after its initialization:

  • a static field that isn't readonly or const,
  • a static automatic property that has a setter or an init accessor,
  • a static field-like event, because each subscription assigns its backing field.

A compile-time type is a type marked with [CompileTime], a type of an assembly marked [CompileTime], a nested type of a compile-time type, or a fabric. The rule doesn't depend on the [ImmutableType] contract, so it also applies to helper classes. Template members, introduced members, and const fields aren't reported. Static members of aspects and of other run-time-or-compile-time types are reported with LAMA0891.

In the message, <Type> is the declaring type and <Member> is the name of the field, property, or event.

In the following example, a [CompileTime] class used by the Log aspect exposes its prefix as a writable static field.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0889.Error;
5
6[CompileTime]
7internal static class LogSettings
8{
9    // Warning: a static field of a compile-time type must be read-only.
    Warning LAMA0889: 'LogSettings' is a compile-time type, but the static member 'Prefix' is not read-only. 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. Add the 'readonly' modifier to the field, or remove the setter of the property.

10    public static string Prefix = "Entering";
11}
12
13public class LogAttribute : OverrideMethodAspect
14{
15    public override dynamic? OverrideMethod()
16    {
17        Console.WriteLine( $"{LogSettings.Prefix} {meta.Target.Method}." );
18
19        return meta.Proceed();
20    }
21}
22
Source Code
1namespace Doc.LAMA0889.Error;
2


3internal class OrderService
4{
5    [Log]
6    public void PlaceOrder( string product, int quantity )
7    {
8        // Places the order.
9    }


10}
11
Transformed Code
1using System;
2
3namespace Doc.LAMA0889.Error;
4
5internal class OrderService
6{
7    [Log]
8    public void PlaceOrder(string product, int quantity)
9    {
10        Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12        // Places the order.
13    }
14}
15

Resolution

Choose one of the following options:

  • Add the readonly modifier to the field, or make it const. This is the usual fix when the value is a setting that never changes, as in the following example.
  • Remove the setter of the automatic property, and initialize the property in its declaration or in the static constructor.
  • Remove the static event.
  • If the value must vary, don't keep it in a static member. Pass it through the properties of the aspect or through aspect options, or compute it from the code model in BuildAspect.

Once the member is read-only, its type must also be immutable, otherwise Metalama reports LAMA0890.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0889.Fixed;
5
6[CompileTime]
7internal static class LogSettings
8{
9    // Fixed: the field is read-only, so its value can't change between compilations.
10    public static readonly string Prefix = "Entering";
11}
12
13public class LogAttribute : OverrideMethodAspect
14{
15    public override dynamic? OverrideMethod()
16    {
17        Console.WriteLine( $"{LogSettings.Prefix} {meta.Target.Method}." );
18
19        return meta.Proceed();
20    }
21}
22
Source Code
1namespace Doc.LAMA0889.Fixed;
2


3internal class OrderService
4{
5    [Log]
6    public void PlaceOrder( string product, int quantity )
7    {
8        // Places the order.
9    }


10}
11
Transformed Code
1using System;
2
3namespace Doc.LAMA0889.Fixed;
4
5internal class OrderService
6{
7    [Log]
8    public void PlaceOrder(string product, int quantity)
9    {
10        Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12        // Places the order.
13    }
14}
15

See also