Last active
August 29, 2015 14:18
-
-
Save rprichard/b4b781bde66a9a407f9e to your computer and use it in GitHub Desktop.
#[allow_internal_unstable] plugin #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![crate_type="dylib"] | |
| #![feature(plugin_registrar, rustc_private, slice_patterns)] | |
| extern crate syntax; | |
| extern crate rustc; | |
| use syntax::codemap::Span; | |
| use syntax::ast; | |
| use syntax::ext::base::*; | |
| use syntax::ext::build::AstBuilder; | |
| use syntax::parse::token; | |
| use rustc::plugin::Registry; | |
| fn expand_foo(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) | |
| -> Box<MacResult + 'static> { | |
| let mut p = cx.new_parser_from_tts(tts); | |
| let arg1 = p.parse_expr(); | |
| let unsafe_expr = cx.expr_path(cx.path_global(sp, vec![ | |
| cx.ident_of("std"), | |
| cx.ident_of("rt"), | |
| cx.ident_of("DEFAULT_ERROR_CODE"), | |
| ])); | |
| let expr = cx.expr_tuple(sp, vec![arg1, unsafe_expr]); | |
| MacEager::expr(expr) | |
| } | |
| #[plugin_registrar] | |
| pub fn plugin_registrar(reg: &mut Registry) { | |
| let expander: MacroExpanderFn = expand_foo; | |
| reg.syntax_exts.push((token::intern("foo"), | |
| NormalTT(Box::new(expander), None, true))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(allow_internal_unstable, plugin)] | |
| #![plugin(foo_macro)] | |
| macro_rules! nested1 { () => (foo!(1)) } | |
| #[allow_internal_unstable] | |
| macro_rules! nested2 { () => (foo!(1)) } | |
| #[allow_internal_unstable] | |
| macro_rules! ident { ($arg:expr) => ({ let x = $arg; x }) } | |
| fn main() { | |
| // GOOD: no stability error | |
| let x = foo!(1); | |
| println!("{:?}", x); | |
| // GOOD: stability error | |
| foo!(std::rt::DEFAULT_ERROR_CODE); | |
| // ^~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| // BAD: stability errors: | |
| nested1!(); | |
| ident!(nested1!()); | |
| nested2!(); | |
| // BAD: stability error for the println! macro range. | |
| let x = println!("{:?}", foo!(1)) ; | |
| // ^~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| // BAD: stability error for the range of the macros. | |
| let x = ident!(ident!(nested2!())) ; | |
| // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment