Skip to content

Commit 4ae956f

Browse files
committed
Remove ExtCtxt::expr_lit.
1 parent 7d30472 commit 4ae956f

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

compiler/rustc_ast/src/util/literal.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ use rustc_span::Span;
88
use std::ascii;
99
use std::str;
1010

11+
// Escapes a string, represented as a symbol. Reuses the original symbol,
12+
// avoiding interning, if no changes are required.
13+
pub fn escape_string_symbol(symbol: Symbol) -> Symbol {
14+
let s = symbol.as_str();
15+
let escaped = s.escape_default().to_string();
16+
if s == escaped { symbol } else { Symbol::intern(&escaped) }
17+
}
18+
19+
// Escapes a char.
20+
pub fn escape_char_symbol(ch: char) -> Symbol {
21+
let s: String = ch.escape_default().map(Into::<char>::into).collect();
22+
Symbol::intern(&s)
23+
}
24+
25+
// Escapes a byte string.
26+
pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
27+
let s = bytes.escape_ascii().to_string();
28+
Symbol::intern(&s)
29+
}
30+
1131
#[derive(Debug)]
1232
pub enum LitError {
1333
LexerError,
@@ -149,16 +169,11 @@ impl LitKind {
149169
pub fn synthesize_token_lit(&self) -> token::Lit {
150170
let (kind, symbol, suffix) = match *self {
151171
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
152-
// Don't re-intern unless the escaped string is different.
153-
let s = symbol.as_str();
154-
let escaped = s.escape_default().to_string();
155-
let symbol = if s == escaped { symbol } else { Symbol::intern(&escaped) };
156-
(token::Str, symbol, None)
172+
(token::Str, escape_string_symbol(symbol), None)
157173
}
158174
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
159175
LitKind::ByteStr(ref bytes, ast::StrStyle::Cooked) => {
160-
let string = bytes.escape_ascii().to_string();
161-
(token::ByteStr, Symbol::intern(&string), None)
176+
(token::ByteStr, escape_byte_str_symbol(bytes), None)
162177
}
163178
LitKind::ByteStr(ref bytes, ast::StrStyle::Raw(n)) => {
164179
// Unwrap because raw byte string literals can only contain ASCII.
@@ -169,10 +184,7 @@ impl LitKind {
169184
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
170185
(token::Byte, Symbol::intern(&string), None)
171186
}
172-
LitKind::Char(ch) => {
173-
let string: String = ch.escape_default().map(Into::<char>::into).collect();
174-
(token::Char, Symbol::intern(&string), None)
175-
}
187+
LitKind::Char(ch) => (token::Char, escape_char_symbol(ch), None),
176188
LitKind::Int(n, ty) => {
177189
let suffix = match ty {
178190
ast::LitIntType::Unsigned(ty) => Some(ty.name()),

compiler/rustc_expand/src/build.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::base::ExtCtxt;
2-
use rustc_ast::attr;
32
use rustc_ast::ptr::P;
43
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
5-
use rustc_data_structures::sync::Lrc;
4+
use rustc_ast::{attr, token, util::literal};
65
use rustc_span::source_map::Spanned;
76
use rustc_span::symbol::{kw, sym, Ident, Symbol};
87
use rustc_span::Span;
@@ -332,36 +331,36 @@ impl<'a> ExtCtxt<'a> {
332331
self.expr_struct(span, self.path_ident(span, id), fields)
333332
}
334333

335-
fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
336-
let token_lit = lit_kind.synthesize_token_lit();
337-
self.expr(span, ast::ExprKind::Lit(token_lit))
334+
pub fn expr_usize(&self, span: Span, n: usize) -> P<ast::Expr> {
335+
let suffix = Some(ast::UintTy::Usize.name());
336+
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
337+
self.expr(span, ast::ExprKind::Lit(lit))
338338
}
339339

340-
pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
341-
self.expr_lit(
342-
span,
343-
ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
344-
)
345-
}
346-
347-
pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
348-
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
340+
pub fn expr_u32(&self, span: Span, n: u32) -> P<ast::Expr> {
341+
let suffix = Some(ast::UintTy::U32.name());
342+
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
343+
self.expr(span, ast::ExprKind::Lit(lit))
349344
}
350345

351-
pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
352-
self.expr_lit(sp, ast::LitKind::Bool(value))
346+
pub fn expr_bool(&self, span: Span, value: bool) -> P<ast::Expr> {
347+
let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None);
348+
self.expr(span, ast::ExprKind::Lit(lit))
353349
}
354350

355-
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
356-
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
351+
pub fn expr_str(&self, span: Span, s: Symbol) -> P<ast::Expr> {
352+
let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None);
353+
self.expr(span, ast::ExprKind::Lit(lit))
357354
}
358355

359-
pub fn expr_char(&self, sp: Span, ch: char) -> P<ast::Expr> {
360-
self.expr_lit(sp, ast::LitKind::Char(ch))
356+
pub fn expr_char(&self, span: Span, ch: char) -> P<ast::Expr> {
357+
let lit = token::Lit::new(token::Char, literal::escape_char_symbol(ch), None);
358+
self.expr(span, ast::ExprKind::Lit(lit))
361359
}
362360

363-
pub fn expr_byte_str(&self, sp: Span, bytes: Vec<u8>) -> P<ast::Expr> {
364-
self.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(bytes), ast::StrStyle::Cooked))
361+
pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> {
362+
let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
363+
self.expr(span, ast::ExprKind::Lit(lit))
365364
}
366365

367366
/// `[expr1, expr2, ...]`

0 commit comments

Comments
 (0)