Pass symbols to ExtCtxt::std_path instead of strings.

Because this function is hot.

Also remove the dead `ty_option` function.
This commit is contained in:
Nicholas Nethercote 2019-05-22 14:41:15 +10:00
parent 26451ef7b5
commit 58c68d00fd
12 changed files with 71 additions and 57 deletions

View File

@ -969,10 +969,10 @@ impl<'a> ExtCtxt<'a> {
pub fn ident_of(&self, st: &str) -> ast::Ident {
ast::Ident::from_str(st)
}
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
pub fn std_path(&self, components: &[Symbol]) -> Vec<ast::Ident> {
let def_site = DUMMY_SP.apply_mark(self.current_expansion.mark);
iter::once(Ident::new(kw::DollarCrate, def_site))
.chain(components.iter().map(|s| self.ident_of(s)))
.chain(components.iter().map(|&s| Ident::with_empty_ctxt(s)))
.collect()
}
pub fn name_of(&self, st: &str) -> ast::Name {

View File

@ -3,11 +3,11 @@ use crate::attr;
use crate::source_map::{dummy_spanned, respan, Spanned};
use crate::ext::base::ExtCtxt;
use crate::ptr::P;
use crate::symbol::{Symbol, kw};
use crate::symbol::{kw, sym, Symbol};
use crate::ThinVec;
use rustc_target::spec::abi::Abi;
use syntax_pos::{Pos, Span, DUMMY_SP};
use syntax_pos::{Pos, Span};
pub trait AstBuilder {
// paths
@ -49,7 +49,6 @@ pub trait AstBuilder {
ty: P<ast::Ty>,
mutbl: ast::Mutability) -> P<ast::Ty>;
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
fn typaram(&self,
@ -425,15 +424,6 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
}
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
self.ty_path(
self.path_all(DUMMY_SP,
true,
self.std_path(&["option", "Option"]),
vec![ast::GenericArg::Type(ty)],
Vec::new()))
}
fn ty_infer(&self, span: Span) -> P<ast::Ty> {
self.ty(span, ast::TyKind::Infer)
}
@ -735,7 +725,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
self.expr(sp, ast::ExprKind::Array(exprs))
}
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
self.expr_call_global(sp, self.std_path(&[sym::vec, sym::Vec, sym::new]),
Vec::new())
}
fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
@ -751,12 +741,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let some = self.std_path(&["option", "Option", "Some"]);
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
self.expr_call_global(sp, some, vec![expr])
}
fn expr_none(&self, sp: Span) -> P<ast::Expr> {
let none = self.std_path(&["option", "Option", "None"]);
let none = self.std_path(&[sym::option, sym::Option, sym::None]);
let none = self.path_global(sp, none);
self.expr_path(none)
}
@ -780,7 +770,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
self.expr_call_global(
span,
self.std_path(&["rt", "begin_panic"]),
self.std_path(&[sym::rt, sym::begin_panic]),
vec![
self.expr_str(span, msg),
expr_loc_ptr])
@ -791,19 +781,19 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
}
fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let ok = self.std_path(&["result", "Result", "Ok"]);
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
self.expr_call_global(sp, ok, vec![expr])
}
fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let err = self.std_path(&["result", "Result", "Err"]);
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
self.expr_call_global(sp, err, vec![expr])
}
fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
let ok = self.std_path(&["result", "Result", "Ok"]);
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
let ok_path = self.path_global(sp, ok);
let err = self.std_path(&["result", "Result", "Err"]);
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
let err_path = self.path_global(sp, err);
let binding_variable = self.ident_of("__try_var");
@ -867,25 +857,25 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
}
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["option", "Option", "Some"]);
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}
fn pat_none(&self, span: Span) -> P<ast::Pat> {
let some = self.std_path(&["option", "Option", "None"]);
let some = self.std_path(&[sym::option, sym::Option, sym::None]);
let path = self.path_global(span, some);
self.pat_path(span, path)
}
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["result", "Result", "Ok"]);
let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["result", "Result", "Err"]);
let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}

View File

