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.
This commit is contained in:
commit
3dfda16525
7 changed files with 19 additions and 14 deletions
|
@ -163,9 +163,9 @@ impl<'a> Quote for &'a str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Quote for usize {
|
impl Quote for u16 {
|
||||||
fn quote(self) -> TokenStream {
|
fn quote(self) -> TokenStream {
|
||||||
TokenTree::from(Literal::usize_unsuffixed(self)).into()
|
TokenTree::from(Literal::u16_unsuffixed(self)).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ macro_rules! literals {
|
||||||
($($i:ident),*; $($raw:ident),*) => {
|
($($i:ident),*; $($raw:ident),*) => {
|
||||||
pub enum LiteralKind {
|
pub enum LiteralKind {
|
||||||
$($i,)*
|
$($i,)*
|
||||||
$($raw(usize),)*
|
$($raw(u16),)*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LiteralKind {
|
impl LiteralKind {
|
||||||
|
|
|
@ -1255,8 +1255,8 @@ pub enum StrStyle {
|
||||||
Cooked,
|
Cooked,
|
||||||
/// A raw string, like `r##"foo"##`
|
/// A raw string, like `r##"foo"##`
|
||||||
///
|
///
|
||||||
/// The uint is the number of `#` symbols used
|
/// The value is the number of `#` symbols used.
|
||||||
Raw(usize)
|
Raw(u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A literal
|
/// A literal
|
||||||
|
|
|
@ -139,6 +139,7 @@ pub trait AstBuilder {
|
||||||
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
|
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
|
||||||
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
|
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
|
||||||
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
|
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
|
||||||
|
fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr>;
|
||||||
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
|
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
|
||||||
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
|
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
|
||||||
|
|
||||||
|
@ -708,6 +709,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||||
self.expr_lit(sp, ast::LitKind::Int(u as u128,
|
self.expr_lit(sp, ast::LitKind::Int(u as u128,
|
||||||
ast::LitIntType::Unsigned(ast::UintTy::U32)))
|
ast::LitIntType::Unsigned(ast::UintTy::U32)))
|
||||||
}
|
}
|
||||||
|
fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
|
||||||
|
self.expr_lit(sp, ast::LitKind::Int(u as u128,
|
||||||
|
ast::LitIntType::Unsigned(ast::UintTy::U16)))
|
||||||
|
}
|
||||||
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
|
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
|
||||||
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
|
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -623,7 +623,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
|
||||||
($name: expr, $suffix: expr, $content: expr $(, $count: expr)*) => {{
|
($name: expr, $suffix: expr, $content: expr $(, $count: expr)*) => {{
|
||||||
let name = mk_name(cx, sp, ast::Ident::with_empty_ctxt($content));
|
let name = mk_name(cx, sp, ast::Ident::with_empty_ctxt($content));
|
||||||
let inner = cx.expr_call(sp, mk_token_path(cx, sp, $name), vec![
|
let inner = cx.expr_call(sp, mk_token_path(cx, sp, $name), vec![
|
||||||
name $(, cx.expr_usize(sp, $count))*
|
name $(, cx.expr_u16(sp, $count))*
|
||||||
]);
|
]);
|
||||||
let suffix = match $suffix {
|
let suffix = match $suffix {
|
||||||
Some(name) => cx.expr_some(sp, mk_name(cx, sp, ast::Ident::with_empty_ctxt(name))),
|
Some(name) => cx.expr_some(sp, mk_name(cx, sp, ast::Ident::with_empty_ctxt(name))),
|
||||||
|
|
|
@ -133,12 +133,12 @@ impl<'a> StringReader<'a> {
|
||||||
Ok(ret_val)
|
Ok(ret_val)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_unterminated_raw_string(&self, pos: BytePos, hash_count: usize) {
|
fn fail_unterminated_raw_string(&self, pos: BytePos, hash_count: u16) {
|
||||||
let mut err = self.struct_span_fatal(pos, pos, "unterminated raw string");
|
let mut err = self.struct_span_fatal(pos, pos, "unterminated raw string");
|
||||||
err.span_label(self.mk_sp(pos, pos), "unterminated raw string");
|
err.span_label(self.mk_sp(pos, pos), "unterminated raw string");
|
||||||
if hash_count > 0 {
|
if hash_count > 0 {
|
||||||
err.note(&format!("this raw string should be terminated with `\"{}`",
|
err.note(&format!("this raw string should be terminated with `\"{}`",
|
||||||
"#".repeat(hash_count)));
|
"#".repeat(hash_count as usize)));
|
||||||
}
|
}
|
||||||
err.emit();
|
err.emit();
|
||||||
FatalError.raise();
|
FatalError.raise();
|
||||||
|
@ -1439,7 +1439,7 @@ impl<'a> StringReader<'a> {
|
||||||
'r' => {
|
'r' => {
|
||||||
let start_bpos = self.pos;
|
let start_bpos = self.pos;
|
||||||
self.bump();
|
self.bump();
|
||||||
let mut hash_count = 0;
|
let mut hash_count: u16 = 0;
|
||||||
while self.ch_is('#') {
|
while self.ch_is('#') {
|
||||||
self.bump();
|
self.bump();
|
||||||
hash_count += 1;
|
hash_count += 1;
|
||||||
|
|
|
@ -72,9 +72,9 @@ pub enum Lit {
|
||||||
Integer(ast::Name),
|
Integer(ast::Name),
|
||||||
Float(ast::Name),
|
Float(ast::Name),
|
||||||
Str_(ast::Name),
|
Str_(ast::Name),
|
||||||
StrRaw(ast::Name, usize), /* raw str delimited by n hash symbols */
|
StrRaw(ast::Name, u16), /* raw str delimited by n hash symbols */
|
||||||
ByteStr(ast::Name),
|
ByteStr(ast::Name),
|
||||||
ByteStrRaw(ast::Name, usize), /* raw byte str delimited by n hash symbols */
|
ByteStrRaw(ast::Name, u16), /* raw byte str delimited by n hash symbols */
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Lit {
|
impl Lit {
|
||||||
|
|
|
@ -234,11 +234,11 @@ pub fn token_to_string(tok: &Token) -> String {
|
||||||
token::Integer(c) => c.to_string(),
|
token::Integer(c) => c.to_string(),
|
||||||
token::Str_(s) => format!("\"{}\"", s),
|
token::Str_(s) => format!("\"{}\"", s),
|
||||||
token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
|
token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
|
||||||
delim=repeat("#", n),
|
delim=repeat("#", n as usize),
|
||||||
string=s),
|
string=s),
|
||||||
token::ByteStr(v) => format!("b\"{}\"", v),
|
token::ByteStr(v) => format!("b\"{}\"", v),
|
||||||
token::ByteStrRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
|
token::ByteStrRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
|
||||||
delim=repeat("#", n),
|
delim=repeat("#", n as usize),
|
||||||
string=s),
|
string=s),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -660,7 +660,7 @@ pub trait PrintState<'a> {
|
||||||
}
|
}
|
||||||
ast::StrStyle::Raw(n) => {
|
ast::StrStyle::Raw(n) => {
|
||||||
(format!("r{delim}\"{string}\"{delim}",
|
(format!("r{delim}\"{string}\"{delim}",
|
||||||
delim=repeat("#", n),
|
delim=repeat("#", n as usize),
|
||||||
string=st))
|
string=st))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue