Skip to content

M0104: Use after move ​

error

What it means ​

A value is used after ownership moved away from it: through x.move(), by passing it to an @Owned parameter, by storing it in an owning field, by capturing it in a thread lambda, or by a use {}/close() that consumed it. A move inside a loop that is hit again on the next iteration is reported too.

Minimal failing example ​

From the test corpus: compiler/testdata/memory/move_explicit.kt. The corpus expects 14:13: M0104 (the ^ comment in the source marks the column).

kotlin
// REJECT M0104: `val b = a.move()` moves; `a` is dead afterwards. Spec 5.1, 7.4
@file:MemoryMode(Mode.Strict)

package memory.move_explicit

import mini.memory.*

class Box(val v: Int)

context(h: Heap)
fun run() {
    val a = Box(1)
    val b = a.move()
    println(a.v)
//          ^ M0104
    b.free()
}

fun main() {
    context(Heap()) { run() }
}

Compiler output ​

Generated by running ./minic check compiler/testdata/memory/move_explicit.kt (exit status 1).

text
$ ./minic check compiler/testdata/memory/move_explicit.kt
error[M0104]: use of moved value `a`
  --> compiler/testdata/memory/move_explicit.kt:14:13   `a` was moved before
   14 |     println(a.v)
      |             ^
  help: use the new owner instead, or write `val b = a` (alias) when no move is intended

note: M0104 (Use after move): https://henryschwerdt.github.io/mini/reference/diagnostics/M0104

error: could not compile move_explicit.kt (1 error, 0 warnings)

How to fix ​

Use the new owner instead of the old variable, or write val b = a (a borrowed alias) if no transfer was intended. Inside loops, re-initialise the variable before the back edge.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.