Skip to content

Commit 58c68d0

Browse files
committed
Pass symbols to ExtCtxt::std_path instead of strings.
Because this function is hot. Also remove the dead `ty_option` function.
1 parent 26451ef commit 58c68d0

File tree

12 files changed

+71
-57
lines changed

12 files changed

+71
-57
lines changed

Diff for: src/libsyntax/ext/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,10 @@ impl<'a> ExtCtxt<'a> {
969969
pub fn ident_of(&self, st: &str) -> ast::Ident {
970970
ast::Ident::from_str(st)
971971
}
972-
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
972+
pub fn std_path(&self, components: &[Symbol]) -> Vec<ast::Ident> {
973973
let def_site = DUMMY_SP.apply_mark(self.current_expansion.mark);
974974
iter::once(Ident::new(kw::DollarCrate, def_site))
975-
.chain(components.iter().map(|s| self.ident_of(s)))
975+
.chain(components.iter().map(|&s| Ident::with_empty_ctxt(s)))
976976
.collect()
977977
}
978978
pub fn name_of(&self, st: &str) -> ast::Name {

Diff for: src/libsyntax/ext/build.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use crate::attr;
33
use crate::source_map::{dummy_spanned, respan, Spanned};
44
use crate::ext::base::ExtCtxt;
55
use crate::ptr::P;
6-
use crate::symbol::{Symbol, kw};
6+
use crate::symbol::{kw, sym, Symbol};
77
use crate::ThinVec;
88

99
use rustc_target::spec::abi::Abi;
10-
use syntax_pos::{Pos, Span, DUMMY_SP};
10+
use syntax_pos::{Pos, Span};
1111

1212
pub trait AstBuilder {
1313
// paths
@@ -49,7 +49,6 @@ pub trait AstBuilder {
4949
ty: P<ast::Ty>,
5050
mutbl: ast::Mutability) -> P<ast::Ty>;
5151

52-
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
5352
fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
5453

5554
fn typaram(&self,
@@ -425,15 +424,6 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
425424
ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
426425
}
427426

428-
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
429-
self.ty_path(
430-
self.path_all(DUMMY_SP,
431-
true,
432-
self.std_path(&["option", "Option"]),
433-
vec![ast::GenericArg::Type(ty)],
434-
Vec::new()))
435-
}
436-
437427
fn ty_infer(&self, span: Span) -> P<ast::Ty> {
438428
self.ty(span, ast::TyKind::Infer)
439429
}
@@ -735,7 +725,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
735725
self.expr(sp, ast::ExprKind::Array(exprs))
736726
}
737727
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
738-
self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
728+
self.expr_call_global(sp, self.std_path(&[sym::vec, sym::Vec, sym::new]),
739729
Vec::new())
740730
}
741731
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> {
751741

752742

753743
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
754-
let some = self.std_path(&["option", "Option", "Some"]);
744+
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
755745
self.expr_call_global(sp, some, vec![expr])
756746
}
757747

758748
fn expr_none(&self, sp: Span) -> P<ast::Expr> {
759-
let none = self.std_path(&["option", "Option", "None"]);
749+
let none = self.std_path(&[sym::option, sym::Option, sym::None]);
760750
let none = self.path_global(sp, none);
761751
self.expr_path(none)
762752
}
@@ -780,7 +770,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
780770
let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
781771
self.expr_call_global(
782772
span,
783-
self.std_path(&["rt", "begin_panic"]),
773+
self.std_path(&[sym::rt, sym::begin_panic]),
784774
vec![
785775
self.expr_str(span, msg),
786776
expr_loc_ptr])
@@ -791,19 +781,19 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
791781
}
792782

793783
fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
794-
let ok = self.std_path(&["result", "Result", "Ok"]);
784+
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
795785
self.expr_call_global(sp, ok, vec![expr])
796786
}
797787

798788
fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
799-
let err = self.std_path(&["result", "Result", "Err"]);
789+
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
800790
self.expr_call_global(sp, err, vec![expr])
801791
}
802792

803793
fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
804-
let ok = self.std_path(&["result", "Result", "Ok"]);
794+
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
805795
let ok_path = self.path_global(sp, ok);
806-
let err = self.std_path(&["result", "Result", "Err"]);
796+
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
807797
let err_path = self.path_global(sp, err);
808798

