Skip to content

M0105: Escape from a Scoped allocator ​

error

What it means ​

A value allocated in an Arena leaves the scope of that arena: it is returned, becomes the result of the use {} block, is stored in a field of a longer-lived object or a global, or is captured by a lambda that outlives the arena.

Minimal failing example ​

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

kotlin
// REJECT M0105: `return` carries an arena object out of the arena scope. Spec 2.4, 7.5
@file:MemoryMode(Mode.Strict)

package memory.arena_escape_return

import mini.memory.*

class Box(val v: Int)

fun build(): Box {
    Arena().use { arena ->
        context(arena) {
            return Box(1)
//                 ^ M0105
        }
    }
}

fun main() {
    println(build().v)
}

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/arena_escape_return.kt
error[M0105]: value allocated in arena `arena` escapes its scope
  --> compiler/testdata/memory/arena_escape_return.kt:13:20   escapes the scope of the arena here
   13 |             return Box(1)
      |                    ^^^
  help: return a value without allocation (e.g. an `Int`), or move the arena into the caller (`context(a: Arena) fun ...`)

note: M0105 (Escape from a Scoped allocator): https://henryschwerdt.github.io/mini/reference/diagnostics/M0105

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

How to fix ​

Return a value that needs no allocation (Int, Boolean, ...), copy the data out, or let the caller own the arena: declare the function with context(a: Arena) so that the objects live in the caller's region.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.