Class GuardedAssignmentPromotion

java.lang.Object
org.ek9lang.compiler.phase7.support.GuardedAssignmentPromotion

public final class GuardedAssignmentPromotion extends Object
THE #^ promotion of a guarded assignment's right-hand side - use this, do not re-derive it.

Phase 3 deliberately permits coercion in a guard: ?= is a member of OperationIsAssignment, so TypesCompatibleOrError accepts any getCostOfMatch >= 0. Character -> String is coercible, so t ?= charSource() with t as String is a clean compile BY DESIGN. Phase 3 resolves the coercion; phase 7 must insert the _promote() call.

Where it is skipped, the RHS's declared type is stored into a slot typed by the target's declared type and the class fails JVM verification at load: "Type 'org/ek9/lang/Character' is not assignable to 'org/ek9/lang/String'". That is a clean compile followed by a hard failure, for a construct that is the language's designated replacement for early return.

Three copies of the guard lowering existed and only the if one promoted, so a guard under while, try, for and switch miscompiled while the identical guard under if ran correctly. This is the single place that decision is now made.

  • Constructor Details

    • GuardedAssignmentPromotion

      public GuardedAssignmentPromotion(PromotionCallGenerator promotionCallGenerator)
  • Method Details

    • promoteIfRequired

      public String promoteIfRequired(List<IRInstr> target, ISymbol rhsSymbol, ISymbol targetSymbol, String rhsVariable, DebugInfo debugInfo)
      Promote the guarded RHS to the target's declared type when the two differ but are coercible.
      Parameters:
      target - instruction list receiving any promotion call
      rhsSymbol - the resolved symbol of the guard expression's right-hand side
      targetSymbol - the resolved symbol of the guard's target identifier
      rhsVariable - the IR variable currently holding the RHS value
      debugInfo - debug info for any emitted promotion
      Returns:
      the variable to store - the promoted one where promotion applied, else the original