Home / Docs / Diagnostics

U3023: Header that grants more than its lock in una.toml

Release v0.4.0

The header of a public function grants more than its lock in una.toml, or from edition 0.4 in ita.una.

Why Una rejects it

From edition 0.3, una.toml can lock the uses of a public function under [contracts], and from edition 0.4 the contracts section of ita.una does (ADR 0092 rule 8). The lock is the consumer's file, so an edit to the function's header cannot widen what the function may do. The header must be at least as strong as its lock:

A header may drop a binding or an operation, strengthen a condition, or read an operation at a higher label: that grants less.

How to fix it

The repair is to bring the header back to its lock. Changing the lock is never the repair.

The repair at a glance

src/demo.unadiff
  module demo

  pub capability Catalog
    op price(sku: Int) -> Int
    op set_price(sku: Int, cents: Int) -> Unit

  pub update(sku: Int, cents: Int, dry_run: Bool) -> Unit
-   uses @shop: Catalog[price, set_price]
+   uses @catalog: Catalog[price, set_price where not dry_run]
  =
-   @shop.set_price(sku, cents)
+   if not dry_run
+     @catalog.set_price(sku, cents)
+   unit

Failing example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"

[contracts]
"demo.update" = ["@catalog: Catalog[price, set_price where not dry_run]"]
src/demo.unauna
module demo

pub capability Catalog
  op price(sku: Int) -> Int
  op set_price(sku: Int, cents: Int) -> Unit

pub update(sku: Int, cents: Int, dry_run: Bool) -> Unit
  uses @shop: Catalog[price, set_price]
=
  @shop.set_price(sku, cents)

Fixed example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"

[contracts]
"demo.update" = ["@catalog: Catalog[price, set_price where not dry_run]"]
src/demo.unauna
module demo

pub capability Catalog
  op price(sku: Int) -> Int
  op set_price(sku: Int, cents: Int) -> Unit

pub update(sku: Int, cents: Int, dry_run: Bool) -> Unit
  uses @catalog: Catalog[price, set_price where not dry_run]
=
  if not dry_run
    @catalog.set_price(sku, cents)
  unit