@ -7,7 +7,7 @@ use syntax::attr;
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::ptr::P;
use syntax::symbol::{kw, sym};
use syntax::symbol::{kw, sym, Symbol};
use syntax_pos::Span;
pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>,
@ -115,7 +115,7 @@ fn cs_clone_shallow(name: &str,
// set the expn ID so we can use the unstable struct.
let span = span.with_ctxt(cx.backtrace());
let assert_path = cx.path_all(span, true,
cx.std_path(&["clone", helper_name]),
cx.std_path(&[sym::clone, Symbol::intern(helper_name)]),
vec![GenericArg::Type(ty)], vec![]);
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
}
@ -157,7 +157,7 @@ fn cs_clone(name: &str,
-> P<Expr> {
let ctor_path;
let all_fields;
let fn_path = cx.std_path(&["clone", "Clone", "clone"]);
let fn_path = cx.std_path(&[sym::clone, sym::Clone, sym::clone]);
let subcall = |cx: &mut ExtCtxt<'_>, field: &FieldInfo<'_>| {
let args = vec![cx.expr_addr_of(field.span, field.self_.clone())];
cx.expr_call_global(field.span, fn_path.clone(), args)

View File

@ -6,7 +6,7 @@ use syntax::ast::{self, Expr, MetaItem, GenericArg};
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::ptr::P;
use syntax::symbol::sym;
use syntax::symbol::{sym, Symbol};
use syntax_pos::Span;
pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
@ -54,7 +54,7 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
// set the expn ID so we can use the unstable struct.
let span = span.with_ctxt(cx.backtrace());
let assert_path = cx.path_all(span, true,
cx.std_path(&["cmp", helper_name]),
cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
vec![GenericArg::Type(ty)], vec![]);
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
}

View File

@ -55,9 +55,9 @@ pub fn ordering_collapsed(cx: &mut ExtCtxt<'_>,
pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
let test_id = cx.ident_of("cmp").gensym();
let equals_path = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
let equals_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]);
let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
// Builds:
//

View File

@ -8,7 +8,7 @@ use syntax::ast::{self, BinOpKind, Expr, MetaItem};
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::ptr::P;
use syntax::symbol::sym;
use syntax::symbol::{sym, Symbol};
use syntax_pos::Span;
pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt<'_>,
@ -114,11 +114,11 @@ pub fn some_ordering_collapsed(cx: &mut ExtCtxt<'_>,
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
let test_id = cx.ident_of("cmp").gensym();
let ordering = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
let ordering = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
let ordering_expr = cx.expr_path(ordering.clone());
let equals_expr = cx.expr_some(span, ordering_expr);
let partial_cmp_path = cx.std_path(&["cmp", "PartialOrd", "partial_cmp"]);
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
// Builds:
//
@ -188,7 +188,8 @@ fn cs_op(less: bool,
span: Span,
substr: &Substructure<'_>) -> P<Expr> {
let ordering_path = |cx: &mut ExtCtxt<'_>, name: &str| {
cx.expr_path(cx.path_global(span, cx.std_path(&["cmp", "Ordering", name])))
cx.expr_path(cx.path_global(
span, cx.std_path(&[sym::cmp, sym::Ordering, Symbol::intern(name)])))
};
let par_cmp = |cx: &mut ExtCtxt<'_>, span, self_f: P<Expr>, other_fs: &[P<Expr>], default| {
@ -198,9 +199,9 @@ fn cs_op(less: bool,
};
// `PartialOrd::partial_cmp(self.fi, other.fi)`
let cmp_path = cx.expr_path(cx.path_global(span, cx.std_path(&["cmp",
"PartialOrd",
"partial_cmp"])));
let cmp_path = cx.expr_path(cx.path_global(span, cx.std_path(&[sym::cmp,
sym::PartialOrd,
sym::partial_cmp])));
let cmp = cx.expr_call(span,
cmp_path,
vec![cx.expr_addr_of(span, self_f),
@ -208,9 +209,9 @@ fn cs_op(less: bool,
let default = ordering_path(cx, default);
// `Option::unwrap_or(_, Ordering::Equal)`
let unwrap_path = cx.expr_path(cx.path_global(span, cx.std_path(&["option",
"Option",
"unwrap_or"])));
let unwrap_path = cx.expr_path(cx.path_global(span, cx.std_path(&[sym::option,
sym::Option,
sym::unwrap_or])));
cx.expr_call(span, unwrap_path, vec![cmp, default])
};
@ -256,9 +257,9 @@ fn cs_op(less: bool,
// `Ordering::then_with(Option::unwrap_or(..), ..)`
let then_with_path = cx.expr_path(cx.path_global(span,
cx.std_path(&["cmp",
"Ordering",
"then_with"])));
cx.std_path(&[sym::cmp,
sym::Ordering,
sym::then_with])));
cx.expr_call(span, then_with_path, vec![par_cmp, cx.lambda0(span, subexpr)])
},
|cx, args| {

View File

@ -6,7 +6,7 @@ use syntax::ast::{Expr, MetaItem};
use syntax::ext::base::{Annotatable, DummyResult, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::ptr::P;
use syntax::symbol::sym;
use syntax::symbol::{kw, sym};
use syntax::span_err;
use syntax_pos::Span;
@ -47,7 +47,8 @@ fn default_substructure(cx: &mut ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>)
-> P<Expr> {
let default_ident = cx.std_path(&["default", "Default", "default"]);
// Note that `kw::Default` is "default" and `sym::Default` is "Default"!
let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
return match *substr.fields {

View File

@ -6,6 +6,7 @@ use syntax::ast::{Expr, MetaItem, Mutability};
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::ptr::P;
use syntax::symbol::sym;
use syntax_pos::Span;
pub fn expand_deriving_hash(cx: &mut ExtCtxt<'_>,
@ -60,7 +61,7 @@ fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructu
};
let call_hash = |span, thing_expr| {
let hash_path = {
let strs = cx.std_path(&["hash", "Hash", "hash"]);
let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]);
cx.expr_path(cx.path_global(span, strs))
};

View File

@ -150,7 +150,7 @@ fn call_intrinsic(cx: &ExtCtxt<'_>,
mark.set_expn_info(info);
span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
}
let path = cx.std_path(&["intrinsics", intrinsic]);
let path = cx.std_path(&[sym::intrinsics, Symbol::intern(intrinsic)]);
let call = cx.expr_call_global(span, path, args);
cx.expr_block(P(ast::Block {

View File

@ -27,7 +27,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
let lt = cx.lifetime(sp, Ident::with_empty_ctxt(kw::StaticLifetime));
cx.expr_path(cx.path_all(sp,
true,
cx.std_path(&["option", "Option", "None"]),
cx.std_path(&[sym::option, sym::Option, sym::None]),
vec![GenericArg::Type(cx.ty_rptr(sp,
cx.ty_ident(sp,
Ident::with_empty_ctxt(sym::str)),
@ -37,7 +37,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
}
Ok(s) => {
cx.expr_call_global(sp,
cx.std_path(&["option", "Option", "Some"]),
cx.std_path(&[sym::option, sym::Option, sym::Some]),
vec![cx.expr_str(sp, Symbol::intern(&s))])
}
};

View File

@ -387,7 +387,7 @@ impl<'a, 'b> Context<'a, 'b> {
}
fn rtpath(ecx: &ExtCtxt<'_>, s: &str) -> Vec<ast::Ident> {
ecx.std_path(&["fmt", "rt", "v1", s])
ecx.std_path(&[sym::fmt, sym::rt, sym::v1, Symbol::intern(s)])
}
fn build_count(&self, c: parse::Count<'_>) -> P<ast::Expr> {
@ -644,7 +644,7 @@ impl<'a, 'b> Context<'a, 'b> {
("new_v1_formatted", vec![pieces, args_slice, fmt])
};
let path = self.ecx.std_path(&["fmt", "Arguments", fn_name]);
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
self.ecx.expr_call_global(self.macsp, path, fn_args)
}
@ -675,14 +675,14 @@ impl<'a, 'b> Context<'a, 'b> {
}
}
Count => {
let path = ecx.std_path(&["fmt", "ArgumentV1", "from_usize"]);
let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::from_usize]);
return ecx.expr_call_global(macsp, path, vec![arg]);
}
};
let path = ecx.std_path(&["fmt", trait_, "fmt"]);
let path = ecx.std_path(&[sym::fmt, Symbol::intern(trait_), sym::fmt]);
let format_fn = ecx.path_global(sp, path);
let path = ecx.std_path(&["fmt", "ArgumentV1", "new"]);
let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::new]);
ecx.expr_call_global(macsp, path, vec![arg, ecx.expr_path(format_fn)])
}
}

View File

@ -132,6 +132,8 @@ symbols! {
always,
any,
arbitrary_self_types,
Arguments,
ArgumentV1,
arm_target_feature,
asm,
associated_consts,
@ -145,6 +147,7 @@ symbols! {
automatically_derived,
avx512_target_feature,
await_macro,
begin_panic,
bench,
bin,
bind_by_move_pattern_guards,
@ -164,9 +167,11 @@ symbols! {
cfg_target_thread_local,
cfg_target_vendor,
clone,
Clone,
clone_closures,
clone_from,
closure_to_fn_coercion,
cmp,
cmpxchg16b_target_feature,
cold,
compile_error,
@ -200,6 +205,7 @@ symbols! {
custom_test_frameworks,
c_variadic,
decl_macro,
Default,
default_lib_allocator,
default_type_parameter_fallback,
default_type_params,
@ -234,6 +240,7 @@ symbols! {
enable,
err,
Err,
Equal,
except,
exclusive_range_pattern,
exhaustive_integer_patterns,
@ -256,6 +263,7 @@ symbols! {
field,
field_init_shorthand,
file,
fmt,
fmt_internals,
fn_must_use,
forbid,
@ -265,6 +273,7 @@ symbols! {
from_error,
from_generator,
from_ok,
from_usize,
fundamental,
future,
Future,
@ -275,6 +284,8 @@ symbols! {
global_allocator,
global_asm,
globs,
hash,
Hash,
hexagon_target_feature,
hidden,
homogeneous_aggregate,
@ -371,6 +382,7 @@ symbols! {
negate_unsigned,
never,
never_type,
new,
next,
__next,
nll,
@ -405,6 +417,8 @@ symbols! {
option,
Option,
opt_out_copy,
Ord,
Ordering,
Output,
overlapping_marker_traits,
packed,
@ -413,6 +427,8 @@ symbols! {
panic_impl,
panic_implementation,
panic_runtime,
partial_cmp,
PartialOrd,
passes,
path,
pattern_parentheses,
@ -473,6 +489,7 @@ symbols! {
Result,
Return,
rlib,
rt,
rtm_target_feature,
rust,
rust_2015_preview,
@ -576,6 +593,7 @@ symbols! {
test_case,
test_removed_feature,
test_runner,
then_with,
thread_local,
tool_attributes,
tool_lints,
@ -615,12 +633,15 @@ symbols! {
untagged_unions,
unwind,
unwind_attributes,
unwrap_or,
used,
use_extern_macros,
use_nested_groups,
usize,
v1,
val,
vec,
Vec,
vis,
visible_private_types,
volatile,