Skip to content

M0204: No allocator for an escaping Strict value ​

error

What it means ​

In a Strict file there is no default allocator. A value that escapes (is returned, stored, passed as @Owned) needs one from a visible context(...) parameter or from an @AllocatedBy class annotation. Non-escaping values simply live on the stack and need none.

Minimal failing example ​

From the test corpus: compiler/testdata/memory/no_allocator_escape.kt. The corpus expects 10:26: M0204 (the ^ comment in the source marks the column).

kotlin
// REJECT M0204: Strict, escaping allocation, no allocator selectable. Spec 2.2, 7.15
@file:MemoryMode(Mode.Strict)

package memory.no_allocator_escape

import mini.memory.*

class Box(val v: Int)

fun make(): @Owned Box = Box(1)
//                       ^ M0204

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

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/no_allocator_escape.kt
error[M0204]: no allocator for an escaping Strict value
  --> compiler/testdata/memory/no_allocator_escape.kt:10:26   this value escapes but no allocator is selectable
   10 | fun make(): @Owned Box = Box(1)
      |                          ^^^
  help: add `context(h: Heap)` to the function or `@AllocatedBy(Heap::class)` to the class

note: M0204 (No allocator for an escaping Strict value): https://henryschwerdt.github.io/mini/reference/diagnostics/M0204

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

How to fix ​

Add context(h: Heap) to the function, wrap the call in context(Heap()) { ... }, or annotate the class with @AllocatedBy(Heap::class).

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.