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.
This commit is contained in:
commit
5a872880bd
8 changed files with 137 additions and 11 deletions
|
|
@ -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!'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")]"#);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue