diff options
| author | bors <bors@rust-lang.org> | 2015-11-12 20:31:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-11-12 20:31:12 +0000 |
| commit | 5a872880bd42f5fc56a3ae35aa05a4e239f5bd35 (patch) | |
| tree | 9ba70793feeae9a817ffdad1b49a1b1ff260e0a5 /src/test | |
| parent | 15e7824f1ce2f622afc195e72469f3c01f88a1f3 (diff) | |
| parent | 8c88308c68691b795815a776d41b3aa11717c146 (diff) | |
| download | rust-5a872880bd42f5fc56a3ae35aa05a4e239f5bd35.tar.gz rust-5a872880bd42f5fc56a3ae35aa05a4e239f5bd35.zip | |
Auto merge of #29780 - KyleMayes:quote-ext, r=nrc
This is my first code contribution to Rust, so I'm sure there are some issues with the changes I've made. I've added the `quote_arg!`, `quote_block!`, `quote_path!`, and `quote_meta_item!` quasiquoting macros. From my experience trying to build AST in compiler plugins, I would like to be able to build any AST piece with a quasiquoting macro (e.g., `quote_struct_field!` or `quote_variant!`) and then use those AST pieces in other quasiquoting macros, but this pull request just adds some of the low-hanging fruit. I'm not sure if these additions are desirable, and I'm sure these macros can be implemented in an external crate if not.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail-fulldeps/gated-quote.rs | 24 | ||||
| -rw-r--r-- | src/test/run-pass-fulldeps/qquote.rs | 37 |
2 files changed, 51 insertions, 10 deletions
diff --git a/src/test/compile-fail-fulldeps/gated-quote.rs b/src/test/compile-fail-fulldeps/gated-quote.rs index 6a5cd88a591..cd801fbcd88 100644 --- a/src/test/compile-fail-fulldeps/gated-quote.rs +++ b/src/test/compile-fail-fulldeps/gated-quote.rs @@ -37,14 +37,18 @@ impl ParseSess { pub fn main() { let ecx = &ParseSess; - let x = quote_tokens!(ecx, 3); //~ ERROR macro undefined: 'quote_tokens!' - let x = quote_expr!(ecx, 3); //~ ERROR macro undefined: 'quote_expr!' - let x = quote_ty!(ecx, 3); //~ ERROR macro undefined: 'quote_ty!' - let x = quote_method!(ecx, 3); //~ ERROR macro undefined: 'quote_method!' - let x = quote_item!(ecx, 3); //~ ERROR macro undefined: 'quote_item!' - let x = quote_pat!(ecx, 3); //~ ERROR macro undefined: 'quote_pat!' - let x = quote_arm!(ecx, 3); //~ ERROR macro undefined: 'quote_arm!' - let x = quote_stmt!(ecx, 3); //~ ERROR macro undefined: 'quote_stmt!' - let x = quote_matcher!(ecx, 3); //~ ERROR macro undefined: 'quote_matcher!' - let x = quote_attr!(ecx, 3); //~ ERROR macro undefined: 'quote_attr!' + let x = quote_tokens!(ecx, 3); //~ ERROR macro undefined: 'quote_tokens!' + let x = quote_expr!(ecx, 3); //~ ERROR macro undefined: 'quote_expr!' + let x = quote_ty!(ecx, 3); //~ ERROR macro undefined: 'quote_ty!' + let x = quote_method!(ecx, 3); //~ ERROR macro undefined: 'quote_method!' + let x = quote_item!(ecx, 3); //~ ERROR macro undefined: 'quote_item!' + let x = quote_pat!(ecx, 3); //~ ERROR macro undefined: 'quote_pat!' + let x = quote_arm!(ecx, 3); //~ ERROR macro undefined: 'quote_arm!' + let x = quote_stmt!(ecx, 3); //~ ERROR macro undefined: 'quote_stmt!' + let x = quote_matcher!(ecx, 3); //~ ERROR macro undefined: 'quote_matcher!' + let x = quote_attr!(ecx, 3); //~ ERROR macro undefined: 'quote_attr!' + let x = quote_arg!(ecx, 3); //~ ERROR macro undefined: 'quote_arg!' + let x = quote_block!(ecx, 3); //~ ERROR macro undefined: 'quote_block!' + let x = quote_meta_item!(ecx, 3); //~ ERROR macro undefined: 'quote_meta_item!' + let x = quote_path!(ecx, 3); //~ ERROR macro undefined: 'quote_path!' } diff --git a/src/test/run-pass-fulldeps/qquote.rs b/src/test/run-pass-fulldeps/qquote.rs index ba713bb98f8..edaa88452c4 100644 --- a/src/test/run-pass-fulldeps/qquote.rs +++ b/src/test/run-pass-fulldeps/qquote.rs @@ -63,4 +63,41 @@ fn main() { let attr = quote_attr!(cx, #![cfg(foo = "bar")]); check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#); + + // quote_arg! + + let arg = quote_arg!(cx, foo: i32); + check!(arg_to_string, arg, quote_arg!(cx, $arg); "foo: i32"); + + let function = quote_item!(cx, fn f($arg) { }).unwrap(); + check!(item_to_string, function; "fn f(foo: i32) { }"); + + let args = vec![arg, quote_arg!(cx, bar: u32)]; + let args = &args[..]; + let function = quote_item!(cx, fn f($args) { }).unwrap(); + check!(item_to_string, function; "fn f(foo: i32, bar: u32) { }"); + + // quote_block! + + let block = quote_block!(cx, { $stmt let y = 40u32; }); + check!(block_to_string, block, *quote_block!(cx, $block); "{ let x = 20u16; let y = 40u32; }"); + + let function = quote_item!(cx, fn f() $block).unwrap(); + check!(item_to_string, function; "fn f() { let x = 20u16; let y = 40u32; }"); + + // quote_path! + + let path = quote_path!(cx, ::syntax::ptr::P<MetaItem>); + check!(path_to_string, path, quote_path!(cx, $path); "::syntax::ptr::P<MetaItem>"); + + let ty = quote_ty!(cx, $path); + check!(ty_to_string, ty; "::syntax::ptr::P<MetaItem>"); + + // quote_meta_item! + + let meta = quote_meta_item!(cx, cfg(foo = "bar")); + check!(meta_item_to_string, meta, *quote_meta_item!(cx, $meta); r#"cfg(foo = "bar")"#); + + let attr = quote_attr!(cx, #![$meta]); + check!(attribute_to_string, attr; r#"#![cfg(foo = "bar")]"#); } |
