2 releases

new 0.1.1 May 7, 2025
0.1.0 May 7, 2025

#421 in No standard library

MIT license

9KB
142 lines

The set_span in the simulation process macro

This macro can be used to improve error messages, etc

Note

Incorrect use may cause some damage to hygiene

Example

macro_rules! foo {
    ($t:tt) => {
        foo! { ($t) ($t) }
    };
    ($t:tt (0)) => {
        set_span::set_span! {$t[0], {
            #set_span {
                compile_error! {"input by zero"}
            }
        }}
    };
    ($_:tt ($lit:literal)) => { /*...*/ };
}
foo!(0);

Output:

error: input by zero
  --> src/lib.rs:25:6
   |
18 | foo!(0);
   |      ^

If set_span is not used:

macro_rules! foo {
    ($t:tt) => {
        foo! { ($t) ($t) }
    };
    ($t:tt (0)) => {
        compile_error! {"input by zero"}
    };
    ($_:tt ($lit:literal)) => { /*...*/ };
}
foo!(0);

Output:

error: input by zero
  --> src/lib.rs:45:9
   |
8  |         compile_error! {"input by zero"}
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
12 | foo!(0);
   | ------- in this macro invocation
   |

Dependencies

~23KB