Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid the pprust infrastructure in macro expansion #45973

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std_inject;
use symbol::Symbol;
use symbol::keywords;
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::hygiene::ExpnFormat;
use tokenstream::{TokenStream, TokenTree};
use util::small_vector::SmallVector;
use visit::Visitor;
Expand Down Expand Up @@ -151,6 +152,26 @@ impl ExpansionKind {
}
}

fn macro_bang_format(path: &ast::Path) -> ExpnFormat {
// We don't want to format a path using pretty-printing,
// `format!("{}", path)`, because that tries to insert
// line-breaks and is slow.
let mut path_str = String::with_capacity(64);
for (i, segment) in path.segments.iter().enumerate() {
if i != 0 {
path_str.push_str("::");
}

if segment.identifier.name != keywords::CrateRoot.name() &&
segment.identifier.name != keywords::DollarCrate.name()
{
path_str.push_str(&segment.identifier.name.as_str())
}
}

MacroBang(Symbol::intern(&path_str))
}

pub struct Invocation {
pub kind: InvocationKind,
expansion_kind: ExpansionKind,
Expand Down Expand Up @@ -517,7 +538,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
mark.set_expn_info(ExpnInfo {
call_site: span,
callee: NameAndSpan {
format: MacroBang(Symbol::intern(&format!("{}", path))),
format: macro_bang_format(path),
span: def_site_span,
allow_internal_unstable,
allow_internal_unsafe,
Expand Down Expand Up @@ -564,7 +585,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
call_site: span,
callee: NameAndSpan {
format: MacroBang(Symbol::intern(&format!("{}", path))),
format: macro_bang_format(path),
span: tt_span,
allow_internal_unstable,
allow_internal_unsafe: false,
Expand Down Expand Up @@ -600,7 +621,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
call_site: span,
callee: NameAndSpan {
format: MacroBang(Symbol::intern(&format!("{}", path))),
format: macro_bang_format(path),
// FIXME procedural macros do not have proper span info
// yet, when they do, we should use it here.
span: None,
Expand Down