Avoid some unnecessary &str to Ident conversions

This commit is contained in:
Matthew Jasper 2019-09-14 21:14:03 +01:00
parent e8d2f62924
commit 57a45e9cbd
5 changed files with 17 additions and 18 deletions

View File

@ -444,7 +444,8 @@ pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
LoadedMacro::MacroDef(ast::Item {
ident: ast::Ident::from_str(&name.as_str()),
// FIXME: cross-crate hygiene
ident: ast::Ident::with_dummy_span(name.as_symbol()),
id: ast::DUMMY_NODE_ID,
span: local_span,
attrs: attrs.iter().cloned().collect(),

View File

@ -363,7 +363,7 @@ pub fn expr_field_access(
self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
}
pub fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
let ident = Ident::new(sym::integer(idx), sp);
self.expr(sp, ast::ExprKind::Field(expr, ident))
}
pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {

View File

@ -237,7 +237,7 @@ pub struct MethodDef<'a> {
/// Whether there is a self argument (outer Option) i.e., whether
/// this is a static function, and whether it is a pointer (inner
/// Option)
pub explicit_self: Option<Option<PtrTy<'a>>>,
pub explicit_self: Option<Option<PtrTy>>,
/// Arguments other than the self argument
pub args: Vec<(Ty<'a>, &'a str)>,

View File

@ -13,9 +13,9 @@
/// The types of pointers
#[derive(Clone)]
pub enum PtrTy<'a> {
pub enum PtrTy {
/// &'lifetime mut
Borrowed(Option<&'a str>, ast::Mutability),
Borrowed(Option<Ident>, ast::Mutability),
/// *mut
#[allow(dead_code)]
Raw(ast::Mutability),
@ -26,7 +26,7 @@ pub enum PtrTy<'a> {
#[derive(Clone)]
pub struct Path<'a> {
path: Vec<&'a str>,
lifetime: Option<&'a str>,
lifetime: Option<Ident>,
params: Vec<Box<Ty<'a>>>,
kind: PathKind,
}
@ -46,7 +46,7 @@ pub fn new_local(path: &str) -> Path<'_> {
Path::new_(vec![path], None, Vec::new(), PathKind::Local)
}
pub fn new_<'r>(path: Vec<&'r str>,
lifetime: Option<&'r str>,
lifetime: Option<Ident>,
params: Vec<Box<Ty<'r>>>,
kind: PathKind)
-> Path<'r> {
@ -99,7 +99,7 @@ pub fn to_path(&self,
pub enum Ty<'a> {
Self_,
/// &/Box/ Ty
Ptr(Box<Ty<'a>>, PtrTy<'a>),
Ptr(Box<Ty<'a>>, PtrTy),
/// mod::mod::Type<[lifetime], [Params...]>, including a plain type
/// parameter, and things like `i32`
Literal(Path<'a>),
@ -107,14 +107,14 @@ pub enum Ty<'a> {
Tuple(Vec<Ty<'a>>),
}
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
pub fn borrowed_ptrty() -> PtrTy {
Borrowed(None, ast::Mutability::Immutable)
}
pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
Ptr(ty, borrowed_ptrty())
}
pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
Some(Some(borrowed_ptrty()))
}
@ -126,13 +126,11 @@ pub fn nil_ty<'r>() -> Ty<'r> {
Tuple(Vec::new())
}
fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
lt.map(|s|
cx.lifetime(span, Ident::from_str(s))
)
fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Option<ast::Lifetime> {
lt.map(|ident| cx.lifetime(span, ident))
}
fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Vec<ast::Lifetime> {
mk_lifetime(cx, span, lt).into_iter().collect()
}
@ -265,7 +263,7 @@ pub fn to_generics(&self,
pub fn get_explicit_self(cx: &ExtCtxt<'_>,
span: Span,
self_ptr: &Option<PtrTy<'_>>)
self_ptr: &Option<PtrTy>)
-> (P<Expr>, ast::ExplicitSelf) {
// this constructs a fresh `self` path
let self_path = cx.expr_self(span);
@ -276,7 +274,7 @@ pub fn get_explicit_self(cx: &ExtCtxt<'_>,
respan(span,
match *ptr {
Borrowed(ref lt, mutbl) => {
let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
let lt = lt.map(|s| cx.lifetime(span, s));
SelfKind::Region(lt, mutbl)
}
Raw(_) => {

View File

@ -288,7 +288,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
// Honor the reexport_test_harness_main attribute
let main_id = match cx.reexport_test_harness_main {
Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
None => Ident::from_str_and_span("main", sp),
None => Ident::new(sym::main, sp),
};
let main = P(ast::Item {