From 5c424ba34ad8b45cfba4619832d23b0278ede696 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 1 May 2014 23:35:06 +1000 Subject: syntax: store char literals/tokens as `char`s rather than u32s. Clearly storing them as `char` is semantically nicer, but this also fixes a bug whereby `quote_expr!(cx, 'a')` wasn't working, because the code created by quotation was not matching the actual AST definitions. --- src/libsyntax/ext/bytes.rs | 4 +--- src/libsyntax/ext/concat.rs | 3 +-- src/libsyntax/ext/format.rs | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index f4680b27084..c6349d616ec 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -16,8 +16,6 @@ use ext::base::*; use ext::base; use ext::build::AstBuilder; -use std::char; - pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> ~base::MacResult { // Gather all argument expressions let exprs = match get_exprs_from_tts(cx, sp, tts) { @@ -59,7 +57,7 @@ pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> // char literal, push to vector expression ast::LitChar(v) => { - if char::from_u32(v).unwrap().is_ascii() { + if v.is_ascii() { bytes.push(cx.expr_u8(expr.span, v as u8)); } else { cx.span_err(expr.span, "non-ascii char literal in bytes!") diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index 123271c5b5e..fe7fa636e7d 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -14,7 +14,6 @@ use ext::base; use ext::build::AstBuilder; use parse::token; -use std::char; use std::strbuf::StrBuf; pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, @@ -35,7 +34,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, accumulator.push_str(s.get()); } ast::LitChar(c) => { - accumulator.push_char(char::from_u32(c).unwrap()); + accumulator.push_char(c); } ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) => { accumulator.push_str(format!("{}", i)); diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 84021f6362b..df79b105444 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -561,7 +561,7 @@ impl<'a, 'b> Context<'a, 'b> { // Translate the format let fill = match arg.format.fill { Some(c) => c, None => ' ' }; - let fill = self.ecx.expr_lit(sp, ast::LitChar(fill as u32)); + let fill = self.ecx.expr_lit(sp, ast::LitChar(fill)); let align = match arg.format.align { parse::AlignLeft => { self.ecx.path_global(sp, self.parsepath("AlignLeft")) -- cgit 1.4.1-3-g733a5 From 1d43a98deadf9d1866d99e253db14651c36726e3 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 1 May 2014 23:39:00 +1000 Subject: syntax: implement ToSource for more things in the quasiquoter. The last few primitive types were missing. --- src/libsyntax/ext/quote.rs | 23 +++++++++++++++++++++++ src/test/run-pass-fulldeps/quote-tokens.rs | 4 ++++ 2 files changed, 27 insertions(+) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 68b0ef40b16..fc7f7722354 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -125,6 +125,26 @@ pub mod rt { } } + impl ToSource for () { + fn to_source(&self) -> ~str { + "()".to_owned() + } + } + + impl ToSource for bool { + fn to_source(&self) -> ~str { + let lit = dummy_spanned(ast::LitBool(*self)); + pprust::lit_to_str(&lit) + } + } + + impl ToSource for char { + fn to_source(&self) -> ~str { + let lit = dummy_spanned(ast::LitChar(*self)); + pprust::lit_to_str(&lit) + } + } + impl ToSource for int { fn to_source(&self) -> ~str { let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI)); @@ -227,6 +247,9 @@ pub mod rt { impl_to_tokens!(@ast::Expr) impl_to_tokens!(ast::Block) impl_to_tokens_self!(&'a str) + impl_to_tokens!(()) + impl_to_tokens!(char) + impl_to_tokens!(bool) impl_to_tokens!(int) impl_to_tokens!(i8) impl_to_tokens!(i16) diff --git a/src/test/run-pass-fulldeps/quote-tokens.rs b/src/test/run-pass-fulldeps/quote-tokens.rs index 4243fcb05d5..7c25246807d 100644 --- a/src/test/run-pass-fulldeps/quote-tokens.rs +++ b/src/test/run-pass-fulldeps/quote-tokens.rs @@ -26,6 +26,10 @@ fn syntax_extension(cx: &ExtCtxt) { let _c: @syntax::ast::Pat = quote_pat!(cx, (x, 1 .. 4, *) ); let _d: @syntax::ast::Stmt = quote_stmt!(cx, let x = $a; ); let _e: @syntax::ast::Expr = quote_expr!(cx, match foo { $p_toks => 10 } ); + + let _f: @syntax::ast::Expr = quote_expr!(cx, ()); + let _g: @syntax::ast::Expr = quote_expr!(cx, true); + let _h: @syntax::ast::Expr = quote_expr!(cx, 'a'); } fn main() { -- cgit 1.4.1-3-g733a5