809799
let binding_variable = self.ident_of("__try_var");
@@ -867,25 +857,25 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
867857
}
868858

869859
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
870-
let some = self.std_path(&["option", "Option", "Some"]);
860+
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
871861
let path = self.path_global(span, some);
872862
self.pat_tuple_struct(span, path, vec![pat])
873863
}
874864

875865
fn pat_none(&self, span: Span) -> P<ast::Pat> {
876-
let some = self.std_path(&["option", "Option", "None"]);
866+
let some = self.std_path(&[sym::option, sym::Option, sym::None]);
877867
let path = self.path_global(span, some);
878868
self.pat_path(span, path)
879869
}
880870

881871
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
882-
let some = self.std_path(&["result", "Result", "Ok"]);
872+
let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
883873
let path = self.path_global(span, some);
884874
self.pat_tuple_struct(span, path, vec![pat])
885875
}
886876

887877
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
888-
let some = self.std_path(&["result", "Result", "Err"]);
878+
let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
889879
let path = self.path_global(span, some);
890880
self.pat_tuple_struct(span, path, vec![pat])
891881
}

Diff for: src/libsyntax_ext/deriving/clone.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::attr;
77
use syntax::ext::base::{Annotatable, ExtCtxt};
88
use syntax::ext::build::AstBuilder;
99
use syntax::ptr::P;
10-
use syntax::symbol::{kw, sym};
10+
use syntax::symbol::{kw, sym, Symbol};
1111
use syntax_pos::Span;
1212

