Skip to content

Issue a warning for receiver-dependent qualifiers on fields or parameters when receiver is invariant #1916

Description

@wmdietl

Summary

When a field, method return type, or parameter is declared with @ReceiverDependentQual (or another receiver-dependent / polymorphic qualifier) in a context where the enclosing class or receiver is invariant (e.g., the class declaration is annotated with a fixed concrete qualifier like @A class C or @A final class C), the receiver-dependent qualifier will always adapt to that fixed qualifier.

Allowing @ReceiverDependentQual in such contexts can be misleading to developers, as it suggests the member varies dynamically with the receiver when it is effectively constant.

Example

@A class InvariantContainer {
    // Misleading: field always viewpoint-adapts to @A Object for instances of InvariantContainer
    @ReceiverDependentQual Object field;

    // Misleading: method return type and parameter always adapt to @A
    @ReceiverDependentQual Object get() { return field; }
    void set(@ReceiverDependentQual Object o) { this.field = o; }
}

Proposed Enhancement

Consider issuing a warning (e.g., under -AwarnRedundantAnnotations or a viewpoint-specific lint):

  1. Static members: When @ReceiverDependentQual is placed on static fields or static methods (where there is no receiver to adapt against).
  2. Fixed final classes: When @ReceiverDependentQual is used in a final class whose qualifier is fixed at class declaration time (e.g. @A final class C).
  3. Monomorphic/Invariant class hierarchies: When a class fixes its qualifier and cannot be subclassed with different qualifiers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions