1
//! DEFMACRO and macro expansion codegen. Macros are expanded at
2
//! compile time by `expand_macro` (in expr.rs) and the expansion is
3
//! re-dispatched into `compile_for_effect` / `compile_for_stack`.
4

            
5
use super::common::{compile_and_validate, compile_expect_error};
6

            
7
#[test]
8
1
fn defmacro_and_simple_expansion() {
9
1
    compile_and_validate("(defmacro inc (x) `(+ ,x 1)) (inc 5)");
10
1
}
11

            
12
#[test]
13
1
fn defmacro_with_unquote_splicing() {
14
1
    compile_and_validate("(defmacro list-of (&rest xs) `(list ,@xs)) (list-of 1 2 3)");
15
1
}
16

            
17
/// An expansion is code: a list datum with symbol members, a `$pair` chain of
18
/// `$symbol` cells (plan 0014 Stage 8). The expansion itself still drives the
19
/// walk — a non-macro form or an atom is answered too.
20
#[test]
21
1
fn macroexpand_results_are_symbol_lists() {
22
1
    for src in [
23
1
        "(defmacro inc (x) `(+ ,x 1)) (macroexpand-1 '(inc 5))",
24
1
        "(defmacro inc (x) `(+ ,x 1)) (macroexpand '(inc 5))",
25
1
        "(macroexpand '(+ 1 2))",
26
1
        "(defmacro inner (x) `(+ ,x 1)) \
27
1
         (defmacro outer (x) `(inner ,x)) \
28
1
         (macroexpand '(outer 5))",
29
1
    ] {
30
4
        compile_and_validate(src);
31
    }
32
1
}
33

            
34
#[test]
35
1
fn macroexpand_of_a_numeric_expansion_is_the_list() {
36
1
    compile_and_validate("(defmacro two () '(1 2)) (macroexpand '(two))");
37
1
}
38

            
39
#[test]
40
1
fn defmacro_missing_body_errors() {
41
1
    let err = compile_expect_error("(defmacro broken)");
42
1
    assert!(err.contains("DEFMACRO"), "got: {err}");
43
1
}
44

            
45
#[test]
46
1
fn macro_used_inside_expression() {
47
1
    compile_and_validate("(defmacro double (x) `(* ,x 2)) (+ (double 3) 1)");
48
1
}
49

            
50
#[test]
51
1
fn self_referential_macro_errors_instead_of_overflowing() {
52
    // A macro expanding to itself would recurse until the native stack
53
    // overflows; the depth guard turns it into a structured compile error.
54
    // The SHARED compile-time recursion budget (24, shared with inlining
55
    // and AND/OR/COND runtime-guard nesting) is tighter than macro
56
    // expansion's own ceiling (64), so it fires first for a pure macro-only
57
    // recursion — either structured error is an acceptable outcome.
58
1
    let err = compile_expect_error("(defmacro loopmac () '(loopmac)) (loopmac)");
59
1
    assert!(
60
1
        err.contains("macro expansion exceeded depth") || err.contains("shared budget"),
61
        "expected a macro-depth error, got: {err}"
62
    );
63
1
}
64

            
65
#[test]
66
1
fn macro_recursing_via_macroexpand_1_errors_instead_of_overflowing() {
67
    // `macroexpand-1` is one-step, but a macro whose body re-expands itself
68
    // via `(macroexpand-1 '(self))` recurses through the expander. The
69
    // depth guard around `macroexpand-1`'s expansion bounds it — the
70
    // SHARED compile-time recursion budget (see the test above) is tighter
71
    // than macro expansion's own ceiling and fires first.
72
1
    let err = compile_expect_error("(defmacro loopmac () (macroexpand-1 '(loopmac))) (loopmac)");
73
1
    assert!(
74
1
        err.contains("macro expansion exceeded depth") || err.contains("shared budget"),
75
        "expected a macro-depth error, got: {err}"
76
    );
77
1
}
78

            
79
#[test]
80
1
fn defmacro_with_docstring_and_body() {
81
    // 3rd arg is a String → doc; body starts at arg 4.
82
1
    compile_and_validate("(defmacro inc (x) \"increment\" `(+ ,x 1))");
83
1
}
84

            
85
#[test]
86
1
fn defmacro_with_docstring_multi_body_wraps_progn() {
87
1
    compile_and_validate("(defmacro mname (x) \"doc string\" `(+ ,x 1) `(* ,x 2)) (mname 7)");
88
1
}
89

            
90
#[test]
91
1
fn defmacro_non_symbol_name_errors() {
92
1
    let err = compile_expect_error("(defmacro 42 (x) x)");
93
1
    assert!(err.contains("DEFMACRO"), "got: {err}");
94
1
}
95

            
96
#[test]
97
1
fn defmacro_with_aux_errors() {
98
1
    let err = compile_expect_error("(defmacro nope (x &aux y) x)");
99
1
    assert!(
100
1
        err.contains("&aux") || err.contains("DEFMACRO"),
101
        "got: {err}"
102
    );
103
1
}
104

            
105
#[test]
106
1
fn macroexpand_1_wrong_arity_errors() {
107
1
    let err = compile_expect_error("(macroexpand-1)");
108
1
    assert!(err.contains("MACROEXPAND-1"), "got: {err}");
109
1
}
110

            
111
#[test]
112
1
fn macroexpand_wrong_arity_errors() {
113
1
    let err = compile_expect_error("(macroexpand)");
114
1
    assert!(err.contains("MACROEXPAND"), "got: {err}");
115
1
}
116

            
117
#[test]
118
1
fn macroexpand_1_on_atom_returns_atom() {
119
    // Atoms are not macros; expand_1 returns them unchanged through
120
    // the Expr::Quote -> non-list fall-through arm.
121
1
    compile_and_validate("(macroexpand-1 'foo)");
122
1
}
123

            
124
#[test]
125
1
fn namespaced_macro_defines_and_expands() {
126
    // ADR-0029: a macro defined under a namespace key (`util:dbl`) expands
127
    // when invoked qualified. Macros are global-keyed like any symbol; the
128
    // body's `*` is a global builtin (unqualified), matching the unhygienic
129
    // rule that a namespaced macro's body resolves in the expansion env.
130
1
    compile_and_validate("(defmacro util:dbl (x) `(* ,x 2)) (util:dbl 21)");
131
1
}
132

            
133
#[test]
134
1
fn namespaced_macro_unqualified_call_does_not_expand() {
135
    // Strict resolution: `dbl` is NOT `util:dbl`. Calling the macro
136
    // unqualified finds no such macro/fn, so it is an undefined call.
137
1
    let err = compile_expect_error("(defmacro util:dbl (x) `(* ,x 2)) (dbl 21)");
138
1
    let lower = err.to_lowercase();
139
1
    assert!(
140
1
        lower.contains("undefined") && lower.contains("dbl"),
141
        "expected 'Undefined symbol: dbl', got: {err}"
142
    );
143
1
}
144

            
145
#[test]
146
1
fn a_macro_built_quote_form_lowers_in_value_position() {
147
    // ISS-125: `(list 'quote …)` expands to a `(quote …)` list form.
148
1
    compile_and_validate("(defmacro m () (list 'quote (list 1 2))) (length (m))");
149
1
}
150

            
151
#[test]
152
1
fn a_quote_form_beside_a_runtime_value_lowers_in_value_position() {
153
1
    compile_and_validate("(defvar v 1) (setf v 2) (list v (quote (1 2)))");
154
1
}