Skip to content

Remove rampant infestation of String from ExpnInfo #28033

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

Merged
merged 2 commits into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
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
43 changes: 36 additions & 7 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use std::io::{self, Read};

use serialize::{Encodable, Decodable, Encoder, Decoder};

use parse::token::intern;
use ast::Name;

// _____________________________________________________________________________
// Pos, BytePos, CharPos
Expand Down Expand Up @@ -257,21 +259,38 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
//

/// The source of expansion.
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
pub enum ExpnFormat {
/// e.g. #[derive(...)] <item>
MacroAttribute,
MacroAttribute(Name),
/// e.g. `format!()`
MacroBang,
MacroBang(Name),
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
CompilerExpansion,
CompilerExpansion(CompilerExpansionFormat),
}

#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
pub enum CompilerExpansionFormat {
IfLet,
PlacementIn,
WhileLet,
ForLoop,
Closure,
}

impl CompilerExpansionFormat {
pub fn name(self) -> &'static str {
match self {
CompilerExpansionFormat::IfLet => "if let expansion",
CompilerExpansionFormat::PlacementIn => "placement-in expansion",
CompilerExpansionFormat::WhileLet => "while let expansion",
CompilerExpansionFormat::ForLoop => "for loop expansion",
CompilerExpansionFormat::Closure => "closure expansion",
}
}
}
#[derive(Clone, Hash, Debug)]
pub struct NameAndSpan {
/// The name of the macro that was invoked to create the thing
/// with this Span.
pub name: String,
/// The format with which the macro was invoked.
pub format: ExpnFormat,
/// Whether the macro is allowed to use #[unstable]/feature-gated
Expand All @@ -284,6 +303,16 @@ pub struct NameAndSpan {
pub span: Option<Span>
}

impl NameAndSpan {
pub fn name(&self) -> Name {
match self.format {
ExpnFormat::MacroAttribute(s) => s,
ExpnFormat::MacroBang(s) => s,
ExpnFormat::CompilerExpansion(ce) => intern(ce.name()),
}
}
}

/// Extra information for tracking spans of macro and syntax sugar expansion
#[derive(Hash, Debug)]
pub struct ExpnInfo {
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ impl EmitterWriter {
let ss = ei.callee.span.map_or(String::new(),
|span| cm.span_to_string(span));
let (pre, post) = match ei.callee.format {
codemap::MacroAttribute => ("#[", "]"),
codemap::MacroBang => ("", "!"),
codemap::CompilerExpansion => ("", ""),
codemap::MacroAttribute(..) => ("#[", "]"),
codemap::MacroBang(..) => ("", "!"),
codemap::CompilerExpansion(..) => ("", ""),
};
try!(self.print_diagnostic(&ss, Note,
&format!("in expansion of {}{}{}",
pre,
ei.callee.name,
ei.callee.name(),
post),
None));
let ss = cm.span_to_string(ei.call_site);
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/ext/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use codemap::Span;
use ext::base;
use ext::base::*;
use feature_gate;
use parse::token::InternedString;
use parse::token::{intern, InternedString};
use parse::token;
use ptr::P;

Expand Down Expand Up @@ -211,8 +211,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
let expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
call_site: sp,
callee: codemap::NameAndSpan {
name: "asm".to_string(),
format: codemap::MacroBang,
format: codemap::MacroBang(intern("asm")),
span: None,
allow_internal_unstable: false,
},
Expand Down
9 changes: 5 additions & 4 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,14 @@ impl<'a> ExtCtxt<'a> {
loop {
if self.codemap().with_expn_info(expn_id, |info| {
info.map_or(None, |i| {
if i.callee.name == "include" {
if i.callee.name() == "include" {
// Stop going up the backtrace once include! is encountered
return None;
}
expn_id = i.call_site.expn_id;
if i.callee.format != CompilerExpansion {
last_macro = Some(i.call_site)
match i.callee.format {
CompilerExpansion(..) => (),
_ => last_macro = Some(i.call_site),
}
return Some(());
})
Expand All @@ -744,7 +745,7 @@ impl<'a> ExtCtxt<'a> {
if self.recursion_count > self.ecfg.recursion_limit {
panic!(self.span_fatal(ei.call_site,
&format!("recursion limit reached while expanding the macro `{}`",
ei.callee.name)));
ei.callee.name())));
}

let mut call_site = ei.call_site;
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ use codemap::Span;
use diagnostic::SpanHandler;
use fold::MoveMap;
use owned_slice::OwnedSlice;
use parse::token::InternedString;
use parse::token::{intern, InternedString};
use parse::token::special_idents;
use ptr::P;

Expand Down Expand Up @@ -1436,8 +1436,7 @@ impl<'a> TraitDef<'a> {
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
call_site: to_set,
callee: codemap::NameAndSpan {
name: format!("derive({})", trait_name),
format: codemap::MacroAttribute,
format: codemap::MacroAttribute(intern(&format!("derive({})", trait_name))),
span: Some(self.span),
allow_internal_unstable: false,
}
Expand Down
60 changes: 26 additions & 34 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use ext::build::AstBuilder;
use attr;
use attr::AttrMetaMethods;
use codemap;
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, CompilerExpansion};
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
use codemap::{CompilerExpansion, CompilerExpansionFormat};
use ext::base::*;
use feature_gate::{self, Features, GatedCfg};
use fold;
Expand All @@ -43,12 +44,12 @@ fn mk_core_path(fld: &mut MacroExpander,
}

pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
fn push_compiler_expansion(fld: &mut MacroExpander, span: Span, expansion_desc: &str) {
fn push_compiler_expansion(fld: &mut MacroExpander, span: Span,
expansion_type: CompilerExpansionFormat) {
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
name: expansion_desc.to_string(),
format: CompilerExpansion,
format: CompilerExpansion(expansion_type),

// This does *not* mean code generated after
// `push_compiler_expansion` is automatically exempt
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
&fld.cx.parse_sess.span_diagnostic,
expr_span);

push_compiler_expansion(fld, expr_span, "placement-in expansion");
push_compiler_expansion(fld, expr_span, CompilerExpansionFormat::PlacementIn);

let value_span = value_expr.span;
let placer_span = placer.span;
Expand Down Expand Up @@ -223,7 +224,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
// }
// }

push_compiler_expansion(fld, span, "while let expansion");
push_compiler_expansion(fld, span, CompilerExpansionFormat::WhileLet);

// `<pat> => <body>`
let pat_arm = {
Expand Down Expand Up @@ -262,7 +263,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
// _ => [<elseopt> | ()]
// }

push_compiler_expansion(fld, span, "if let expansion");
push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);

// `<pat> => <body>`
let pat_arm = {
Expand Down Expand Up @@ -334,7 +335,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
ast::ExprIf(cond, blk, elseopt) => {
let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
ast::ExprIfLet(..) => {
push_compiler_expansion(fld, span, "if let expansion");
push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
// wrap the if-let expr in a block
let span = els.span;
let blk = P(ast::Block {
Expand Down Expand Up @@ -378,7 +379,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
// result
// }

push_compiler_expansion(fld, span, "for loop expansion");
push_compiler_expansion(fld, span, CompilerExpansionFormat::ForLoop);

let span = fld.new_span(span);

Expand Down Expand Up @@ -458,7 +459,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
}

ast::ExprClosure(capture_clause, fn_decl, block) => {
push_compiler_expansion(fld, span, "closure expansion");
push_compiler_expansion(fld, span, CompilerExpansionFormat::Closure);
let (rewritten_fn_decl, rewritten_block)
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
let new_node = ast::ExprClosure(capture_clause,
Expand Down Expand Up @@ -542,8 +543,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
name: extname.to_string(),
format: MacroBang,
format: MacroBang(extname),
span: exp_span,
allow_internal_unstable: allow_internal_unstable,
},
Expand Down Expand Up @@ -721,8 +721,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
name: extname.to_string(),
format: MacroBang,
format: MacroBang(extname),
span: span,
allow_internal_unstable: allow_internal_unstable,
}
Expand All @@ -741,8 +740,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
name: extname.to_string(),
format: MacroBang,
format: MacroBang(extname),
span: span,
allow_internal_unstable: allow_internal_unstable,
}
Expand All @@ -762,8 +760,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
callee: NameAndSpan {
name: extname.to_string(),
format: MacroBang,
format: MacroBang(extname),
span: None,
// `macro_rules!` doesn't directly allow
// unstable (this is orthogonal to whether
Expand Down Expand Up @@ -1090,8 +1087,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
fld.cx.bt_push(ExpnInfo {
call_site: span,
callee: NameAndSpan {
name: extname.to_string(),
format: MacroBang,
format: MacroBang(extname),
span: tt_span,
allow_internal_unstable: allow_internal_unstable,
}
Expand Down Expand Up @@ -1293,17 +1289,16 @@ fn expand_decorators(a: Annotatable,
new_attrs: &mut Vec<ast::Attribute>)
{
for attr in a.attrs() {
let mname = attr.name();
match fld.cx.syntax_env.find(&intern(&mname)) {
let mname = intern(&attr.name());
match fld.cx.syntax_env.find(&mname) {
Some(rc) => match *rc {
Decorator(ref dec) => {
attr::mark_used(&attr);

fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
name: mname.to_string(),
format: MacroAttribute,
format: MacroAttribute(mname),
span: Some(attr.span),
// attributes can do whatever they like,
// for now.
Expand All @@ -1330,8 +1325,7 @@ fn expand_decorators(a: Annotatable,
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
name: mname.to_string(),
format: MacroAttribute,
format: MacroAttribute(mname),
span: Some(attr.span),
// attributes can do whatever they like,
// for now.
Expand Down Expand Up @@ -1372,17 +1366,16 @@ fn expand_item_multi_modifier(mut it: Annotatable,
}

for attr in &modifiers {
let mname = attr.name();
let mname = intern(&attr.name());

match fld.cx.syntax_env.find(&intern(&mname)) {
match fld.cx.syntax_env.find(&mname) {
Some(rc) => match *rc {
MultiModifier(ref mac) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
name: mname.to_string(),
format: MacroAttribute,
format: MacroAttribute(mname),
span: Some(attr.span),
// attributes can do whatever they like,
// for now
Expand Down Expand Up @@ -1421,17 +1414,16 @@ fn expand_item_modifiers(mut it: P<ast::Item>,
}

for attr in &modifiers {
let mname = attr.name();
let mname = intern(&attr.name());

match fld.cx.syntax_env.find(&intern(&mname)) {
match fld.cx.syntax_env.find(&mname) {
Some(rc) => match *rc {
Modifier(ref mac) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
callee: NameAndSpan {
name: mname.to_string(),
format: MacroAttribute,
format: MacroAttribute(mname),
span: Some(attr.span),
// attributes can do whatever they like,
// for now
Expand Down
6 changes: 2 additions & 4 deletions src/libsyntax/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
use codemap;
use fold::Folder;
use fold;
use parse::token::InternedString;
use parse::token::special_idents;
use parse::token::{intern, InternedString, special_idents};
use parse::{token, ParseSess};
use ptr::P;
use util::small_vector::SmallVector;
Expand All @@ -27,8 +26,7 @@ fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
let info = ExpnInfo {
call_site: DUMMY_SP,
callee: NameAndSpan {
name: "std_inject".to_string(),
format: MacroAttribute,
format: MacroAttribute(intern("std_inject")),
span: None,
allow_internal_unstable: true,
}
Expand Down
Loading