Skip to content

M0205: Allocation in a @NoAlloc function ​

error

What it means ​

A function marked @NoAlloc contains an allocation that is not on the stack (it escapes) or calls alloc.allocate(...). Only the function's own body is checked; calls to other functions are not followed yet.

Minimal failing example ​

From the test corpus: compiler/testdata/memory/noalloc_alloc.kt. The corpus expects 12:26: M0205 (the ^ comment in the source marks the column).

kotlin
// REJECT M0205: an escaping allocation in a @NoAlloc function. Spec 8, 7.16
@file:MemoryMode(Mode.Strict)

package memory.noalloc_alloc

import mini.memory.*

class Box(val v: Int)

@NoAlloc
context(h: Heap)
fun make(): @Owned Box = Box(1)
//                       ^ M0205

fun main() {
    context(Heap()) { make().free() }
}

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/noalloc_alloc.kt
error[M0205]: allocation in `@NoAlloc` function `make`
  --> compiler/testdata/memory/noalloc_alloc.kt:12:26   allocates here
   12 | fun make(): @Owned Box = Box(1)
      |                          ^^^
  help: remove `@NoAlloc` or avoid the escaping allocation

note: M0205 (Allocation in a @NoAlloc function): https://henryschwerdt.github.io/mini/reference/diagnostics/M0205

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

How to fix ​

Take the object as a parameter, keep it non-escaping so that it lives on the stack, or remove @NoAlloc.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.