Enumify CompilerExpansion in ExpnInfo
This commit is contained in:
parent
5c630a61c6
commit
4ec7b713dd
8 changed files with 68 additions and 53 deletions
|
@ -257,21 +257,38 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
|
||||||
//
|
//
|
||||||
|
|
||||||
/// The source of expansion.
|
/// The source of expansion.
|
||||||
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
|
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
|
||||||
pub enum ExpnFormat {
|
pub enum ExpnFormat {
|
||||||
/// e.g. #[derive(...)] <item>
|
/// e.g. #[derive(...)] <item>
|
||||||
MacroAttribute,
|
MacroAttribute(String),
|
||||||
/// e.g. `format!()`
|
/// e.g. `format!()`
|
||||||
MacroBang,
|
MacroBang(String),
|
||||||
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
|
/// 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)]
|
#[derive(Clone, Hash, Debug)]
|
||||||
pub struct NameAndSpan {
|
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.
|
/// The format with which the macro was invoked.
|
||||||
pub format: ExpnFormat,
|
pub format: ExpnFormat,
|
||||||
/// Whether the macro is allowed to use #[unstable]/feature-gated
|
/// Whether the macro is allowed to use #[unstable]/feature-gated
|
||||||
|
@ -284,6 +301,16 @@ pub struct NameAndSpan {
|
||||||
pub span: Option<Span>
|
pub span: Option<Span>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl NameAndSpan {
|
||||||
|
pub fn name(&self) -> &str{
|
||||||
|
match self.format {
|
||||||
|
ExpnFormat::MacroAttribute(ref s) => &s,
|
||||||
|
ExpnFormat::MacroBang(ref s) => &s,
|
||||||
|
ExpnFormat::CompilerExpansion(ce) => ce.name(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extra information for tracking spans of macro and syntax sugar expansion
|
/// Extra information for tracking spans of macro and syntax sugar expansion
|
||||||
#[derive(Hash, Debug)]
|
#[derive(Hash, Debug)]
|
||||||
pub struct ExpnInfo {
|
pub struct ExpnInfo {
|
||||||
|
|
|
@ -733,14 +733,14 @@ impl EmitterWriter {
|
||||||
let ss = ei.callee.span.map_or(String::new(),
|
let ss = ei.callee.span.map_or(String::new(),
|
||||||
|span| cm.span_to_string(span));
|
|span| cm.span_to_string(span));
|
||||||
let (pre, post) = match ei.callee.format {
|
let (pre, post) = match ei.callee.format {
|
||||||
codemap::MacroAttribute => ("#[", "]"),
|
codemap::MacroAttribute(..) => ("#[", "]"),
|
||||||
codemap::MacroBang => ("", "!"),
|
codemap::MacroBang(..) => ("", "!"),
|
||||||
codemap::CompilerExpansion => ("", ""),
|
codemap::CompilerExpansion(..) => ("", ""),
|
||||||
};
|
};
|
||||||
try!(self.print_diagnostic(&ss, Note,
|
try!(self.print_diagnostic(&ss, Note,
|
||||||
&format!("in expansion of {}{}{}",
|
&format!("in expansion of {}{}{}",
|
||||||
pre,
|
pre,
|
||||||
ei.callee.name,
|
ei.callee.name(),
|
||||||
post),
|
post),
|
||||||
None));
|
None));
|
||||||
let ss = cm.span_to_string(ei.call_site);
|
let ss = cm.span_to_string(ei.call_site);
|
||||||
|
|
|
@ -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 {
|
let expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
|
||||||
call_site: sp,
|
call_site: sp,
|
||||||
callee: codemap::NameAndSpan {
|
callee: codemap::NameAndSpan {
|
||||||
name: "asm".to_string(),
|
format: codemap::MacroBang("asm".to_string()),
|
||||||
format: codemap::MacroBang,
|
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: false,
|
allow_internal_unstable: false,
|
||||||
},
|
},
|
||||||
|
|
|
@ -714,13 +714,14 @@ impl<'a> ExtCtxt<'a> {
|
||||||
loop {
|
loop {
|
||||||
if self.codemap().with_expn_info(expn_id, |info| {
|
if self.codemap().with_expn_info(expn_id, |info| {
|
||||||
info.map_or(None, |i| {
|
info.map_or(None, |i| {
|
||||||
if i.callee.name == "include" {
|
if i.callee.name() == "include" {
|
||||||
// Stop going up the backtrace once include! is encountered
|
// Stop going up the backtrace once include! is encountered
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
expn_id = i.call_site.expn_id;
|
expn_id = i.call_site.expn_id;
|
||||||
if i.callee.format != CompilerExpansion {
|
match i.callee.format {
|
||||||
last_macro = Some(i.call_site)
|
CompilerExpansion(..) => (),
|
||||||
|
_ => last_macro = Some(i.call_site),
|
||||||
}
|
}
|
||||||
return Some(());
|
return Some(());
|
||||||
})
|
})
|
||||||
|
@ -744,7 +745,7 @@ impl<'a> ExtCtxt<'a> {
|
||||||
if self.recursion_count > self.ecfg.recursion_limit {
|
if self.recursion_count > self.ecfg.recursion_limit {
|
||||||
panic!(self.span_fatal(ei.call_site,
|
panic!(self.span_fatal(ei.call_site,
|
||||||
&format!("recursion limit reached while expanding the macro `{}`",
|
&format!("recursion limit reached while expanding the macro `{}`",
|
||||||
ei.callee.name)));
|
ei.callee.name())));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut call_site = ei.call_site;
|
let mut call_site = ei.call_site;
|
||||||
|
|
|
@ -1436,8 +1436,7 @@ impl<'a> TraitDef<'a> {
|
||||||
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
|
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
|
||||||
call_site: to_set,
|
call_site: to_set,
|
||||||
callee: codemap::NameAndSpan {
|
callee: codemap::NameAndSpan {
|
||||||
name: format!("derive({})", trait_name),
|
format: codemap::MacroAttribute(format!("derive({})", trait_name)),
|
||||||
format: codemap::MacroAttribute,
|
|
||||||
span: Some(self.span),
|
span: Some(self.span),
|
||||||
allow_internal_unstable: false,
|
allow_internal_unstable: false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@ use ext::build::AstBuilder;
|
||||||
use attr;
|
use attr;
|
||||||
use attr::AttrMetaMethods;
|
use attr::AttrMetaMethods;
|
||||||
use codemap;
|
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 ext::base::*;
|
||||||
use feature_gate::{self, Features, GatedCfg};
|
use feature_gate::{self, Features, GatedCfg};
|
||||||
use fold;
|
use fold;
|
||||||
|
@ -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> {
|
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 {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: expansion_desc.to_string(),
|
format: CompilerExpansion(expansion_type),
|
||||||
format: CompilerExpansion,
|
|
||||||
|
|
||||||
// This does *not* mean code generated after
|
// This does *not* mean code generated after
|
||||||
// `push_compiler_expansion` is automatically exempt
|
// `push_compiler_expansion` is automatically exempt
|
||||||
|
@ -111,7 +112,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
|
||||||
&fld.cx.parse_sess.span_diagnostic,
|
&fld.cx.parse_sess.span_diagnostic,
|
||||||
expr_span);
|
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 value_span = value_expr.span;
|
||||||
let placer_span = placer.span;
|
let placer_span = placer.span;
|
||||||
|
@ -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>`
|
// `<pat> => <body>`
|
||||||
let pat_arm = {
|
let pat_arm = {
|
||||||
|
@ -262,7 +263,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
|
||||||
// _ => [<elseopt> | ()]
|
// _ => [<elseopt> | ()]
|
||||||
// }
|
// }
|
||||||
|
|
||||||
push_compiler_expansion(fld, span, "if let expansion");
|
push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
|
||||||
|
|
||||||
// `<pat> => <body>`
|
// `<pat> => <body>`
|
||||||
let pat_arm = {
|
let pat_arm = {
|
||||||
|
@ -334,7 +335,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
|
||||||
ast::ExprIf(cond, blk, elseopt) => {
|
ast::ExprIf(cond, blk, elseopt) => {
|
||||||
let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
|
let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
|
||||||
ast::ExprIfLet(..) => {
|
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
|
// wrap the if-let expr in a block
|
||||||
let span = els.span;
|
let span = els.span;
|
||||||
let blk = P(ast::Block {
|
let blk = P(ast::Block {
|
||||||
|
@ -378,7 +379,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
|
||||||
// result
|
// result
|
||||||
// }
|
// }
|
||||||
|
|
||||||
push_compiler_expansion(fld, span, "for loop expansion");
|
push_compiler_expansion(fld, span, CompilerExpansionFormat::ForLoop);
|
||||||
|
|
||||||
let span = fld.new_span(span);
|
let span = fld.new_span(span);
|
||||||
|
|
||||||
|
@ -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) => {
|
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)
|
let (rewritten_fn_decl, rewritten_block)
|
||||||
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
|
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
|
||||||
let new_node = ast::ExprClosure(capture_clause,
|
let new_node = ast::ExprClosure(capture_clause,
|
||||||
|
@ -542,8 +543,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: extname.to_string(),
|
format: MacroBang(extname.to_string()),
|
||||||
format: MacroBang,
|
|
||||||
span: exp_span,
|
span: exp_span,
|
||||||
allow_internal_unstable: allow_internal_unstable,
|
allow_internal_unstable: allow_internal_unstable,
|
||||||
},
|
},
|
||||||
|
@ -721,8 +721,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: it.span,
|
call_site: it.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: extname.to_string(),
|
format: MacroBang(extname.to_string()),
|
||||||
format: MacroBang,
|
|
||||||
span: span,
|
span: span,
|
||||||
allow_internal_unstable: allow_internal_unstable,
|
allow_internal_unstable: allow_internal_unstable,
|
||||||
}
|
}
|
||||||
|
@ -741,8 +740,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: it.span,
|
call_site: it.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: extname.to_string(),
|
format: MacroBang(extname.to_string()),
|
||||||
format: MacroBang,
|
|
||||||
span: span,
|
span: span,
|
||||||
allow_internal_unstable: allow_internal_unstable,
|
allow_internal_unstable: allow_internal_unstable,
|
||||||
}
|
}
|
||||||
|
@ -762,8 +760,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: it.span,
|
call_site: it.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: extname.to_string(),
|
format: MacroBang(extname.to_string()),
|
||||||
format: MacroBang,
|
|
||||||
span: None,
|
span: None,
|
||||||
// `macro_rules!` doesn't directly allow
|
// `macro_rules!` doesn't directly allow
|
||||||
// unstable (this is orthogonal to whether
|
// unstable (this is orthogonal to whether
|
||||||
|
@ -1090,8 +1087,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: span,
|
call_site: span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: extname.to_string(),
|
format: MacroBang(extname.to_string()),
|
||||||
format: MacroBang,
|
|
||||||
span: tt_span,
|
span: tt_span,
|
||||||
allow_internal_unstable: allow_internal_unstable,
|
allow_internal_unstable: allow_internal_unstable,
|
||||||
}
|
}
|
||||||
|
@ -1302,8 +1298,7 @@ fn expand_decorators(a: Annotatable,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: mname.to_string(),
|
format: MacroAttribute(mname.to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: Some(attr.span),
|
span: Some(attr.span),
|
||||||
// attributes can do whatever they like,
|
// attributes can do whatever they like,
|
||||||
// for now.
|
// for now.
|
||||||
|
@ -1330,8 +1325,7 @@ fn expand_decorators(a: Annotatable,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: mname.to_string(),
|
format: MacroAttribute(mname.to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: Some(attr.span),
|
span: Some(attr.span),
|
||||||
// attributes can do whatever they like,
|
// attributes can do whatever they like,
|
||||||
// for now.
|
// for now.
|
||||||
|
@ -1381,8 +1375,7 @@ fn expand_item_multi_modifier(mut it: Annotatable,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: mname.to_string(),
|
format: MacroAttribute(mname.to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: Some(attr.span),
|
span: Some(attr.span),
|
||||||
// attributes can do whatever they like,
|
// attributes can do whatever they like,
|
||||||
// for now
|
// for now
|
||||||
|
@ -1430,8 +1423,7 @@ fn expand_item_modifiers(mut it: P<ast::Item>,
|
||||||
fld.cx.bt_push(ExpnInfo {
|
fld.cx.bt_push(ExpnInfo {
|
||||||
call_site: attr.span,
|
call_site: attr.span,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: mname.to_string(),
|
format: MacroAttribute(mname.to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: Some(attr.span),
|
span: Some(attr.span),
|
||||||
// attributes can do whatever they like,
|
// attributes can do whatever they like,
|
||||||
// for now
|
// for now
|
||||||
|
|
|
@ -27,8 +27,7 @@ fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
|
||||||
let info = ExpnInfo {
|
let info = ExpnInfo {
|
||||||
call_site: DUMMY_SP,
|
call_site: DUMMY_SP,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: "std_inject".to_string(),
|
format: MacroAttribute("std_inject".to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: true,
|
allow_internal_unstable: true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -265,8 +265,7 @@ fn generate_test_harness(sess: &ParseSess,
|
||||||
cx.ext_cx.bt_push(ExpnInfo {
|
cx.ext_cx.bt_push(ExpnInfo {
|
||||||
call_site: DUMMY_SP,
|
call_site: DUMMY_SP,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: "test".to_string(),
|
format: MacroAttribute("test".to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: false,
|
allow_internal_unstable: false,
|
||||||
}
|
}
|
||||||
|
@ -298,8 +297,7 @@ fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
|
||||||
let info = ExpnInfo {
|
let info = ExpnInfo {
|
||||||
call_site: DUMMY_SP,
|
call_site: DUMMY_SP,
|
||||||
callee: NameAndSpan {
|
callee: NameAndSpan {
|
||||||
name: "test".to_string(),
|
format: MacroAttribute("test".to_string()),
|
||||||
format: MacroAttribute,
|
|
||||||
span: None,
|
span: None,
|
||||||
allow_internal_unstable: true,
|
allow_internal_unstable: true,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue