Skip to content

M0114: Ambiguous allocator ​

error

What it means ​

Two or more allocator context parameters are visible at the same level (context(a: Arena, h: Heap)), so the compiler cannot tell which one an allocation should use.

Minimal failing example ​

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

kotlin
// REJECT M0114: two allocators of the same rank in scope. Spec 2.2, 7.14
@file:MemoryMode(Mode.Strict)

package memory.ambiguous_allocator

import mini.memory.*

class Box(val v: Int)

context(a: Arena, h: Heap)
fun both() {
    val b = Box(1)
//          ^ M0114
    println(b.v)
}

fun main() {
    Arena().use { arena -> context(arena, Heap()) { both() } }
}

Compiler output ​

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

text
$ ./minic check compiler/testdata/memory/ambiguous_allocator.kt
error[M0114]: ambiguous allocator for `Box`
  --> compiler/testdata/memory/ambiguous_allocator.kt:12:13   several allocators are visible at the same level
   12 |     val b = Box(1)
      |             ^^^
  help: wrap the allocation in a nested `context(x) { ... }` block to choose one

note: M0114 (Ambiguous allocator): https://henryschwerdt.github.io/mini/reference/diagnostics/M0114

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

How to fix ​

Nest the contexts: context(h) { ... } inside the outer one. The innermost context wins.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.