Skip to content

Commit 5c424ba

Browse files
committed
syntax: store char literals/tokens as chars 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.
1 parent 239557d commit 5c424ba

File tree

8 files changed

+13
-19
lines changed

8 files changed

+13
-19
lines changed

src/librustdoc/clean.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc::metadata::decoder;
2626

2727
use std::local_data;
2828
use std::strbuf::StrBuf;
29-
use std;
3029

3130
use core;
3231
use doctree;
@@ -1246,7 +1245,7 @@ fn lit_to_str(lit: &ast::Lit) -> ~str {
12461245
match lit.node {
12471246
ast::LitStr(ref st, _) => st.get().to_owned(),
12481247
ast::LitBinary(ref data) => format!("{:?}", data.as_slice()),
1249-
ast::LitChar(c) => "'".to_owned() + std::char::from_u32(c).unwrap().to_str() + "'",
1248+
ast::LitChar(c) => format!("'{}'", c),
12501249
ast::LitInt(i, _t) => i.to_str(),
12511250
ast::LitUint(u, _t) => u.to_str(),
12521251
ast::LitIntUnsuffixed(i) => i.to_str(),

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ pub type Lit = Spanned<Lit_>;
653653
pub enum Lit_ {
654654
LitStr(InternedString, StrStyle),
655655
LitBinary(Rc<Vec<u8> >),
656-
LitChar(u32),
656+
LitChar(char),
657657
LitInt(i64, IntTy),
658658
LitUint(u64, UintTy),
659659
LitIntUnsuffixed(i64),

src/libsyntax/ext/bytes.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use ext::base::*;
1616
use ext::base;
1717
use ext::build::AstBuilder;
1818

19-
use std::char;
20-
2119
pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> ~base::MacResult {
2220
// Gather all argument expressions
2321
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]) ->
5957

6058
// char literal, push to vector expression
6159
ast::LitChar(v) => {
62-
if char::from_u32(v).unwrap().is_ascii() {
60+
if v.is_ascii() {
6361
bytes.push(cx.expr_u8(expr.span, v as u8));
6462
} else {
6563
cx.span_err(expr.span, "non-ascii char literal in bytes!")

src/libsyntax/ext/concat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use ext::base;
1414
use ext::build::AstBuilder;
1515
use parse::token;
1616

17-
use std::char;
1817
use std::strbuf::StrBuf;
1918

2019
pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
@@ -35,7 +34,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
3534
accumulator.push_str(s.get());
3635
}
3736
ast::LitChar(c) => {
38-
accumulator.push_char(char::from_u32(c).unwrap());
37+
accumulator.push_char(c);
3938
}
4039
ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) => {
4140
accumulator.push_str(format!("{}", i));

src/libsyntax/ext/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'a, 'b> Context<'a, 'b> {
561561

562562
// Translate the format
563563
let fill = match arg.format.fill { Some(c) => c, None => ' ' };
564-
let fill = self.ecx.expr_lit(sp, ast::LitChar(fill as u32));
564+
let fill = self.ecx.expr_lit(sp, ast::LitChar(fill));
565565
let align = match arg.format.align {
566566
parse::AlignLeft => {
567567
self.ecx.path_global(sp, self.parsepath("AlignLeft"))

src/libsyntax/parse/lexer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ fn next_token_inner(rdr: &mut StringReader) -> token::Token {
874874
"unterminated character constant".to_owned());
875875
}
876876
bump(rdr); // advance curr past token
877-
return token::LIT_CHAR(c2 as u32);
877+
return token::LIT_CHAR(c2);
878878
}
879879
'"' => {
880880
let mut accum_str = StrBuf::new();
@@ -1097,17 +1097,17 @@ mod test {
10971097

10981098
#[test] fn character_a() {
10991099
assert_eq!(setup(&mk_sh(), "'a'".to_owned()).next_token().tok,
1100-
token::LIT_CHAR('a' as u32));
1100+
token::LIT_CHAR('a'));
11011101
}
11021102

11031103
#[test] fn character_space() {
11041104
assert_eq!(setup(&mk_sh(), "' '".to_owned()).next_token().tok,
1105-
token::LIT_CHAR(' ' as u32));
1105+
token::LIT_CHAR(' '));
11061106
}
11071107

11081108
#[test] fn character_escaped() {
11091109
assert_eq!(setup(&mk_sh(), "'\\n'".to_owned()).next_token().tok,
1110-
token::LIT_CHAR('\n' as u32));
1110+
token::LIT_CHAR('\n'));
11111111
}
11121112

11131113
#[test] fn lifetime_name() {
@@ -1128,7 +1128,7 @@ mod test {
11281128

11291129
#[test] fn nested_block_comments() {
11301130
assert_eq!(setup(&mk_sh(), "/* /* */ */'a'".to_owned()).next_token().tok,
1131-
token::LIT_CHAR('a' as u32));
1131+
token::LIT_CHAR('a'));
11321132
}
11331133

11341134
}

src/libsyntax/parse/token.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use util::interner;
1818

1919
use serialize::{Decodable, Decoder, Encodable, Encoder};
2020
use std::cast;
21-
use std::char;
2221
use std::fmt;
2322
use std::local_data;
2423
use std::path::BytesContainer;
@@ -81,7 +80,7 @@ pub enum Token {
8180
DOLLAR,
8281

8382
/* Literals */
84-
LIT_CHAR(u32),
83+
LIT_CHAR(char),
8584
LIT_INT(i64, ast::IntTy),
8685
LIT_UINT(u64, ast::UintTy),
8786
LIT_INT_UNSUFFIXED(i64),
@@ -195,7 +194,7 @@ pub fn to_str(t: &Token) -> ~str {
195194
/* Literals */
196195
LIT_CHAR(c) => {
197196
let mut res = StrBuf::from_str("'");
198-
char::from_u32(c).unwrap().escape_default(|c| {
197+
c.escape_default(|c| {
199198
res.push_char(c);
200199
});
201200
res.push_char('\'');

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use print::pp::{Breaks, Consistent, Inconsistent, eof};
2626
use print::pp;
2727

2828
use std::cast;
29-
use std::char;
3029
use std::io::{IoResult, MemWriter};
3130
use std::io;
3231
use std::rc::Rc;
@@ -2196,7 +2195,7 @@ impl<'a> State<'a> {
21962195
ast::LitStr(ref st, style) => self.print_string(st.get(), style),
21972196
ast::LitChar(ch) => {
21982197
let mut res = StrBuf::from_str("'");
2199-
char::from_u32(ch).unwrap().escape_default(|c| res.push_char(c));
2198+
ch.escape_default(|c| res.push_char(c));
22002199
res.push_char('\'');
22012200
word(&mut self.s, res.into_owned())
22022201
}

0 commit comments

Comments
 (0)