Skip to content

M0113: Owned field stays moved ​

error

What it means ​

A value was moved out of an owning field with s.item.move() and the field was not given a valid value again before the next call or the end of the function.

Minimal failing example ​

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

kotlin
// REJECT M0113: an owned field is moved out and not restored before the function ends. Spec 5.3, 7.13
@file:MemoryMode(Mode.Strict)

package memory.field_left_moved

import mini.memory.*

class Box(val v: Int)

class Slot(var item: Box?)

fun steal(s: Slot): @Owned Box? {
    val b = s.item.move()
//          ^ M0113
    return b
}

fun main() {
    context(Heap()) {
        val s = Slot(Box(1))
        val b = steal(s)
        b.free()
        s.free()
    }
}

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/field_left_moved.kt
error[M0113]: field `s.item` is moved out and not restored
  --> compiler/testdata/memory/field_left_moved.kt:13:13   moved out here
   13 |     val b = s.item.move()
      |             ^
  help: assign `s.item = ...` (or `null`) before calls and before the function ends

note: M0113 (Owned field stays moved): https://henryschwerdt.github.io/mini/reference/diagnostics/M0113

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

How to fix ​

Assign the field again (a new value or null) before the function returns or calls anything else.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.