Skip to content

Commit 3dfda16

Browse files
committed
Auto merge of #49993 - nnethercote:shrink-Token, r=alexcrichton
Change the hashcounts in raw `Lit` variants from usize to u16. This reduces the size of `Token` from 32 bytes to 24 bytes on 64-bit platforms.
2 parents 65d201f + 4d34bfd commit 3dfda16

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

src/libproc_macro/quote.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ impl<'a> Quote for &'a str {
163163
}
164164
}
165165

166-
impl Quote for usize {
166+
impl Quote for u16 {
167167
fn quote(self) -> TokenStream {
168-
TokenTree::from(Literal::usize_unsuffixed(self)).into()
168+
TokenTree::from(Literal::u16_unsuffixed(self)).into()
169169
}
170170
}
171171

@@ -197,7 +197,7 @@ macro_rules! literals {
197197
($($i:ident),*; $($raw:ident),*) => {
198198
pub enum LiteralKind {
199199
$($i,)*
200-
$($raw(usize),)*
200+
$($raw(u16),)*
201201
}
202202

203203
impl LiteralKind {

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,8 @@ pub enum StrStyle {
12551255
Cooked,
12561256
/// A raw string, like `r##"foo"##`
12571257
///
1258-
/// The uint is the number of `#` symbols used
1259-
Raw(usize)
1258+
/// The value is the number of `#` symbols used.
1259+
Raw(u16)
12601260
}
12611261

12621262
/// A literal

src/libsyntax/ext/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub trait AstBuilder {
139139
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
140140
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
141141
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
142+
fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
142143
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
143144
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
144145

@@ -708,6 +709,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
708709
self.expr_lit(sp, ast::LitKind::Int(u as u128,
709710
ast::LitIntType::Unsigned(ast::UintTy::U32)))
710711
}
712+
fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
713+
self.expr_lit(sp, ast::LitKind::Int(u as u128,
714+
ast::LitIntType::Unsigned(ast::UintTy::U16)))
715+
}
711716
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
712717
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
713718
}

src/libsyntax/ext/quote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
623623
($name: expr, $suffix: expr, $content: expr $(, $count: expr)*) => {{
624624
let name = mk_name(cx, sp, ast::Ident::with_empty_ctxt($content));
625625
let inner = cx.expr_call(sp, mk_token_path(cx, sp, $name), vec![
626-
name $(, cx.expr_usize(sp, $count))*
626+
name $(, cx.expr_u16(sp, $count))*
627627
]);
628628
let suffix = match $suffix {
629629
Some(name) => cx.expr_some(sp, mk_name(cx, sp, ast::Ident::with_empty_ctxt(name))),

src/libsyntax/parse/lexer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ impl<'a> StringReader<'a> {
133133
Ok(ret_val)
134134
}
135135

136-
fn fail_unterminated_raw_string(&self, pos: BytePos, hash_count: usize) {
136+
fn fail_unterminated_raw_string(&self, pos: BytePos, hash_count: u16) {
137137
let mut err = self.struct_span_fatal(pos, pos, "unterminated raw string");
138138
err.span_label(self.mk_sp(pos, pos), "unterminated raw string");
139139
if hash_count > 0 {
140140
err.note(&format!("this raw string should be terminated with `\"{}`",
141-
"#".repeat(hash_count)));
141+
"#".repeat(hash_count as usize)));
142142
}
143143
err.emit();
144144
FatalError.raise();
@@ -1439,7 +1439,7 @@ impl<'a> StringReader<'a> {
14391439
'r' => {
14401440
let start_bpos = self.pos;
14411441
self.bump();
1442-
let mut hash_count = 0;
1442+
let mut hash_count: u16 = 0;
14431443
while self.ch_is('#') {
14441444
self.bump();
14451445
hash_count += 1;

src/libsyntax/parse/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ pub enum Lit {
7272
Integer(ast::Name),
7373
Float(ast::Name),
7474
Str_(ast::Name),
75-
StrRaw(ast::Name, usize), /* raw str delimited by n hash symbols */
75+
StrRaw(ast::Name, u16), /* raw str delimited by n hash symbols */
7676
ByteStr(ast::Name),
77-
ByteStrRaw(ast::Name, usize), /* raw byte str delimited by n hash symbols */
77+
ByteStrRaw(ast::Name, u16), /* raw byte str delimited by n hash symbols */
7878
}
7979

8080
impl Lit {

src/libsyntax/print/pprust.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ pub fn token_to_string(tok: &Token) -> String {
234234
token::Integer(c) => c.to_string(),
235235
token::Str_(s) => format!("\"{}\"", s),
236236
token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
237-
delim=repeat("#", n),
237+
delim=repeat("#", n as usize),
238238
string=s),
239239
token::ByteStr(v) => format!("b\"{}\"", v),
240240
token::ByteStrRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
241-
delim=repeat("#", n),
241+
delim=repeat("#", n as usize),
242242
string=s),
243243
};
244244

@@ -660,7 +660,7 @@ pub trait PrintState<'a> {
660660
}
661661
ast::StrStyle::Raw(n) => {
662662
(format!("r{delim}\"{string}\"{delim}",
663-
delim=repeat("#", n),
663+
delim=repeat("#", n as usize),
664664
string=st))
665665
}
666666
};

0 commit comments

Comments
 (0)