Lyricdev
GitHub

LYR-CT0007

a macro expansion that does not end

error ยท spec 11-metaprogramming

The expansion of macro sites does not finish: after 32 rounds a macro's result still holds a site, or every macro still to be expanded waits for a module whose own sites wait for it.

Explanation

A macro's result may hold further macro sites, which the next round expands; a macro runs only once its own module has no sites left. A result that holds its own site again, or two macro modules that each wait for the other, never finish.

Let the macro return syntax without the site it was called from, or move the macros so that their modules do not use each other's.

Wrong

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

fn main(): void {
    assert(again!(1) == 1);
}
//- src/macros.lyr
import std.syntax { Expr, parseExpr };

pub macro again(n: Expr): Expr {
    return parseExpr(f"again!({n.toSource()})");
}
//- src/main.lyr
import app.macros { again };

fn main(): void {
    assert(again!(1) == 1);
}
//- src/macros.lyr
import std.syntax { Expr, parseExpr };

pub macro again(n: Expr): Expr {
    return parseExpr(f"({n.toSource()})");
}