1 个不稳定版本

0.1.9 2019 年 12 月 1 日

#1976开发工具

MIT 许可协议

51KB
1K SLoC

macro_tt_utils

过程宏工具集 (tt_ident, throw_diagnostics, deref_span).

Build Status Mit/Apache licensed Documentation

// 实验性 API

tt_ident

操作宏中的输入名称,目前仅支持名称比较。

#![feature(proc_macro_hygiene)]

use macro_tt_utils::tt_ident;
use macro_tt_utils::throw_compile_warning;
use macro_tt_utils::throw_compile_error;


#[macro_export]
macro_rules! macros_generate {
	( fn name_checker($i:ident) -> | $i2:ident | $b:block ) => {
		throw_compile_warning!(@root "We are testing warning.");
		tt_ident! {
			if ($i == $i2 || $i == "__hidden_a" || $i2 == "__hidden_a") {
				throw_compile_error!(@root "Perhaps undefined behavior");
			}
		}
	}
}

fn main() {
	macros_generate! {
		fn name_checker(a) -> |a| {
			ff
		}
	}
}


/* 
warning: We are testing warning.
--> src/main.rs:21:2
|
21 |       macros_generate! {
|  _____^
22 | |         fn name_checker(a) -> |a| {
23 | |             ff
24 | |         }
25 | |     }
| |_____^

error: Perhaps undefined behavior
--> src/main.rs:21:2
|
21 |       macros_generate! {
|  _____^
22 | |         fn name_checker(a) -> |a| {
23 | |             ff
24 | |         }
25 | |     }
| |_____^
*/

throw_diagnostics

抛出编译器错误或警告的宏 (throw_compile_error, throw_compile_warning, throw_compile_note, throw_compile_help).

#![feature(proc_macro_hygiene)]

use macro_tt_utils::throw_compile_warning;

#[macro_export]
macro_rules! macros_generate {
	{ fn name_checker($i:ident) -> | $i2:ident | $b:block } => {
		throw_compile_warning!("#1 We are testing warning.");
		throw_compile_warning!(@root "#2 We are testing warning.");
	}
}

fn main() {
	macros_generate! {
		fn name_checker(a) -> |a| {}
	}
}

/*
warning: #1 We are testing warning.
  --> src/main.rs:8:3
   |
8  |           throw_compile_warning!("#1 We are testing warning.");
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
14 |       macros_generate! {
   |  _____-
15 | |         fn name_checker(a) -> |a| {}
16 | |     }
   | |_____- in this macro invocation

warning: #2 We are testing warning.
  --> src/main.rs:14:2
   |
14 |       macros_generate! {
   |  _____^
15 | |         fn name_checker(a) -> |a| {}
16 | |     }
   | |_____^

*/

deref_span

解包当前的 span。

#![feature(proc_macro_hygiene)]

use macro_tt_utils::deref_span;

#[macro_export]
macro_rules! macros_generate {
	{ fn name_checker($i:ident) -> | $i2:ident | $b:block } => {
		compile_error!("#1 Test");
		
		deref_span! {
			compile_error!("#2 Test");
		}
	}
}

fn main() {
	macros_generate! {
		fn name_checker(a) -> |a| {}
	}
}

/*
error: #1 Test
  --> src/main.rs:8:3
   |
8  |           compile_error!("#1 Test");
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^
...
17 |       macros_generate! {
   |  _____-
18 | |         fn name_checker(a) -> |a| {}
19 | |     }
   | |_____- in this macro invocation

error: #2 Test
  --> src/main.rs:17:2
   |
17 |       macros_generate! {
   |  _____^
18 | |         fn name_checker(a) -> |a| {}
19 | |     }
   | |_____^

error: aborting due to 2 previous errors
*/

许可协议

版权 2019 #UlinProject (Denis Kotlyarov) Денис Котляров

MIT 许可协议下授权

Apache License, Version 2.0 许可协议下授权

依赖

~9KB