E1016: argument does not satisfy the parameter's role

View source

Every parameter has a role, and the call site must state a matching role (ADR-0020).

Every parameter has a role, and the call site must state a matching role (ADR-0020). The argument must also be able to play that role. An ins parameter needs an exclusive loan of an active erg or dat owner. It cannot be given a view, or a binding that is moved, frozen or suspended. An erg parameter needs an active owner that is not frozen, or an expression of scalar type, which is a value (ADR-0126): a literal, an operator expression, a call result, a field or an element, as well as the result of a written copy(value: abs x), or an expression that produces a fresh owned value (ADR-0129): a struct or pack literal, an enum constructor, an array constructor such as Array[Int, 2](), a call whose declared result is owned (not abs). An if or case of aggregate type is not accepted as an argument; bind it first. Such a value belongs to nobody else, so it needs no binding and the callee owns it. A view is still rejected: ref x, an abs binding and a call whose result is abs. An erg parameter also takes over a buffer or an arena, so the caller cannot use it after the call (E1005); a scalar is copied and stays usable. The receiver is an argument too: method(self: ROLE value) follows the same rules, and the spelling value.method() is not a call (E1152). The error points at the argument.

An argument to an erg or dat parameter that is a binding, or a field path of a binding, of a non-scalar type must be written with its role, because the call hands the value over (REQ-OWN-0078, ADR-0060). A scalar and a fresh owned value need no role. For a parameter of generic type the type of the argument decides. The receiver follows the rule like every other argument: the call method(self: dat value) writes the role, and method(self: value) is rejected. The method spelling value.method() was exempt until ADR-0134; it is no longer a call and is rejected with E1152.

A ? that cannot return early from where it stands is reported with the same code (bug #130): a ? in an argument of a verb, a method or an intrinsic such as print, in a case guard, in the index of an assigned element, or in a conditional, a loop or an if statement nested in the arm of a conditional whose value an owner receives. The message names the place, and the error points at the ?. A ? in the value of an if or case arm, on the right of && or ||, in a condition, a case subject or a compound assignment is accepted and returns early.

Example

actus
verb bump(ins value: Int) {
    value += 1;
}

verb main() {
    erg a: Int = 2;
    bump(value: abs a);
}
actus
verb bump(ins value: Int) {
    value += 1;
}

verb relay(abs value: Int) {
    bump(value: ins value);
}

verb main() {
}
actus
enum Fail {
    Bad,
}

verb load() -> Result[Int, Fail] {
    return Result[Int, Fail].Ok(4);
}

verb show() -> Result[Int, Fail] {
    println(int: load()?);
    return Result[Int, Fail].Ok(0);
}

verb main() {
}
actus
struct Point {
    x: Int,
}

verb read(erg point: Point) -> Int {
    return point.x;
}

verb main() -> Int {
    erg point: Point = Point { x: 1, };
    return read(point: erg ref point);
}
actus
verb consume(dat packet: Buffer) {
    drop(packet);
}

verb main() {
    erg buffer: Buffer = Buffer[4];
    consume(packet: buffer);
}
actus
struct Holder {
    erg payload: Buffer,
}

verb consume(dat packet: Buffer) {
    drop(packet);
}

verb main() {
    erg holder = Holder { payload: Buffer[4], };
    consume(packet: holder.payload);
}

Fix

State the role that the parameter declares. When the argument is a view, the forwarding verb must hold the same kind of access, so declare its parameter ins too.

actus check --format json offers this as a fix: write the role keyword the parameter declares in place of the one the call wrote, or in front of an argument that has none, only when the argument can play that role (a review fix, because the role decides who owns the value; a safe fix when the omitted role is erg or dat).

actus
verb bump(ins value: Int) {
    value += 1;
}

verb main() {
    erg a: Int = 2;
    bump(value: ins a);
}
actus
verb bump(ins value: Int) {
    value += 1;
}

verb relay(ins value: Int) {
    bump(value: ins value);
}

verb main() {
}

An omitted erg or dat is written as the parameter declares it. This fix is safe, because it writes the role that the compiler already executed:

actus
verb consume(dat packet: Buffer) {
    drop(packet);
}

verb main() {
    erg buffer: Buffer = Buffer[4];
    consume(packet: dat buffer);
}
actus
struct Holder {
    erg payload: Buffer,
}

verb consume(dat packet: Buffer) {
    drop(packet);
}

verb main() {
    erg holder = Holder { payload: Buffer[4], };
    consume(packet: dat holder.payload);
}

A written copy is an owned value, so it is accepted for an erg parameter:

actus
verb take(erg n: Int) -> Int {
    return n;
}

verb main() -> Int {
    erg a: Int = 1;
    return take(n: copy(value: abs a)) + a;
}

A fresh owned value is accepted without a binding (ADR-0129), and a view is still rejected (see the last example above):

actus
struct Point {
    x: Int,
}

enum Shape {
    Dot(Int),
    Empty,
}

verb make() -> Point {
    return Point { x: 2, };
}

verb read(erg point: Point) -> Int {
    return point.x;
}

verb size(erg shape: Shape) -> Int {
    return 1;
}

verb main() -> Int {
    erg first: Int = read(point: erg Point { x: 1, });
    erg second: Int = read(point: erg make());
    erg third: Int = size(shape: erg Shape.Dot(3));
    return first + second + third;
}

Bind a propagated value before passing it on:

actus
enum Fail {
    Bad,
}

verb load() -> Result[Int, Fail] {
    return Result[Int, Fail].Ok(4);
}

verb show() -> Result[Int, Fail] {
    erg value: Int = load()?;
    println(int: value);
    return Result[Int, Fail].Ok(0);
}

verb main() {
}

Run actus explain E1016 in your terminal to read this entry offline. Browse Ownership & types or all error codes.