1313
pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>,
@@ -115,7 +115,7 @@ fn cs_clone_shallow(name: &str,
115115
// set the expn ID so we can use the unstable struct.
116116
let span = span.with_ctxt(cx.backtrace());
117117
let assert_path = cx.path_all(span, true,
118-
cx.std_path(&["clone", helper_name]),
118+
cx.std_path(&[sym::clone, Symbol::intern(helper_name)]),
119119
vec![GenericArg::Type(ty)], vec![]);
120120
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
121121
}
@@ -157,7 +157,7 @@ fn cs_clone(name: &str,
157157
-> P<Expr> {
158158
let ctor_path;
159159
let all_fields;
160-
let fn_path = cx.std_path(&["clone", "Clone", "clone"]);
160+
let fn_path = cx.std_path(&[sym::clone, sym::Clone, sym::clone]);
161161
let subcall = |cx: &mut ExtCtxt<'_>, field: &FieldInfo<'_>| {
162162
let args = vec![cx.expr_addr_of(field.span, field.self_.clone())];
163163
cx.expr_call_global(field.span, fn_path.clone(), args)

Diff for: src/libsyntax_ext/deriving/cmp/eq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::ast::{self, Expr, MetaItem, GenericArg};
66
use syntax::ext::base::{Annotatable, ExtCtxt};
77
use syntax::ext::build::AstBuilder;
88
use syntax::ptr::P;
9-
use syntax::symbol::sym;
9+
use syntax::symbol::{sym, Symbol};
1010
use syntax_pos::Span;
1111

1212
pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
@@ -54,7 +54,7 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
5454
// set the expn ID so we can use the unstable struct.
5555
let span = span.with_ctxt(cx.backtrace());
5656
let assert_path = cx.path_all(span, true,
57-
cx.std_path(&["cmp", helper_name]),
57+
cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
5858
vec![GenericArg::Type(ty)], vec![]);
5959
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
6060
}

Diff for: src/libsyntax_ext/deriving/cmp/ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ pub fn ordering_collapsed(cx: &mut ExtCtxt<'_>,
5555

5656
pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
5757
let test_id = cx.ident_of("cmp").gensym();
58-
let equals_path = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
58+
let equals_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
5959

60-
let cmp_path = cx.std_path(&["cmp", "Ord", "cmp"]);
60+
let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
6161

6262
// Builds:
6363
//

Diff for: src/libsyntax_ext/deriving/cmp/partial_ord.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syntax::ast::{self, BinOpKind, Expr, MetaItem};
88
use syntax::ext::base::{Annotatable, ExtCtxt};
99
use syntax::ext::build::AstBuilder;
1010
use syntax::ptr::P;
11-
use syntax::symbol::sym;
11+
use syntax::symbol::{sym, Symbol};
1212
use syntax_pos::Span;
1313

1414
pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt<'_>,
@@ -114,11 +114,11 @@ pub fn some_ordering_collapsed(cx: &mut ExtCtxt<'_>,
114114

115115
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
116116
let test_id = cx.ident_of("cmp").gensym();
117-
let ordering = cx.path_global(span, cx.std_path(&["cmp", "Ordering", "Equal"]));
117+
let ordering = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
118118
let ordering_expr = cx.expr_path(ordering.clone());
119119
let equals_expr = cx.expr_some(span, ordering_expr);
120120

121-
let partial_cmp_path = cx.std_path(&["cmp", "PartialOrd", "partial_cmp"]);
121+
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
122122

123123
// Builds:
124124
//
@@ -188,7 +188,8 @@ fn cs_op(less: bool,
188188
span: Span,
189189
substr: &Substructure<'_>) -> P<Expr> {
190190
let ordering_path = |cx: &mut ExtCtxt<'_>, name: &str| {
191-
cx.expr_path(cx.path_global(span, cx.std_path(&["cmp", "Ordering", name])))
191+
cx.expr_path(cx.path_global(
192+
span, cx.std_path(&[sym::cmp, sym::Ordering, Symbol::intern(name)])))
192193
};
193194

194195
let par_cmp = |cx: &mut ExtCtxt<'_>, span, self_f: P<Expr>, other_fs: &[P<Expr>], default| {
@@ -198,19 +199,19 @@ fn cs_op(less: bool,
198199
};
199200

200201
// `PartialOrd::partial_cmp(self.fi, other.fi)`
201-
let cmp_path = cx.expr_path(cx.path_global(span, cx.std_path(&["cmp",
202-
"PartialOrd",
203-
"partial_cmp"])));
202+
let cmp_path = cx.expr_path(cx.path_global(span, cx.std_path(&[sym::cmp,
203+
sym::PartialOrd,
204+
sym::partial_cmp])));
204205
let cmp = cx.expr_call(span,
205206
cmp_path,
206207
vec![cx.expr_addr_of(span, self_f),
207208
cx.expr_addr_of(span, other_f.clone())]);
208209

209210
let default = ordering_path(cx, default);
210211
// `Option::unwrap_or(_, Ordering::Equal)`
211-
let unwrap_path = cx.expr_path(cx.path_global(span, cx.std_path(&["option",
212-
"Option",
213-
"unwrap_or"])));
212+
let unwrap_path = cx.expr_path(cx.path_global(span, cx.std_path(&[sym::option,
213+
sym::Option,
214+
sym::unwrap_or])));
214215
cx.expr_call(span, unwrap_path, vec![cmp, default])
215216
};
216217

@@ -256,9 +257,9 @@ fn cs_op(less: bool,
256257

257258
// `Ordering::then_with(Option::unwrap_or(..), ..)`
258259
let then_with_path = cx.expr_path(cx.path_global(span,
259-
cx.std_path(&["cmp",
260-
"Ordering",
261-
"then_with"])));
260+
cx.std_path(&[sym::cmp,
261+
sym::Ordering,
262+
sym::then_with])));
262263
cx.expr_call(span, then_with_path, vec![par_cmp, cx.lambda0(span, subexpr)])
263264
},
264265
|cx, args| {

Diff for: src/libsyntax_ext/deriving/default.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::ast::{Expr, MetaItem};
66
use syntax::ext::base::{Annotatable, DummyResult, ExtCtxt};
77
use syntax::ext::build::AstBuilder;
88
use syntax::ptr::P;
9-
use syntax::symbol::sym;
9+
use syntax::symbol::{kw, sym};
1010
use syntax::span_err;
1111
use syntax_pos::Span;
1212

@@ -47,7 +47,8 @@ fn default_substructure(cx: &mut ExtCtxt<'_>,
4747
trait_span: Span,
4848
substr: &Substructure<'_>)
4949
-> P<Expr> {
50-
let default_ident = cx.std_path(&["default", "Default", "default"]);
50+
// Note that `kw::Default` is "default" and `sym::Default` is "Default"!
51+
let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
5152
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
5253

5354
return match *substr.fields {

Diff for: src/libsyntax_ext/deriving/hash.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use syntax::ast::{Expr, MetaItem, Mutability};
66
use syntax::ext::base::{Annotatable, ExtCtxt};
77
use syntax::ext::build::AstBuilder;
88
use syntax::ptr::P;
9+
use syntax::symbol::sym;
910
use syntax_pos::Span;
1011

1112
pub fn expand_deriving_hash(cx: &mut ExtCtxt<'_>,
@@ -60,7 +61,7 @@ fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructu
6061
};
6162
let call_hash = |span, thing_expr| {
6263
let hash_path = {
63-
let strs = cx.std_path(&["hash", "Hash", "hash"]);
64+
let strs = cx.std_path(&[sym::hash, sym::Hash, sym::hash]);
6465

6566
cx.expr_path(cx.path_global(span, strs))
6667
};

Diff for: src/libsyntax_ext/deriving/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn call_intrinsic(cx: &ExtCtxt<'_>,
150150
mark.set_expn_info(info);
151151
span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
152152
}
153-
let path = cx.std_path(&["intrinsics", intrinsic]);
153+
let path = cx.std_path(&[sym::intrinsics, Symbol::intern(intrinsic)]);
154154
let call = cx.expr_call_global(span, path, args);
155155

156156
cx.expr_block(P(ast::Block {

Diff for: src/libsyntax_ext/env.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
2727
let lt = cx.lifetime(sp, Ident::with_empty_ctxt(kw::StaticLifetime));
2828
cx.expr_path(cx.path_all(sp,
2929
true,
30-
cx.std_path(&["option", "Option", "None"]),
30+
cx.std_path(&[sym::option, sym::Option, sym::None]),
3131
vec![GenericArg::Type(cx.ty_rptr(sp,
3232
cx.ty_ident(sp,
3333
Ident::with_empty_ctxt(sym::str)),
@@ -37,7 +37,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
3737
}
3838
Ok(s) => {
3939
cx.expr_call_global(sp,
40-
cx.std_path(&["option", "Option", "Some"]),
40+
cx.std_path(&[sym::option, sym::Option, sym::Some]),
4141
vec![cx.expr_str(sp, Symbol::intern(&s))])
4242
}
4343
};

Diff for: src/libsyntax_ext/format.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a, 'b> Context<'a, 'b> {
387387
}
388388

389389
fn rtpath(ecx: &ExtCtxt<'_>, s: &str) -> Vec<ast::Ident> {
390-
ecx.std_path(&["fmt", "rt", "v1", s])
390+
ecx.std_path(&[sym::fmt, sym::rt, sym::v1, Symbol::intern(s)])
391391
}
392392

393393
fn build_count(&self, c: parse::Count<'_>) -> P<ast::Expr> {
@@ -644,7 +644,7 @@ impl<'a, 'b> Context<'a, 'b> {
644644
("new_v1_formatted", vec![pieces, args_slice, fmt])
645645
};
646646

647-
let path = self.ecx.std_path(&["fmt", "Arguments", fn_name]);
647+
let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
648648
self.ecx.expr_call_global(self.macsp, path, fn_args)
649649
}
650650

@@ -675,14 +675,14 @@ impl<'a, 'b> Context<'a, 'b> {
675675
}
676676
}
677677
Count => {
678-
let path = ecx.std_path(&["fmt", "ArgumentV1", "from_usize"]);
678+
let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::from_usize]);
679679
return ecx.expr_call_global(macsp, path, vec![arg]);
680680
}
681681
};
682682

683-
let path = ecx.std_path(&["fmt", trait_, "fmt"]);
683+
let path = ecx.std_path(&[sym::fmt, Symbol::intern(trait_), sym::fmt]);
684684
let format_fn = ecx.path_global(sp, path);
685-
let path = ecx.std_path(&["fmt", "ArgumentV1", "new"]);
685+
let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::new]);
686686
ecx.expr_call_global(macsp, path, vec![arg, ecx.expr_path(format_fn)])
687687
}
688688
}

0 commit comments

Comments
 (0)