Open sandboxFocusImprove this doc

LAMA0883: An immutable type derives from a mutable base type

Severity Warning
Reported by Metalama.Framework (Roslyn analyzer)
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.

An analyzer in the Metalama.Framework package reports LAMA0883 when such a type, <Type>, derives from a base class <BaseType> that isn't immutable. A base class is immutable only if it is marked with [ImmutableType], or if the analyzer knows it to be immutable, as for System.Attribute. 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 LoggingAspect -> AspectBase (the type is not marked [ImmutableType]).

The diagnostic is reported on 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.

If the base type is in your code, marking it with [ImmutableType] is enough to make the analyzer verify it. The analyzer then checks its members with the rules described in LAMA0882.

This diagnostic is reported by a Roslyn analyzer, which doesn't run in the snapshot tests of this documentation, so it can't be shown in an example.

Resolution

  • If you own the base type, mark it with [ImmutableType] and fix the diagnostics that the analyzer then reports on its members: LAMA0880, LAMA0881 and LAMA0882.
  • If you don't need the base class, derive from an immutable type instead, 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.

See also