Skip to content

Bug: match move analysis ignores returning arms #579

Closed

Description

@ehartford

Summary

match over a Copy enum field can make the compiler treat the whole owner as moved when one arm consumes the owner but returns from the function. This blocks normal state-machine code where the consuming arm exits and the fallthrough path returns the original state.

match state.mode:
    .Hidden => intents.push(Intent.SetAnimation("hidden"))
    _ =>
        let (next, more) = start_going_hero(state)
        for intent in more:
            intents.push(intent)
        return (next, intents)

(state, intents)

The owner should remain available after the match because the only arm that moves it exits the function.

Version

with v0.15.1-g1f9ec5626

Repro 1

From https://github.com/withlang-dev/smallhold, run:

with run resources/enemies/slime_cyan.w

Relevant source:

pub fn on_restarted(state: SlimeCyanState) -> (SlimeCyanState, Vec[Intent]):
    var intents: Vec[Intent] = Vec.new()
    intents.push(Intent.RemoveNamedSprite("shadow"))
    match state.mode:
        .Hidden => intents.push(Intent.SetAnimation("hidden"))
        _ =>
            let (next, more) = start_going_hero(state)
            for intent in more:
                intents.push(intent)
            intents.push(Intent.ScheduleGroundCheck(300))
            intents.push(Intent.ScheduleHeroCheck(30))
            return (next, intents)
    intents.push(Intent.ScheduleGroundCheck(300))
    intents.push(Intent.ScheduleHeroCheck(30))
    (state, intents)

Actual:

error: use of moved value
 --> resources/enemies/slime_cyan.w:139:6
139 |     (state, intents)
  |      ^^^^^
error: run failed

Repro 2

From the same repo, run:

with run resources/enemies/slime_green.w

Relevant source:

pub fn on_hero_check(state: SlimeState, obs: Observation) -> (SlimeState, Vec[Intent]):
    if obs.hero_distance <= DETECTION_DISTANCE:
        match state.mode:
            .Hidden => return unhide(state)
            _ => ()
    else:
        match state.mode:
            .GoingHero => return hide(state)
            _ => ()
    var intents: Vec[Intent] = Vec.new()
    intents.push(Intent.ScheduleHeroCheck(30))
    (state, intents)

Actual:

error: use of moved value
 --> resources/enemies/slime_green.w:154:15
154 |         match state.mode:
  |               ^^^^^

error: use of moved value
 --> resources/enemies/slime_green.w:155:39
155 |             .GoingHero => return hide(state)
  |                                       ^^^^^

error: use of moved value
 --> resources/enemies/slime_green.w:159:6
159 |     (state, intents)
  |      ^^^^^
error: run failed

The same file also reproduces the post-match return failure in on_restarted.

Expected

  • Matching a Copy enum field such as state.mode should not move state.
  • If one match arm moves state and returns from the function, later code should only need to be valid for the non-returning arms.
  • Multiple conditional matches over the same Copy field should not poison the owner for later use.

Notes

This came up while porting Smallhold resources. I avoided rewriting the state-machine match blocks to if so the compiler issue remains visible.

Activity

  1. ehartford commented on Jun 27, 2026

    @ehartford
    ContributorAuthor

    Fixed by the match-arm move-state union-join (this commit). check_match_expr now snapshots the entry move-state, analyzes each arm from it, and unions only the non-diverging arm exits — so an arm that consumes the owner and returns no longer marks the owner moved on the fallthrough. Verified: test/behavior/behav_match_returning_arm_move.w compiles and runs; full fresh :test green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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