Class GuardedAssignmentNeverAppliesOrError

java.lang.Object
org.ek9lang.compiler.common.RuleSupport
org.ek9lang.compiler.common.TypedSymbolAccess
org.ek9lang.compiler.phase5.GuardedAssignmentNeverAppliesOrError
All Implemented Interfaces:
Consumer<EK9Parser.AssignmentStatementContext>

final class GuardedAssignmentNeverAppliesOrError extends TypedSymbolAccess implements Consumer<EK9Parser.AssignmentStatementContext>
Detects guarded assignments (:=?) that can never apply (E08095).

:=? assigns ONLY when the target is unset. When the target is provably SET at that point, the assignment is dead and the right-hand side is silently discarded - the code compiles clean and does nothing. This is the same ValueTrackingAnalyzer question already asked by RedundantIsSetCheckOrError, because :=? is an isSet check plus a conditional assign.

The motivating case is a record property initialised to a set value and then assigned in a constructor - which, in a pure constructor, is the only assignment form permitted (E08100), so the mistake is easy to make and invisible:

  Connections
    up as Boolean: false     // SET
    Connections() as pure
      -> isUp as Boolean
      up :=? isUp            // E08095 - never applies, isUp is thrown away

Only the provably-SET case is an error. A provably-UNSET target is the correct and expected use of :=? (it is how a pure constructor initialises its fields), and UNKNOWN stays silent so that no false positive can be raised - EK9 has no warnings, so an error must be certain.

Conditions inside loop bodies are suppressed because widening has not yet been applied, mirroring RedundantIsSetCheckOrError.