Skip to content

Instantly share code, notes, and snippets.

@Osspial
Created April 22, 2017 20:30
Show Gist options
  • Save Osspial/d0eb4827d764aa40912bface538dd0ea to your computer and use it in GitHub Desktop.
Save Osspial/d0eb4827d764aa40912bface538dd0ea to your computer and use it in GitHub Desktop.
Error messages when reparsing macro tokens
// This works
macro_rules! macro_into_ident {
(id) => ();
}
macro_rules! ident_into_macro {
($ident:ident) => (macro_into_ident!($ident););
}
ident_into_macro!(id);
// This also works
macro_rules! macro_into_tt {
(&) => ();
}
macro_rules! tt_into_macro {
($tt:tt) => (macro_into_tt!($tt););
}
tt_into_macro!(&);
// Fails with this error:
// error: no rules expected the token `T::SpecialA`
// --> src/main.rs:23:39
// |
// 32 | ($path:path) => (macro_into_path!($path););
// | ^^^^^
// 33 | }
// 34 | path_into_macro!(T::SpecialA);
// | ------------------------------ in this macro invocation
macro_rules! macro_into_path {
(T::SpecialA) => ();
}
macro_rules! path_into_macro {
($path:path) => (macro_into_path!($path););
}
path_into_macro!(T::SpecialA);
// Fails with this error:
// error: no rules expected the token `2 + 2`
// --> .\macro_reparse.rs:40:39
// |
// 49 | ($expr:expr) => (macro_into_expr!($expr););
// | ^^^^^
// 50 | }
// 51 | expr_into_macro!(2 + 2);
// | ------------------------ in this macro invocation
macro_rules! macro_into_expr {
(2 + 2) => ();
}
macro_rules! expr_into_macro {
($expr:expr) => (macro_into_expr!($expr););
}
expr_into_macro!(2 + 2);
// Fails with this error:
// error: no rules expected the token `i32`
// --> .\macro_reparse.rs:66:33
// |
// 66 | ($ty:ty) => (macro_into_ty!($ty););
// | ^^^
// 67 | }
// 68 | ty_into_macro!(i32);
// | -------------------- in this macro invocation
macro_rules! macro_into_ty {
(i32) => ();
}
macro_rules! ty_into_macro {
($ty:ty) => (macro_into_ty!($ty););
}
ty_into_macro!(i32);
// Fails with this error:
// error: no rules expected the token `_`
// --> .\macro_reparse.rs:83:36
// |
// 83 | ($pat:pat) => (macro_into_pat!($pat););
// | ^^^^
// 84 | }
// 85 | pat_into_macro!(_);
// | ------------------- in this macro invocation
macro_rules! macro_into_pat {
(_) => ();
}
macro_rules! pat_into_macro {
($pat:pat) => (macro_into_pat!($pat););
}
pat_into_macro!(_);
// Fails with this error:
// error: no rules expected the token `let x = 3;`
// --> .\macro_reparse.rs:100:39
// |
// 100 | ($stmt:stmt) => (macro_into_stmt!($stmt););
// | ^^^^^
// 101 | }
// 102 | stmt_into_macro!(let x = 3);
// | ---------------------------- in this macro invocation
macro_rules! macro_into_stmt {
(let x = 3) => ();
}
macro_rules! stmt_into_macro {
($stmt:stmt) => (macro_into_stmt!($stmt););
}
stmt_into_macro!(let x = 3);
// Fails with this error:
// error: no rules expected the token `{ log(error, "hi"); return 12; }`
// --> .\macro_reparse.rs:117:42
// |
// 117 | ($block:block) => (macro_into_block!($block););
// | ^^^^^^
// 118 | }
// 119 | block_into_macro!({ log(error, "hi"); return 12; });
// | ---------------------------------------------------- in this macro invocation
macro_rules! macro_into_block {
({ log(error, "hi"); return 12; }) => ();
}
macro_rules! block_into_macro {
($block:block) => (macro_into_block!($block););
}
block_into_macro!({ log(error, "hi"); return 12; });
// Fails with this error:
// error: no rules expected the token `fn foo() { }`
// --> .\macro_reparse.rs:134:39
// |
// 134 | ($item:item) => (macro_into_item!($item););
// | ^^^^^
// 135 | }
// 136 | item_into_macro!(fn foo() { });
// | ------------------------------- in this macro invocation
macro_rules! macro_into_item {
(fn foo() { }) => ();
}
macro_rules! item_into_macro {
($item:item) => (macro_into_item!($item););
}
item_into_macro!(fn foo() { });
// Fails with this error:
// error: no rules expected the token `cfg(target_os = "windows")`
// --> .\macro_reparse.rs:151:39
// |
// 151 | ($meta:meta) => (macro_into_meta!($meta););
// | ^^^^^
// 152 | }
// 153 | meta_into_macro!(cfg(target_os = "windows"));
// | --------------------------------------------- in this macro invocation
macro_rules! macro_into_meta {
(cfg(target_os = "windows")) => ();
}
macro_rules! meta_into_macro {
($meta:meta) => (macro_into_meta!($meta););
}
meta_into_macro!(cfg(target_os = "windows"));
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment