| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | '<Type>' is a compile-time type, but the static member '<Member>' is of type '<MemberType>', which is not immutable. 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. Use an immutable type, such as an immutable collection. Path: <Path>. |
Cause
A static field of a compile-time type is shared by every aspect instance, every target, and every compilation that the compiler or IDE process handles (see LAMA0889). The readonly modifier prevents only the reassignment of the field. If the type of the field is mutable, such as List<string>, Dictionary<string, int>, or an array, any code can still change the object that the field references, and the change leaks into the following compilations.
Metalama reports LAMA0890 on a static field or static automatic property of a compile-time type that is read-only but whose type isn't immutable. A type is immutable when it's a primitive type, string, an enum, a delegate, an immutable collection such as ImmutableArray<T> or ImmutableDictionary<TKey, TValue>, another well-known immutable type, a type marked with [ImmutableType], or a type declared immutable with a MetalamaImmutableType item in the project file. Nullable types and tuples are immutable when their components are. Arrays, object, and dynamic are never immutable.
In the message, <Type> is the compile-time type, <Member> is the member, <MemberType> is its type, and <Path> is the chain from <Type> to the mutable type, followed by the reason why that type isn't immutable. A member that isn't read-only is reported only once, with LAMA0889.
In the following example, a [CompileTime] class used by the Log aspect stores the names of the excluded methods in a List<string>.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6
7namespace Doc.LAMA0890.Error;
8
9[CompileTime]
10internal static class LogSettings
11{
12 // Warning: the field is read-only, but a List<string> can still be modified.
Warning LAMA0890: 'LogSettings' is a compile-time type, but the static member 'ExcludedMethods' is of type 'List<string>', which is not immutable. 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. Path: LogSettings -> ExcludedMethods -> List<string> (a mutable collection; use the corresponding type of System.Collections.Immutable).
13 public static readonly List<string> ExcludedMethods = new() { "Dispose", "ToString" };
14}
15
16public class LogAttribute : TypeAspect
17{
18 public override void BuildAspect( IAspectBuilder<INamedType> builder )
19 {
20 foreach ( var method in builder.Target.Methods.Where( m => !LogSettings.ExcludedMethods.Contains( m.Name ) ) )
21 {
22 builder.With( method ).Override( nameof(this.OverrideMethod) );
23 }
24 }
25
26 [Template]
27 public dynamic? OverrideMethod()
28 {
29 Console.WriteLine( $"Entering {meta.Target.Method}." );
30
31 return meta.Proceed();
32 }
33}
34
1using System;
2
3namespace Doc.LAMA0890.Error;
4
5[Log]
6internal class OrderService : IDisposable
7{
8 public void PlaceOrder( string product, int quantity )
9 {
10 // Places the order.
11 }
12
13 public void Dispose() { }
14}
15
1using System;
2
3namespace Doc.LAMA0890.Error;
4
5[Log]
6internal class OrderService : IDisposable
7{
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14 public void Dispose() { }
15}
16
Resolution
Choose one of the following options:
- Replace the mutable type with an immutable one:
ImmutableArray<T>instead ofList<T>or an array,ImmutableDictionary<TKey, TValue>instead ofDictionary<TKey, TValue>, orImmutableHashSet<T>instead ofHashSet<T>. The following example usesImmutableArray<string>. - If the type is yours and is immutable, mark it with
[ImmutableType]. Metalama then verifies the type itself. - If the type isn't yours and you know that it's immutable, declare it with a
MetalamaImmutableTypeitem in the project file (see LAMA0885). - If the member is deliberately a thread-safe cache whose values don't depend on the compilation, suppress the warning with
#pragma warning disable LAMA0890around the declaration.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4using System.Collections.Immutable;
5using System.Linq;
6
7namespace Doc.LAMA0890.Fixed;
8
9[CompileTime]
10internal static class LogSettings
11{
12 // Fixed: an ImmutableArray<string> can't be modified once it's created.
13 public static readonly ImmutableArray<string> ExcludedMethods = ImmutableArray.Create( "Dispose", "ToString" );
14}
15
16public class LogAttribute : TypeAspect
17{
18 public override void BuildAspect( IAspectBuilder<INamedType> builder )
19 {
20 foreach ( var method in builder.Target.Methods.Where( m => !LogSettings.ExcludedMethods.Contains( m.Name ) ) )
21 {
22 builder.With( method ).Override( nameof(this.OverrideMethod) );
23 }
24 }
25
26 [Template]
27 public dynamic? OverrideMethod()
28 {
29 Console.WriteLine( $"Entering {meta.Target.Method}." );
30
31 return meta.Proceed();
32 }
33}
34
1using System;
2
3namespace Doc.LAMA0890.Fixed;
4
5[Log]
6internal class OrderService : IDisposable
7{
8 public void PlaceOrder( string product, int quantity )
9 {
10 // Places the order.
11 }
12
13 public void Dispose() { }
14}
15
1using System;
2
3namespace Doc.LAMA0890.Fixed;
4
5[Log]
6internal class OrderService : IDisposable
7{
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14 public void Dispose() { }
15}
16