Sakura Lang

W8001 copy_on_write

A List, Map or Set update is proven to copy because the earlier version is still held (design §8.2, §17.172): read again after the update, captured by a closure or handler that is still live, or stored in a value that is still live.

A warning from the package phase. sakura explain W8001 prints this explanation offline.

A List, Map or Set update is proven to copy because the earlier version is still held (design §8.2, §17.172): read again after the update, captured by a closure or handler that is still live, or stored in a value that is still live. The primary span is the update; the cause span is that witness. sakura check reports it statically (nothing runs), and only with a witness: an update whose value merely might be shared (a parameter, a call result, a join of two lists) is never warned about, because it may still be updated in place. Values keep their meaning either way; the copy costs time and memory, once per update and so once per iteration in a loop. Repair by ending the earlier version's lifetime before the update where the program allows it (read what you need from it first, or drop the retaining variable), or keep the copy when both versions are wanted. sakura build --dump owned prints each update's verdict and the witness's byte range. Warning:

let parts = String.split("a,b,c", ",")
let more = List.push(parts, "d")
Console.print("{List.len(parts)} {List.len(more)}")

Fixed (read the earlier version first):

let parts = String.split("a,b,c", ",")
let n = List.len(parts)
let more = List.push(parts, "d")
Console.print("{n} {List.len(more)}")

Wrong program

compiler/tests/ui/W8001_borrowed_after.sak, which the compiler's ui suite runs through sakura check:

pub fn main() -> Unit ! {Console} {
  let parts = String.split("a,b,c", ",")
  let more = List.push(parts, "d")
  Console.print("{List.len(parts)} {List.len(more)}")
}

Open in playground

sakura check --json reports this record (pretty-printed; $DIR is the fixture's directory):

{
  "schema_version": "1.0",
  "kind": "diagnostic",
  "id": "d:35301c5bfb709f87",
  "code": "W8001",
  "severity": "warning",
  "phase": "package",
  "message": "this update copies shared `List` storage; `parts` still holds the earlier version",
  "snapshot": "sha256:$SNAPSHOT",
  "symbol_id": "main::W8001_borrowed_after::main",
  "spans": [
    {
      "uri": "file://$DIR/W8001_borrowed_after.sak",
      "content_hash": "sha256:10a7cf4beb4a4aa552f1205470440c40cf35b462cd4a80ebe71a6b9676dcb8a5",
      "start_byte": 255,
      "end_byte": 276,
      "role": "primary"
    },
    {
      "uri": "file://$DIR/W8001_borrowed_after.sak",
      "content_hash": "sha256:10a7cf4beb4a4aa552f1205470440c40cf35b462cd4a80ebe71a6b9676dcb8a5",
      "start_byte": 295,
      "end_byte": 310,
      "role": "cause",
      "label": "`parts` is used again here"
    }
  ],
  "related_ids": [],
  "expected": null,
  "actual": null,
  "obligation": null,
  "counterexample": null,
  "explanation_uri": "sakura://explain/W8001",
  "fixes": []
}

More fixtures for this code: compiler/tests/ui/W8001_captured.sak, compiler/tests/ui/W8001_copy_on_write.sak, compiler/tests/ui/W8001_handler.sak, compiler/tests/ui/W8001_map_retained.sak, compiler/tests/ui/W8001_stored.sak.

Fixed program

compiler/tests/ui/W8001_borrowed_after.fixed.sak, which sakura check accepts with no diagnostics:

pub fn main() -> Unit ! {Console} {
  let parts = String.split("a,b,c", ",")
  let n = List.len(parts)
  let more = List.push(parts, "d")
  Console.print("{n} {List.len(more)}")
}

Open in playground

Edit on GitHub

Last updated

On this page