Skip to content

Commit

Permalink
Remove ExtCtxt::ident_of.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Jul 16, 2020
1 parent bccff14 commit 9f00808
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 134 deletions.
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/cmp/ord.rs
Expand Up @@ -29,7 +29,7 @@ pub fn expand_deriving_ord(
name: sym::cmp,
generics: Bounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
args: vec![(borrowed_self(), sym::other)],
ret_ty: Literal(path_std!(cmp::Ordering)),
attributes: attrs,
is_unsafe: false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/cmp/partial_eq.rs
Expand Up @@ -71,7 +71,7 @@ pub fn expand_deriving_partial_eq(
name: $name,
generics: Bounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
args: vec![(borrowed_self(), sym::other)],
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
is_unsafe: false,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_builtin_macros/deriving/cmp/partial_ord.rs
Expand Up @@ -25,7 +25,7 @@ pub fn expand_deriving_partial_ord(
name: $name,
generics: Bounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
args: vec![(borrowed_self(), sym::other)],
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
is_unsafe: false,
Expand All @@ -52,7 +52,7 @@ pub fn expand_deriving_partial_ord(
name: sym::partial_cmp,
generics: Bounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
args: vec![(borrowed_self(), sym::other)],
ret_ty,
attributes: attrs,
is_unsafe: false,
Expand Down
11 changes: 6 additions & 5 deletions src/librustc_builtin_macros/deriving/debug.rs
Expand Up @@ -32,7 +32,7 @@ pub fn expand_deriving_debug(
name: sym::fmt,
generics: Bounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(fmtr, "f")],
args: vec![(fmtr, sym::f)],
ret_ty: Literal(path_std!(fmt::Result)),
attributes: Vec::new(),
is_unsafe: false,
Expand Down Expand Up @@ -62,7 +62,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
// We want to make sure we have the ctxt set so that we can use unstable methods
let span = cx.with_def_site_ctxt(span);
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
let builder = cx.ident_of("debug_trait_builder", span);
let builder = Ident::new(sym::debug_trait_builder, span);
let builder_expr = cx.expr_ident(span, builder);

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

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

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

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

stmts.push(cx.stmt_expr(expr));
let block = cx.block(span, stmts);
Expand Down
36 changes: 18 additions & 18 deletions src/librustc_builtin_macros/deriving/decodable.rs
Expand Up @@ -8,7 +8,7 @@ use rustc_ast::ast;
use rustc_ast::ast::{Expr, MetaItem, Mutability};
use rustc_ast::ptr::P;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span;

pub fn expand_deriving_rustc_decodable(
Expand All @@ -18,13 +18,13 @@ pub fn expand_deriving_rustc_decodable(
item: &Annotatable,
push: &mut dyn FnMut(Annotatable),
) {
let krate = "rustc_serialize";
let typaram = "__D";
let krate = sym::rustc_serialize;
let typaram = sym::__D;

let trait_def = TraitDef {
span,
attributes: Vec::new(),
path: Path::new_(vec![krate, "Decodable"], None, vec![], PathKind::Global),
path: Path::new_(vec![krate, sym::Decodable], None, vec![], PathKind::Global),
additional_bounds: Vec::new(),
generics: Bounds::empty(),
is_unsafe: false,
Expand All @@ -34,21 +34,21 @@ pub fn expand_deriving_rustc_decodable(
generics: Bounds {
bounds: vec![(
typaram,
vec![Path::new_(vec![krate, "Decoder"], None, vec![], PathKind::Global)],
vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
)],
},
explicit_self: None,
args: vec![(
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
"d",
sym::d,
)],
ret_ty: Literal(Path::new_(
pathvec_std!(result::Result),
None,
vec![
Box::new(Self_),
Box::new(Literal(Path::new_(
vec![typaram, "Error"],
vec![typaram, sym::Error],
None,
vec![],
PathKind::Local,
Expand All @@ -73,17 +73,17 @@ fn decodable_substructure(
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
krate: &str,
krate: Symbol,
) -> P<Expr> {
let decoder = substr.nonself_args[0].clone();
let recurse = vec![
cx.ident_of(krate, trait_span),
cx.ident_of("Decodable", trait_span),
cx.ident_of("decode", trait_span),
Ident::new(krate, trait_span),
Ident::new(sym::Decodable, trait_span),
Ident::new(sym::decode, trait_span),
];
let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
// throw an underscore in front to suppress unused variable warnings
let blkarg = cx.ident_of("_d", trait_span);
let blkarg = Ident::new(sym::_d, trait_span);
let blkdecoder = cx.expr_ident(trait_span, blkarg);

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

let path = cx.path_ident(trait_span, substr.type_ident);
let result =
Expand All @@ -115,7 +115,7 @@ fn decodable_substructure(
cx.expr_method_call(
trait_span,
decoder,
cx.ident_of("read_struct", trait_span),
Ident::new(sym::read_struct, trait_span),
vec![
cx.expr_str(trait_span, substr.type_ident.name),
cx.expr_usize(trait_span, nfields),
Expand All @@ -124,11 +124,11 @@ fn decodable_substructure(
)
}
StaticEnum(_, ref fields) => {
let variant = cx.ident_of("i", trait_span);
let variant = Ident::new(sym::i, trait_span);

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

for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() {
variants.push(cx.expr_str(v_span, ident.name));
Expand Down Expand Up @@ -163,13 +163,13 @@ fn decodable_substructure(
let result = cx.expr_method_call(
trait_span,
blkdecoder,
cx.ident_of("read_enum_variant", trait_span),
Ident::new(sym::read_enum_variant, trait_span),
vec![variant_vec, lambda],
);
cx.expr_method_call(
trait_span,
decoder,
cx.ident_of("read_enum", trait_span),
Ident::new(sym::read_enum, trait_span),
vec![
cx.expr_str(trait_span, substr.type_ident.name),
cx.lambda1(trait_span, result, blkarg),
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_builtin_macros/deriving/default.rs
@@ -1,6 +1,5 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::path_std;

use rustc_ast::ast::{Expr, MetaItem};
use rustc_ast::ptr::P;
Expand All @@ -21,7 +20,7 @@ pub fn expand_deriving_default(
let trait_def = TraitDef {
span,
attributes: Vec::new(),
path: path_std!(default::Default),
path: Path::new(vec![kw::Default, sym::Default]),
additional_bounds: Vec::new(),
generics: Bounds::empty(),
is_unsafe: false,
Expand Down
39 changes: 22 additions & 17 deletions src/librustc_builtin_macros/deriving/encodable.rs
Expand Up @@ -92,7 +92,7 @@ use crate::deriving::pathvec_std;
use rustc_ast::ast::{Expr, ExprKind, MetaItem, Mutability};
use rustc_ast::ptr::P;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span;

pub fn expand_deriving_rustc_encodable(
Expand All @@ -102,13 +102,13 @@ pub fn expand_deriving_rustc_encodable(
item: &Annotatable,
push: &mut dyn FnMut(Annotatable),
) {
let krate = "rustc_serialize";
let typaram = "__S";
let krate = sym::rustc_serialize;
let typaram = sym::__S;

let trait_def = TraitDef {
span,
attributes: Vec::new(),
path: Path::new_(vec![krate, "Encodable"], None, vec![], PathKind::Global),
path: Path::new_(vec![krate, sym::Encodable], None, vec![], PathKind::Global),
additional_bounds: Vec::new(),
generics: Bounds::empty(),
is_unsafe: false,
Expand All @@ -118,21 +118,26 @@ pub fn expand_deriving_rustc_encodable(
generics: Bounds {
bounds: vec![(
typaram,
vec![Path::new_(vec![krate, "Encoder"], None, vec![], PathKind::Global)],
vec![Path::new_(vec![krate, sym::Encoder], None, vec![], PathKind::Global)],
)],
},
explicit_self: borrowed_explicit_self(),
args: vec![(
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
"s",
// FIXME: we could use `sym::s` here, but making `s` a static
// symbol changes the symbol index ordering in a way that makes
// ui/lint/rfc-2457-non-ascii-idents/lint-confusable-idents.rs
// fail. The linting code should be fixed so that its output
// does not depend on the symbol index ordering.
Symbol::intern("s"),
)],
ret_ty: Literal(Path::new_(
pathvec_std!(result::Result),
None,
vec![
Box::new(Tuple(Vec::new())),
Box::new(Literal(Path::new_(
vec![typaram, "Error"],
vec![typaram, sym::Error],
None,
vec![],
PathKind::Local,
Expand All @@ -157,24 +162,24 @@ fn encodable_substructure(
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
krate: &'static str,
krate: Symbol,
) -> P<Expr> {
let encoder = substr.nonself_args[0].clone();
// throw an underscore in front to suppress unused variable warnings
let blkarg = cx.ident_of("_e", trait_span);
let blkarg = Ident::new(sym::_e, trait_span);
let blkencoder = cx.expr_ident(trait_span, blkarg);
let fn_path = cx.expr_path(cx.path_global(
trait_span,
vec![
cx.ident_of(krate, trait_span),
cx.ident_of("Encodable", trait_span),
cx.ident_of("encode", trait_span),
Ident::new(krate, trait_span),
Ident::new(sym::Encodable, trait_span),
Ident::new(sym::encode, trait_span),
],
));

match *substr.fields {
Struct(_, ref fields) => {
let emit_struct_field = cx.ident_of("emit_struct_field", trait_span);
let emit_struct_field = Ident::new(sym::emit_struct_field, trait_span);
let mut stmts = Vec::new();
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
let name = match name {
Expand Down Expand Up @@ -214,7 +219,7 @@ fn encodable_substructure(
cx.expr_method_call(
trait_span,
encoder,
cx.ident_of("emit_struct", trait_span),
Ident::new(sym::emit_struct, trait_span),
vec![
cx.expr_str(trait_span, substr.type_ident.name),
cx.expr_usize(trait_span, fields.len()),
Expand All @@ -230,7 +235,7 @@ fn encodable_substructure(
// actually exist.
let me = cx.stmt_let(trait_span, false, blkarg, encoder);
let encoder = cx.expr_ident(trait_span, blkarg);
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg", trait_span);
let emit_variant_arg = Ident::new(sym::emit_enum_variant_arg, trait_span);
let mut stmts = Vec::new();
if !fields.is_empty() {
let last = fields.len() - 1;
Expand Down Expand Up @@ -263,7 +268,7 @@ fn encodable_substructure(
let call = cx.expr_method_call(
trait_span,
blkencoder,
cx.ident_of("emit_enum_variant", trait_span),
Ident::new(sym::emit_enum_variant, trait_span),
vec![
name,
cx.expr_usize(trait_span, idx),
Expand All @@ -275,7 +280,7 @@ fn encodable_substructure(
let ret = cx.expr_method_call(
trait_span,
encoder,
cx.ident_of("emit_enum", trait_span),
Ident::new(sym::emit_enum, trait_span),
vec![cx.expr_str(trait_span, substr.type_ident.name), blk],
);
cx.expr_block(cx.block(trait_span, vec![me, cx.stmt_expr(ret)]))
Expand Down

0 comments on commit 9f00808

Please sign in to comment.