Lyricdev
GitHub

LYR-MAC0001

an error a macro reports

error ยท spec 11-metaprogramming

A macro called error(at, message) of std.syntax while it expanded: the macro refuses its arguments. The diagnostic stands at the node the macro named, with a note at the site's !.

Explanation

A macro checks what it is given and refuses what it cannot expand with its own message, at the place in the program the message is about. The message is the macro's; its documentation says what it takes.

Change the arguments of the macro site as the message asks.

Wrong

//- src/main.lyr
import app.macros { positive };

fn main(): void {
    assert(positive!(-1) < 0);
}
//- src/macros.lyr
import std.syntax { Expr, error };

pub macro positive(n: Expr): Expr {
    match (n) {
        .Unary(.Neg, _, _) => { error(n.span(), "positive! takes no negative literal"); },
        _ => { },
    }
    return n;
}
//- src/main.lyr
import app.macros { positive };

fn main(): void {
    assert(positive!(1) > 0);
}
//- src/macros.lyr
import std.syntax { Expr, error };

pub macro positive(n: Expr): Expr {
    match (n) {
        .Unary(.Neg, _, _) => { error(n.span(), "positive! takes no negative literal"); },
        _ => { },
    }
    return n;
}