Lyricdev
GitHub

LYR-SEM0188

a macro's result does not fit its site

error ยท spec 11-metaprogramming

A macro that gives statements back (Stmt, Stmt[], Block) stands where an expression is needed, or a macro gives back a kind (an Ident, a Type, a declaration) that no expression or statement site takes.

Explanation

What a macro gives back replaces its site. An expression site takes an Expr; a site that is a statement of its own also takes Stmt, Stmt[] or Block. Declarations come from attribute macros only, @name on the declaration they rewrite.

Write a statement-giving macro as a statement of its own, or change what the macro returns.

Wrong

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

fn main(): void {
    var n = 0;
    let x = run! { n += 1; };
    assert(n == 1);
}
//- src/macros.lyr
import std.syntax { Block };

pub macro run(body: Block): Block {
    return body;
}
//- src/main.lyr
import app.macros { run };

fn main(): void {
    var n = 0;
    run! { n += 1; }
    assert(n == 1);
}
//- src/macros.lyr
import std.syntax { Block };

pub macro run(body: Block): Block {
    return body;
}