mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 11:44:28 +00:00
Avoid using pretty printer if possible
Setting a pretty printer adds noticeable overhead. We can replace the usage in `ast:Lifetime::rewrite` by simply converting `Ident` to string. We can do the same thing for a macro path as long as it is not nested, which should hold for most cases.
This commit is contained in:
parent
d7d9850c42
commit
61c6c591e4
@ -114,6 +114,20 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Rewrite macro name without using pretty-printer if possible.
|
||||
fn rewrite_macro_name(path: &ast::Path, extra_ident: Option<ast::Ident>) -> String {
|
||||
let name = if path.segments.len() == 1 {
|
||||
// Avoid using pretty-printer in the common case.
|
||||
format!("{}!", path.segments[0].identifier)
|
||||
} else {
|
||||
format!("{}!", path)
|
||||
};
|
||||
match extra_ident {
|
||||
Some(ident) if ident != symbol::keywords::Invalid.ident() => format!("{} {}", name, ident),
|
||||
_ => name,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rewrite_macro(
|
||||
mac: &ast::Mac,
|
||||
extra_ident: Option<ast::Ident>,
|
||||
@ -132,16 +146,7 @@ pub fn rewrite_macro(
|
||||
|
||||
let original_style = macro_style(mac, context);
|
||||
|
||||
let macro_name = match extra_ident {
|
||||
None => format!("{}!", mac.node.path),
|
||||
Some(ident) => {
|
||||
if ident == symbol::keywords::Invalid.ident() {
|
||||
format!("{}!", mac.node.path)
|
||||
} else {
|
||||
format!("{}! {}", mac.node.path, ident)
|
||||
}
|
||||
}
|
||||
};
|
||||
let macro_name = rewrite_macro_name(&mac.node.path, extra_ident);
|
||||
|
||||
let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
|
||||
MacroStyle::Brackets
|
||||
|
@ -14,7 +14,6 @@ use std::ops::Deref;
|
||||
use config::lists::*;
|
||||
use syntax::ast::{self, FunctionRetTy, Mutability};
|
||||
use syntax::codemap::{self, BytePos, Span};
|
||||
use syntax::print::pprust;
|
||||
use syntax::symbol::keywords;
|
||||
|
||||
use codemap::SpanUtils;
|
||||
@ -539,7 +538,7 @@ impl Rewrite for ast::TyParamBound {
|
||||
|
||||
impl Rewrite for ast::Lifetime {
|
||||
fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
|
||||
Some(pprust::lifetime_to_string(self))
|
||||
Some(self.ident.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user