Skip to content

Commit d7a041f

Browse files
committed
Make ExtCtxt::expr_lit non-pub.
By using `expr_str` more and adding `expr_{char,byte_str}`.
1 parent 7d1e5a4 commit d7a041f

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

compiler/rustc_builtin_macros/src/concat_bytes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_ast as ast;
22
use rustc_ast::{ptr::P, tokenstream::TokenStream};
3-
use rustc_data_structures::sync::Lrc;
43
use rustc_errors::Applicability;
54
use rustc_expand::base::{self, DummyResult};
65

@@ -185,5 +184,5 @@ pub fn expand_concat_bytes(
185184
return base::MacEager::expr(DummyResult::raw_expr(sp, true));
186185
}
187186
let sp = cx.with_def_site_ctxt(sp);
188-
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(accumulator))))
187+
base::MacEager::expr(cx.expr_byte_str(sp, accumulator))
189188
}

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
5252

5353
// We want to make sure we have the ctxt set so that we can use unstable methods
5454
let span = cx.with_def_site_ctxt(span);
55-
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
55+
let name = cx.expr_str(span, ident.name);
5656
let fmt = substr.nonselflike_args[0].clone();
5757

5858
// Struct and tuples are similar enough that we use the same code for both,
@@ -89,10 +89,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
8989
for i in 0..fields.len() {
9090
let field = &fields[i];
9191
if is_struct {
92-
let name = cx.expr_lit(
93-
field.span,
94-
ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
95-
);
92+
let name = cx.expr_str(field.span, field.name.unwrap().name);
9693
args.push(name);
9794
}
9895
// Use an extra indirection to make sure this works for unsized types.
@@ -108,10 +105,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
108105

109106
for field in fields {
110107
if is_struct {
111-
name_exprs.push(cx.expr_lit(
112-
field.span,
113-
ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
114-
));
108+
name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
115109
}
116110

117111
// Use an extra indirection to make sure this works for unsized types.

compiler/rustc_builtin_macros/src/format.rs

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

925925
// Build the format
926-
let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill));
926+
let fill = self.ecx.expr_char(sp, fill);
927927
let align = |name| {
928928
let mut p = Context::rtpath(self.ecx, sym::Alignment);
929929
p.push(Ident::new(name, sp));

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn expand_include_bytes(
216216
}
217217
};
218218
match cx.source_map().load_binary_file(&file) {
219-
Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(bytes.into()))),
219+
Ok(bytes) => base::MacEager::expr(cx.expr_byte_str(sp, bytes)),
220220
Err(e) => {
221221
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
222222
DummyResult::any(sp)

compiler/rustc_expand/src/build.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::base::ExtCtxt;
33
use rustc_ast::attr;
44
use rustc_ast::ptr::P;
55
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
6+
use rustc_data_structures::sync::Lrc;
67
use rustc_span::source_map::Spanned;
78
use rustc_span::symbol::{kw, sym, Ident, Symbol};
89

@@ -330,23 +331,38 @@ impl<'a> ExtCtxt<'a> {
330331
self.expr_struct(span, self.path_ident(span, id), fields)
331332
}
332333

333-
pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
334+
fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
334335
let lit = ast::Lit::from_lit_kind(lit_kind, span);
335336
self.expr(span, ast::ExprKind::Lit(lit))
336337
}
338+
337339
pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
338340
self.expr_lit(
339341
span,
340342
ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
341343
)
342344
}
345+
343346
pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
344347
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
345348
}
349+
346350
pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
347351
self.expr_lit(sp, ast::LitKind::Bool(value))
348352
}
349353

354+
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
355+
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
356+
}
357+
358+
pub fn expr_char(&self, sp: Span, ch: char) -> P<ast::Expr> {
359+
self.expr_lit(sp, ast::LitKind::Char(ch))
360+
}
361+
362+
pub fn expr_byte_str(&self, sp: Span, bytes: Vec<u8>) -> P<ast::Expr> {
363+
self.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(bytes)))
364+
}
365+
350366
/// `[expr1, expr2, ...]`
351367
pub fn expr_array(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
352368
self.expr(sp, ast::ExprKind::Array(exprs))
@@ -357,10 +373,6 @@ impl<'a> ExtCtxt<'a> {
357373
self.expr_addr_of(sp, self.expr_array(sp, exprs))
358374
}
359375

360-
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
361-
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
362-
}
363-
364376
pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
365377
self.expr(sp, ast::ExprKind::Cast(expr, ty))
366378
}

0 commit comments

Comments
 (0)