From a53b8933509eabbbf3faab6822b9a2fb0bbfc786 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 1 May 2015 11:11:11 -0700 Subject: [PATCH] Re-enable unquoting generics and where clauses --- Cargo.toml | 8 ++++---- src/lib.rs | 24 ++++++++++++++++++++++++ tests/test.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b24d2e96..ced88a72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "quasi" -version = "0.1.10" +version = "0.1.11" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "A quasi-quoting macro system" repository = "https://github.com/erickt/rust-quasi" -[dev-dependencies.quasi_macros] -version = "*" -path = "quasi_macros" +[dev-dependencies] +aster = "*" +quasi_macros = { version = "*", path = "quasi_macros" } diff --git a/src/lib.rs b/src/lib.rs index 7fb61919..b77a05e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,30 @@ impl ToTokens for P { } } +impl ToTokens for ast::Generics { + fn to_tokens(&self, cx: &ExtCtxt) -> Vec { + use syntax::print::pprust; + use syntax::parse::parse_tts_from_source_str; + + let s = pprust::generics_to_string(self); + + parse_tts_from_source_str(" Vec { + use syntax::print::pprust; + use syntax::parse::parse_tts_from_source_str; + + let s = pprust::to_string(|s| { + s.print_where_clause(&self) + }); + + parse_tts_from_source_str(" { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { vec![ast::TtToken(self.span, token::Interpolated(token::NtStmt(self.clone())))] diff --git a/tests/test.rs b/tests/test.rs index 078ed6cc..93c4c911 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -11,13 +11,16 @@ #![feature(plugin, rustc_private)] #![plugin(quasi_macros)] +extern crate aster; extern crate syntax; extern crate quasi; +use syntax::ast; use syntax::ext::base::ExtCtxt; use syntax::ext::expand; use syntax::parse; use syntax::print::pprust; +use syntax::owned_slice::OwnedSlice; fn make_ext_ctxt(sess: &parse::ParseSess) -> ExtCtxt { let info = syntax::codemap::ExpnInfo { @@ -190,3 +193,51 @@ fn test_quote_with_macro() { assert_eq!(pprust::block_to_string(&block), "{ value!() }"); } + +#[test] +fn test_quote_with_generics_and_where_clause() { + let sess = parse::new_parse_sess(); + let cx = make_ext_ctxt(&sess); + + let builder = aster::AstBuilder::new(); + let generics = builder.generics() + .ty_param("T") + .trait_bound("Clone").build() + .build() + .build(); + + let where_clause = ast::WhereClause { + id: ast::DUMMY_NODE_ID, + predicates: vec![ + ast::WherePredicate::BoundPredicate( + ast::WhereBoundPredicate { + span: syntax::codemap::DUMMY_SP, + bound_lifetimes: vec![], + bounded_ty: quote_ty!(&cx, T), + bounds: OwnedSlice::from_vec(vec![ + ast::TraitTyParamBound( + ast::PolyTraitRef { + bound_lifetimes: vec![], + trait_ref: ast::TraitRef { + path: builder.path().id("Clone").build(), + ref_id: ast::DUMMY_NODE_ID, + }, + span: syntax::codemap::DUMMY_SP, + }, + ast::TraitBoundModifier::None, + ), + ]), + } + ), + ], + }; + + let item = quote_item!(&cx, + impl $generics Clone for Foo $where_clause {} + ).unwrap(); + + assert_eq!( + pprust::item_to_string(&item), + "impl Clone for Foo where T: Clone { }" + ); +}