Open sandboxFocusImprove this doc

LAMA0883: An immutable type derives from a mutable base type

Severity Warning
Reported by Metalama.Framework
Message '<Type>' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but its base type '<BaseType>' is not immutable. Path: <Path>.

Cause

Aspects, fabrics, validators, and the types that you mark with the [ImmutableType] attribute must be written in an immutable style, because an aspect instance is reused for all its targets. See LAMA0882 for the rationale and for the rules that decide whether a type is immutable.

Metalama reports LAMA0883 when such a type, <Type>, derives from a base class <BaseType> that isn't immutable. A base class is immutable if it's itself bound by the contract, that is, marked with [ImmutableType] or derived from a type that requires immutability, such as OverrideMethodAspect. It's also immutable if Metalama knows it to be immutable, as for System.Attribute, or if the project declares it immutable. The base classes object and System.ValueType are always accepted. An abstract base class that isn't marked is also reported by this diagnostic, not by LAMA0884. <Path> shows the chain from <Type> to <BaseType> and the reason, for example LogAttribute -> CategorizedAttribute (an interface or abstract type that is not marked [ImmutableType]).

The diagnostic is reported on the name of the derived type, and not on the members inherited from the base type, because the author of the derived type can't fix a member declared by the base type.

In the following example, the Log aspect implements IAspect<T> and derives from CategorizedAttribute, a base class shared by several attributes, which isn't marked with [ImmutableType].

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using Metalama.Framework.Utilities;
4using System;
5
6namespace Doc.LAMA0883.Error;
7
8[RunTimeOrCompileTime]
9[Durable]
10public abstract class CategorizedAttribute : Attribute
11{
12    // The compile-time serializer of derived aspects requires a parameterless constructor.
13    protected CategorizedAttribute() : this( "Default" ) { }
14
15    protected CategorizedAttribute( string category )
16    {
17        this.Category = category;
18    }
19
20    public string Category { get; }
21}
22
23// Warning: the aspect derives from a base class that isn't immutable.
Warning LAMA0883: 'LogAttribute' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but its base type 'CategorizedAttribute' is not immutable. Path: LogAttribute -> CategorizedAttribute (an interface or abstract type that is not marked [ImmutableType]).

24public class LogAttribute : CategorizedAttribute, IAspect<IMethod>
25{
26    public LogAttribute( string category ) : base( category ) { }
27
28    public void BuildAspect( IAspectBuilder<IMethod> builder )
29    {
30        builder.Override( nameof(this.OverrideMethod) );
31    }
32
33    [Template]
34    private dynamic? OverrideMethod()
35    {
36        Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
37
38        return meta.Proceed();
39    }
40}
41
Source Code
1namespace Doc.LAMA0883.Error;
2


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


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

Resolution

Choose one of the following options:

  • If you own the base type, mark it with [ImmutableType], as in the following example. Metalama then checks its members with the rules described in LAMA0882, so fix the diagnostics that it reports on them: LAMA0880, LAMA0881, and LAMA0882. Because an aspect is also bound by the [Durable] contract, the base type of an aspect must be marked [Durable] too, otherwise LAMA0873 is reported.
  • If you don't need the base class, derive from an immutable type instead, such as OverrideMethodAspect, or implement the interface directly.
  • If the base type isn't yours and you know that it's immutable, declare it with a MetalamaImmutableType item in the project file, using its full metadata name (see LAMA0885).
  • If the warning is unwanted on one declaration, suppress it with #pragma warning disable LAMA0883.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using Metalama.Framework.Utilities;
4using System;
5
6namespace Doc.LAMA0883.Fixed;
7
8// Fixed: the base class is marked [ImmutableType] and [Durable], so Metalama verifies its members.
9[RunTimeOrCompileTime]
10[ImmutableType]
11[Durable]
12public abstract class CategorizedAttribute : Attribute
13{
14    // The compile-time serializer of derived aspects requires a parameterless constructor.
15    protected CategorizedAttribute() : this( "Default" ) { }
16
17    protected CategorizedAttribute( string category )
18    {
19        this.Category = category;
20    }
21
22    public string Category { get; }
23}
24
25public class LogAttribute : CategorizedAttribute, IAspect<IMethod>
26{
27    public LogAttribute( string category ) : base( category ) { }
28
29    public void BuildAspect( IAspectBuilder<IMethod> builder )
30    {
31        builder.Override( nameof(this.OverrideMethod) );
32    }
33
34    [Template]
35    private dynamic? OverrideMethod()
36    {
37        Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
38
39        return meta.Proceed();
40    }
41}
42
Source Code
1namespace Doc.LAMA0883.Fixed;
2


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


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

See also