about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorGeoffry Song <goffrie@gmail.com>2015-03-05 15:06:49 -0500
committerGeoffry Song <goffrie@gmail.com>2015-04-25 21:42:10 -0400
commit2d9831dea598d8a45c69e8c799503e8a397aacc0 (patch)
tree01b440d423b022b089549022f8a5b411514360aa /src/test
parentda623844a9b3f9164723bf7ef2c4744b539af13f (diff)
downloadrust-2d9831dea598d8a45c69e8c799503e8a397aacc0.tar.gz
rust-2d9831dea598d8a45c69e8c799503e8a397aacc0.zip
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.

The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.

A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).

`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).

This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.

As such, this is a [breaking-change].

Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass-fulldeps/qquote.rs79
1 files changed, 38 insertions, 41 deletions
diff --git a/src/test/run-pass-fulldeps/qquote.rs b/src/test/run-pass-fulldeps/qquote.rs
index 995dd80df65..ceface38484 100644
--- a/src/test/run-pass-fulldeps/qquote.rs
+++ b/src/test/run-pass-fulldeps/qquote.rs
@@ -9,59 +9,56 @@
 // except according to those terms.
 
 // ignore-cross-compile
-// ignore-pretty
 
 #![feature(quote, rustc_private)]
 
 extern crate syntax;
 
-use syntax::ast;
-use syntax::codemap;
-use syntax::parse;
-use syntax::print::pprust;
+use syntax::codemap::DUMMY_SP;
+use syntax::print::pprust::*;
 
-trait FakeExtCtxt {
-    fn call_site(&self) -> codemap::Span;
-    fn cfg(&self) -> ast::CrateConfig;
-    fn ident_of(&self, st: &str) -> ast::Ident;
-    fn name_of(&self, st: &str) -> ast::Name;
-    fn parse_sess(&self) -> &parse::ParseSess;
-}
-
-impl FakeExtCtxt for parse::ParseSess {
-    fn call_site(&self) -> codemap::Span {
-        codemap::Span {
-            lo: codemap::BytePos(0),
-            hi: codemap::BytePos(0),
-            expn_id: codemap::NO_EXPANSION,
+fn main() {
+    let ps = syntax::parse::new_parse_sess();
+    let mut cx = syntax::ext::base::ExtCtxt::new(
+        &ps, vec![],
+        syntax::ext::expand::ExpansionConfig::default("qquote".to_string()));
+    cx.bt_push(syntax::codemap::ExpnInfo {
+        call_site: DUMMY_SP,
+        callee: syntax::codemap::NameAndSpan {
+            name: "".to_string(),
+            format: syntax::codemap::MacroBang,
+            allow_internal_unstable: false,
+            span: None,
         }
+    });
+    let cx = &mut cx;
+
+    macro_rules! check {
+        ($f: ident, $($e: expr),+; $expect: expr) => ({
+            $(assert_eq!($f(&$e), $expect);)+
+        });
     }
-    fn cfg(&self) -> ast::CrateConfig { Vec::new() }
-    fn ident_of(&self, st: &str) -> ast::Ident {
-        parse::token::str_to_ident(st)
-    }
-    fn name_of(&self, st: &str) -> ast::Name {
-        parse::token::intern(st)
-    }
-    fn parse_sess(&self) -> &parse::ParseSess { self }
-}
 
-fn main() {
-    let cx = parse::new_parse_sess();
+    let abc = quote_expr!(cx, 23);
+    check!(expr_to_string, abc, *quote_expr!(cx, $abc); "23");
+
+    let ty = quote_ty!(cx, isize);
+    check!(ty_to_string, ty, *quote_ty!(cx, $ty); "isize");
 
-    assert_eq!(pprust::expr_to_string(&*quote_expr!(&cx, 23)), "23");
-    assert_eq!(pprust::pat_to_string(&*quote_pat!(&cx, Some(_))), "Some(_)");
-    assert_eq!(pprust::ty_to_string(&*quote_ty!(&cx, isize)), "isize");
+    let item = quote_item!(cx, static x: $ty = 10;).unwrap();
+    check!(item_to_string, item, quote_item!(cx, $item).unwrap(); "static x: isize = 10;");
 
-    let arm = quote_arm!(&cx, (ref x, ref y) => (x, y),);
-    assert_eq!(pprust::arm_to_string(&arm), " (ref x, ref y) => (x, y),");
+    let twenty: u16 = 20;
+    let stmt = quote_stmt!(cx, let x = $twenty;).unwrap();
+    check!(stmt_to_string, stmt, *quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;");
 
-    let attr = quote_attr!(&cx, #![cfg(foo = "bar")]);
-    assert_eq!(pprust::attr_to_string(&attr), "#![cfg(foo = \"bar\")]");
+    let pat = quote_pat!(cx, Some(_));
+    check!(pat_to_string, pat, *quote_pat!(cx, $pat); "Some(_)");
 
-    let item = quote_item!(&cx, static x : isize = 10;).unwrap();
-    assert_eq!(pprust::item_to_string(&*item), "static x: isize = 10;");
+    let expr = quote_expr!(cx, (x, y));
+    let arm = quote_arm!(cx, (ref x, ref y) => $expr,);
+    check!(arm_to_string, arm, quote_arm!(cx, $arm); " (ref x, ref y) => (x, y),");
 
-    let stmt = quote_stmt!(&cx, let x = 20;).unwrap();
-    assert_eq!(pprust::stmt_to_string(&*stmt), "let x = 20;");
+    let attr = quote_attr!(cx, #![cfg(foo = "bar")]);
+    check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#);
 }