Skip to content

Commit 9f00808

Browse files
committed
Remove ExtCtxt::ident_of.
It's equivalent to `Ident::from_str_and_span`. The commit also introduces some more static symbols so that `Ident::new` can be used in various places instead of `Ident::from_str_and_span`. The commit also changes `Path::path` from a `&str` to a `Symbol`, which then allows the lifetime annotation to be removed from `Ty`. Also, the use of `Symbol` in `Bounds` removes the need for its lifetime annotation.
1 parent bccff14 commit 9f00808

File tree

19 files changed

+209
-134
lines changed

19 files changed

+209
-134
lines changed

src/librustc_builtin_macros/deriving/cmp/ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn expand_deriving_ord(
2929
name: sym::cmp,
3030
generics: Bounds::empty(),
3131
explicit_self: borrowed_explicit_self(),
32-
args: vec![(borrowed_self(), "other")],
32+
args: vec![(borrowed_self(), sym::other)],
3333
ret_ty: Literal(path_std!(cmp::Ordering)),
3434
attributes: attrs,
3535
is_unsafe: false,

src/librustc_builtin_macros/deriving/cmp/partial_eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn expand_deriving_partial_eq(
7171
name: $name,
7272
generics: Bounds::empty(),
7373
explicit_self: borrowed_explicit_self(),
74-
args: vec![(borrowed_self(), "other")],
74+
args: vec![(borrowed_self(), sym::other)],
7575
ret_ty: Literal(path_local!(bool)),
7676
attributes: attrs,
7777
is_unsafe: false,

src/librustc_builtin_macros/deriving/cmp/partial_ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn expand_deriving_partial_ord(
2525
name: $name,
2626
generics: Bounds::empty(),
2727
explicit_self: borrowed_explicit_self(),
28-
args: vec![(borrowed_self(), "other")],
28+
args: vec![(borrowed_self(), sym::other)],
2929
ret_ty: Literal(path_local!(bool)),
3030
attributes: attrs,
3131
is_unsafe: false,
@@ -52,7 +52,7 @@ pub fn expand_deriving_partial_ord(
5252
name: sym::partial_cmp,
5353
generics: Bounds::empty(),
5454
explicit_self: borrowed_explicit_self(),
55-
args: vec![(borrowed_self(), "other")],
55+
args: vec![(borrowed_self(), sym::other)],
5656
ret_ty,
5757
attributes: attrs,
5858
is_unsafe: false,

src/librustc_builtin_macros/deriving/debug.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn expand_deriving_debug(
3232
name: sym::fmt,
3333
generics: Bounds::empty(),
3434
explicit_self: borrowed_explicit_self(),
35-
args: vec![(fmtr, "f")],
35+
args: vec![(fmtr, sym::f)],
3636
ret_ty: Literal(path_std!(fmt::Result)),
3737
attributes: Vec::new(),
3838
is_unsafe: false,
@@ -62,7 +62,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
6262
// We want to make sure we have the ctxt set so that we can use unstable methods
6363
let span = cx.with_def_site_ctxt(span);
6464
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
65-
let builder = cx.ident_of("debug_trait_builder", span);
65+
let builder = Ident::new(sym::debug_trait_builder, span);
6666
let builder_expr = cx.expr_ident(span, builder);
6767

6868
let fmt = substr.nonself_args[0].clone();
@@ -71,7 +71,8 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
7171
match vdata {
7272
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
7373
// tuple struct/"normal" variant
74-
let expr = cx.expr_method_call(span, fmt, cx.ident_of("debug_tuple", span), vec![name]);
74+
let expr =
75+
cx.expr_method_call(span, fmt, Ident::new(sym::debug_tuple, span), vec![name]);
7576
stmts.push(cx.stmt_let(span, true, builder, expr));
7677

7778
for field in fields {
@@ -94,7 +95,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
9495
ast::VariantData::Struct(..) => {
9596
// normal struct/struct variant
9697
let expr =
97-
cx.expr_method_call(span, fmt, cx.ident_of("debug_struct", span), vec![name]);
98+
cx.expr_method_call(span, fmt, Ident::new(sym::debug_struct, span), vec![name]);
9899
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
99100

100101
for field in fields {
@@ -117,7 +118,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
117118
}
118119
}
119120

120-
let expr = cx.expr_method_call(span, builder_expr, cx.ident_of("finish", span), vec![]);
121+
let expr = cx.expr_method_call(span, builder_expr, Ident::new(sym::finish, span), vec![]);
121122

122123
stmts.push(cx.stmt_expr(expr));
123124
let block = cx.block(span, stmts);

src/librustc_builtin_macros/deriving/decodable.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::ast;
88
use rustc_ast::ast::{Expr, MetaItem, Mutability};
99
use rustc_ast::ptr::P;
1010
use rustc_expand::base::{Annotatable, ExtCtxt};
11-
use rustc_span::symbol::{sym, Symbol};
11+
use rustc_span::symbol::{sym, Ident, Symbol};
1212
use rustc_span::Span;
1313

1414
pub fn expand_deriving_rustc_decodable(
@@ -18,13 +18,13 @@ pub fn expand_deriving_rustc_decodable(
1818
item: &Annotatable,
1919
push: &mut dyn FnMut(Annotatable),
2020
) {
21-
let krate = "rustc_serialize";
22-
let typaram = "__D";
21+
let krate = sym::rustc_serialize;
22+
let typaram = sym::__D;
2323

2424
let trait_def = TraitDef {
2525
span,
2626
attributes: Vec::new(),
27-
path: Path::new_(vec![krate, "Decodable"], None, vec![], PathKind::Global),
27+
path: Path::new_(vec![krate, sym::Decodable], None, vec![], PathKind::Global),
2828
additional_bounds: Vec::new(),
2929
generics: Bounds::empty(),
3030
is_unsafe: false,
@@ -34,21 +34,21 @@ pub fn expand_deriving_rustc_decodable(
3434
generics: Bounds {
3535
bounds: vec![(
3636
typaram,
37-
vec![Path::new_(vec![krate, "Decoder"], None, vec![], PathKind::Global)],
37+
vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
3838
)],
3939
},
4040
explicit_self: None,
4141
args: vec![(
4242
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
43-
"d",
43+
sym::d,
4444
)],
4545
ret_ty: Literal(Path::new_(
4646
pathvec_std!(result::Result),
4747
None,
4848
vec![
4949
Box::new(Self_),
5050
Box::new(Literal(Path::new_(
51-
vec![typaram, "Error"],
51+
vec![typaram, sym::Error],
5252
None,
5353
vec![],
5454
PathKind::Local,
@@ -73,17 +73,17 @@ fn decodable_substructure(
7373
cx: &mut ExtCtxt<'_>,
7474
trait_span: Span,
7575
substr: &Substructure<'_>,
76-
krate: &str,
76+
krate: Symbol,
7777
) -> P<Expr> {
7878
let decoder = substr.nonself_args[0].clone();
7979
let recurse = vec![
80-
cx.ident_of(krate, trait_span),
81-
cx.ident_of("Decodable", trait_span),
82-
cx.ident_of("decode", trait_span),
80+
Ident::new(krate, trait_span),
81+
Ident::new(sym::Decodable, trait_span),
82+
Ident::new(sym::decode, trait_span),
8383
];
8484
let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
8585
// throw an underscore in front to suppress unused variable warnings
86-
let blkarg = cx.ident_of("_d", trait_span);
86+
let blkarg = Ident::new(sym::_d, trait_span);
8787
let blkdecoder = cx.expr_ident(trait_span, blkarg);
8888

8989
match *substr.fields {
@@ -92,7 +92,7 @@ fn decodable_substructure(
9292
Unnamed(ref fields, _) => fields.len(),
9393
Named(ref fields) => fields.len(),
9494
};
95-
let read_struct_field = cx.ident_of("read_struct_field", trait_span);
95+
let read_struct_field = Ident::new(sym::read_struct_field, trait_span);
9696

9797
let path = cx.path_ident(trait_span, substr.type_ident);
9898
let result =
@@ -115,7 +115,7 @@ fn decodable_substructure(
115115
cx.expr_method_call(
116116
trait_span,
117117
decoder,
118-
cx.ident_of("read_struct", trait_span),
118+
Ident::new(sym::read_struct, trait_span),
119119
vec![
120120
cx.expr_str(trait_span, substr.type_ident.name),
121121
cx.expr_usize(trait_span, nfields),
@@ -124,11 +124,11 @@ fn decodable_substructure(
124124
)
125125
}
126126
StaticEnum(_, ref fields) => {
127-
let variant = cx.ident_of("i", trait_span);
127+
let variant = Ident::new(sym::i, trait_span);
128128

129129
let mut arms = Vec::with_capacity(fields.len() + 1);
130130
let mut variants = Vec::with_capacity(fields.len());
131-
let rvariant_arg = cx.ident_of("read_enum_variant_arg", trait_span);
131+
let rvariant_arg = Ident::new(sym::read_enum_variant_arg, trait_span);
132132

133133
for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() {
134134
variants.push(cx.expr_str(v_span, ident.name));
@@ -163,13 +163,13 @@ fn decodable_substructure(
163163
let result = cx.expr_method_call(
164164
trait_span,
165165
blkdecoder,
166-
cx.ident_of("read_enum_variant", trait_span),
166+
Ident::new(sym::read_enum_variant, trait_span),
167167
vec![variant_vec, lambda],
168168
);
169169
cx.expr_method_call(
170170
trait_span,
171171
decoder,
172-
cx.ident_of("read_enum", trait_span),
172+
Ident::new(sym::read_enum, trait_span),
173173
vec![
174174
cx.expr_str(trait_span, substr.type_ident.name),
175175
cx.lambda1(trait_span, result, blkarg),

src/librustc_builtin_macros/deriving/default.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
3-
use crate::deriving::path_std;
43

54
use rustc_ast::ast::{Expr, MetaItem};
65
use rustc_ast::ptr::P;
@@ -21,7 +20,7 @@ pub fn expand_deriving_default(
2120
let trait_def = TraitDef {
2221
span,
2322
attributes: Vec::new(),
24-
path: path_std!(default::Default),
23+
path: Path::new(vec![kw::Default, sym::Default]),
2524
additional_bounds: Vec::new(),
2625
generics: Bounds::empty(),
2726
is_unsafe: false,

src/librustc_builtin_macros/deriving/encodable.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use crate::deriving::pathvec_std;
9292
use rustc_ast::ast::{Expr, ExprKind, MetaItem, Mutability};
9393
use rustc_ast::ptr::P;
9494
use rustc_expand::base::{Annotatable, ExtCtxt};
95-
use rustc_span::symbol::{sym, Symbol};
95+
use rustc_span::symbol::{sym, Ident, Symbol};
9696
use rustc_span::Span;
9797

9898
pub fn expand_deriving_rustc_encodable(
@@ -102,13 +102,13 @@ pub fn expand_deriving_rustc_encodable(
102102
item: &Annotatable,
103103
push: &mut dyn FnMut(Annotatable),
104104
) {
105-
let krate = "rustc_serialize";
106-
let typaram = "__S";
105+
let krate = sym::rustc_serialize;
106+
let typaram = sym::__S;
107107

108108
let trait_def = TraitDef {
109109
span,
110110
attributes: Vec::new(),
111-
path: Path::new_(vec![krate, "Encodable"], None, vec![], PathKind::Global),
111+
path: Path::new_(vec![krate, sym::Encodable], None, vec![], PathKind::Global),
112112
additional_bounds: Vec::new(),
113113
generics: Bounds::empty(),
114114
is_unsafe: false,
@@ -118,21 +118,26 @@ pub fn expand_deriving_rustc_encodable(
118118
generics: Bounds {
119119
bounds: vec![(
120120
typaram,
121-
vec![Path::new_(vec![krate, "Encoder"], None, vec![], PathKind::Global)],
121+
vec![Path::new_(vec![krate, sym::Encoder], None, vec![], PathKind::Global)],
122122
)],
123123
},
124124
explicit_self: borrowed_explicit_self(),
125125
args: vec![(
126126
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
127-
"s",
127+
// FIXME: we could use `sym::s` here, but making `s` a static
128+
// symbol changes the symbol index ordering in a way that makes
129+
// ui/lint/rfc-2457-non-ascii-idents/lint-confusable-idents.rs
130+
// fail. The linting code should be fixed so that its output
131+
// does not depend on the symbol index ordering.
132+
Symbol::intern("s"),
128133
)],
129134
ret_ty: Literal(Path::new_(
130135
pathvec_std!(result::Result),
131136
None,
132137
vec![
133138
Box::new(Tuple(Vec::new())),
134139
Box::new(Literal(Path::new_(
135-
vec![typaram, "Error"],
140+
vec![typaram, sym::Error],
136141
None,
137142
vec![],
138143
PathKind::Local,
@@ -157,24 +162,24 @@ fn encodable_substructure(
157162
cx: &mut ExtCtxt<'_>,
158163
trait_span: Span,
159164
substr: &Substructure<'_>,
160-
krate: &'static str,
165+
krate: Symbol,
161166
) -> P<Expr> {
162167
let encoder = substr.nonself_args[0].clone();
163168
// throw an underscore in front to suppress unused variable warnings
164-
let blkarg = cx.ident_of("_e", trait_span);
169+
let blkarg = Ident::new(sym::_e, trait_span);
165170
let blkencoder = cx.expr_ident(trait_span, blkarg);
166171
let fn_path = cx.expr_path(cx.path_global(
167172
trait_span,
168173
vec![
169-
cx.ident_of(krate, trait_span),
170-
cx.ident_of("Encodable", trait_span),
171-
cx.ident_of("encode", trait_span),
174+
Ident::new(krate, trait_span),
175+
Ident::new(sym::Encodable, trait_span),
176+
Ident::new(sym::encode, trait_span),
172177
],
173178
));
174179

175180
match *substr.fields {
176181
Struct(_, ref fields) => {
177-
let emit_struct_field = cx.ident_of("emit_struct_field", trait_span);
182+
let emit_struct_field = Ident::new(sym::emit_struct_field, trait_span);
178183
let mut stmts = Vec::new();
179184
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
180185
let name = match name {
@@ -214,7 +219,7 @@ fn encodable_substructure(
214219
cx.expr_method_call(
215220
trait_span,
216221
encoder,
217-
cx.ident_of("emit_struct", trait_span),
222+
Ident::new(sym::emit_struct, trait_span),
218223
vec![
219224
cx.expr_str(trait_span, substr.type_ident.name),
220225
cx.expr_usize(trait_span, fields.len()),
@@ -230,7 +235,7 @@ fn encodable_substructure(
230235
// actually exist.
231236
let me = cx.stmt_let(trait_span, false, blkarg, encoder);
232237
let encoder = cx.expr_ident(trait_span, blkarg);
233-
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg", trait_span);
238+
let emit_variant_arg = Ident::new(sym::emit_enum_variant_arg, trait_span);
234239
let mut stmts = Vec::new();
235240
if !fields.is_empty() {
236241
let last = fields.len() - 1;
@@ -263,7 +268,7 @@ fn encodable_substructure(
263268
let call = cx.expr_method_call(
264269
trait_span,
265270
blkencoder,
266-
cx.ident_of("emit_enum_variant", trait_span),
271+
Ident::new(sym::emit_enum_variant, trait_span),
267272
vec![
268273
name,
269274
cx.expr_usize(trait_span, idx),
@@ -275,7 +280,7 @@ fn encodable_substructure(
275280
let ret = cx.expr_method_call(
276281
trait_span,
277282
encoder,
278-
cx.ident_of("emit_enum", trait_span),
283+
Ident::new(sym::emit_enum, trait_span),
279284
vec![cx.expr_str(trait_span, substr.type_ident.name), blk],
280285
);
281286
cx.expr_block(cx.block(trait_span, vec![me, cx.stmt_expr(ret)]))

0 commit comments

Comments
 (0)