1 unstable release

0.1.9 Dec 1, 2019

#1957 in Development tools

MIT license

51KB
1K SLoC

macro_tt_utils

Procedural Macro Utilities (tt_ident, throw_diagnostics, deref_span).

Build Status Mit/Apache licensed Documentation

// Experimental API

tt_ident

Manipulation of input names in macros, currently only name comparison is possible.

#![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

Macros to throw compiler errors or warnings (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

Unpacks the current 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
*/

License

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

Licensed under the MIT License

Licensed under the Apache License, Version 2.0

Dependencies

~9KB