Open sandboxFocusImprove this doc

LAMA0888: A member of an immutable type is passed as an out argument

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 '<Member>' is passed as an 'out' argument here, which writes it. Pass a local instead, and assign the member in a constructor.

Cause

Aspects, fabrics and validators must be written in an immutable style, because Metalama can share one instance among several threads and several compilations. A type is subject to this rule when it carries the [ImmutableType] attribute or when it derives from, or implements, a type that requires immutability, such as IAspect or Fabric.

An out argument is a write, but it doesn't look like an assignment, so it's easy to miss. Metalama reports LAMA0888 when an instance field or automatic property of such a type is passed as an out argument, for example int.TryParse( text, out this._value ), outside a constructor or an init accessor of the declaring type. In the message, <Type> is the type that must be immutable and <Member> is the name of the field or property that is passed.

An out argument in a constructor is part of the construction and isn't reported. Metalama doesn't report ref arguments either, because the called method may only read the value, as Volatile.Read does. A direct assignment is reported by LAMA0887.

In the following example, the Log aspect passes a private field as the out argument of IProject.TryGetProperty in BuildAspect.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0888.Error;
6
7public class LogAttribute : MethodAspect
8{
9    private string? _prefix;
10
11    public override void BuildAspect( IAspectBuilder<IMethod> builder )
12    {
13        // Warning: the aspect field is passed as an 'out' argument outside a constructor.
        Warning LAMA0888: '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 '_prefix' is passed as an 'out' argument here, which writes it. Pass a local instead, and assign the member in a constructor.

14        builder.Project.TryGetProperty( "LogPrefix", out this._prefix );
15
16        builder.Override( nameof(this.LogTemplate) );
17    }
18
19    [Template]
20    public dynamic? LogTemplate()
21    {
22        var prefix = this._prefix ?? "Entering";
23        Console.WriteLine( $"{prefix} {meta.Target.Method}." );
24
25        return meta.Proceed();
26    }
27}
28
Source Code
1namespace Doc.LAMA0888.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.LAMA0888.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

Pass a local variable as the out argument. If the value is only needed by the template, pass the local variable to the template as an argument instead of storing it in the aspect. Otherwise, assign the member from the local variable in a constructor. If the value must change after construction, don't keep it in the aspect, fabric or validator.

In the following example, BuildAspect passes a local variable to TryGetProperty, and passes the result to the template through a compile-time parameter.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0888.Fixed;
6
7public class LogAttribute : MethodAspect
8{
9    public override void BuildAspect( IAspectBuilder<IMethod> builder )
10    {
11        // Fixed: the 'out' argument is a local, which is passed to the template as an argument.
12        var prefix = builder.Project.TryGetProperty( "LogPrefix", out var value ) ? value : "Entering";
13
14        builder.Override( nameof(this.LogTemplate), args: new { prefix } );
15    }
16
17    [Template]
18    public dynamic? LogTemplate( [CompileTime] string prefix )
19    {
20        Console.WriteLine( $"{prefix} {meta.Target.Method}." );
21
22        return meta.Proceed();
23    }
24}
25
Source Code
1namespace Doc.LAMA0888.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.LAMA0888.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