Skip to content

Commit 4ec7b71

Browse files
committed
Enumify CompilerExpansion in ExpnInfo
1 parent 5c630a6 commit 4ec7b71

File tree

8 files changed

+68
-53
lines changed

8 files changed

+68
-53
lines changed

src/libsyntax/codemap.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,38 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
257257
//
258258

259259
/// The source of expansion.
260-
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
260+
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
261261
pub enum ExpnFormat {
262262
/// e.g. #[derive(...)] <item>
263-
MacroAttribute,
263+
MacroAttribute(String),
264264
/// e.g. `format!()`
265-
MacroBang,
265+
MacroBang(String),
266266
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
267-
CompilerExpansion,
267+
CompilerExpansion(CompilerExpansionFormat),
268268
}
269269

270+
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
271+
pub enum CompilerExpansionFormat {
272+
IfLet,
273+
PlacementIn,
274+
WhileLet,
275+
ForLoop,
276+
Closure,
277+
}
278+
279+
impl CompilerExpansionFormat {
280+
pub fn name(self) -> &'static str {
281+
match self {
282+
CompilerExpansionFormat::IfLet => "if let expansion",
283+
CompilerExpansionFormat::PlacementIn => "placement-in expansion",
284+
CompilerExpansionFormat::WhileLet => "while let expansion",
285+
CompilerExpansionFormat::ForLoop => "for loop expansion",
286+
CompilerExpansionFormat::Closure => "closure expansion",
287+
}
288+
}
289+
}
270290
#[derive(Clone, Hash, Debug)]
271291
pub struct NameAndSpan {
272-
/// The name of the macro that was invoked to create the thing
273-
/// with this Span.
274-
pub name: String,
275292
/// The format with which the macro was invoked.
276293
pub format: ExpnFormat,
277294
/// Whether the macro is allowed to use #[unstable]/feature-gated
@@ -284,6 +301,16 @@ pub struct NameAndSpan {
284301
pub span: Option<Span>
285302
}
286303

304+
impl NameAndSpan {
305+
pub fn name(&self) -> &str{
306+
match self.format {
307+
ExpnFormat::MacroAttribute(ref s) => &s,
308+
ExpnFormat::MacroBang(ref s) => &s,
309+
ExpnFormat::CompilerExpansion(ce) => ce.name(),
310+
}
311+
}
312+
}
313+
287314
/// Extra information for tracking spans of macro and syntax sugar expansion
288315
#[derive(Hash, Debug)]
289316
pub struct ExpnInfo {

src/libsyntax/diagnostic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -733,14 +733,14 @@ impl EmitterWriter {
733733
let ss = ei.callee.span.map_or(String::new(),
734734
|span| cm.span_to_string(span));
735735
let (pre, post) = match ei.callee.format {
736-
codemap::MacroAttribute => ("#[", "]"),
737-
codemap::MacroBang => ("", "!"),
738-
codemap::CompilerExpansion => ("", ""),
736+
codemap::MacroAttribute(..) => ("#[", "]"),
737+
codemap::MacroBang(..) => ("", "!"),
738+
codemap::CompilerExpansion(..) => ("", ""),
739739
};
740740
try!(self.print_diagnostic(&ss, Note,
741741
&format!("in expansion of {}{}{}",
742742
pre,
743-
ei.callee.name,
743+
ei.callee.name(),
744744
post),
745745
None));
746746
let ss = cm.span_to_string(ei.call_site);

src/libsyntax/ext/asm.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
211211
let expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
212212
call_site: sp,
213213
callee: codemap::NameAndSpan {
214-
name: "asm".to_string(),
215-
format: codemap::MacroBang,
214+
format: codemap::MacroBang("asm".to_string()),
216215
span: None,
217216
allow_internal_unstable: false,
218217
},

src/libsyntax/ext/base.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,14 @@ impl<'a> ExtCtxt<'a> {
714714
loop {
715715
if self.codemap().with_expn_info(expn_id, |info| {
716716
info.map_or(None, |i| {
717-
if i.callee.name == "include" {
717+
if i.callee.name() == "include" {
718718
// Stop going up the backtrace once include! is encountered
719719
return None;
720720
}
721721
expn_id = i.call_site.expn_id;
722-
if i.callee.format != CompilerExpansion {
723-
last_macro = Some(i.call_site)
722+
match i.callee.format {
723+
CompilerExpansion(..) => (),
724+
_ => last_macro = Some(i.call_site),
724725
}
725726
return Some(());
726727
})
@@ -744,7 +745,7 @@ impl<'a> ExtCtxt<'a> {
744745
if self.recursion_count > self.ecfg.recursion_limit {
745746
panic!(self.span_fatal(ei.call_site,
746747
&format!("recursion limit reached while expanding the macro `{}`",
747-
ei.callee.name)));
748+
ei.callee.name())));
748749
}
749750

750751
let mut call_site = ei.call_site;

src/libsyntax/ext/deriving/generic/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,7 @@ impl<'a> TraitDef<'a> {
14361436
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
14371437
call_site: to_set,
14381438
callee: codemap::NameAndSpan {
1439-
name: format!("derive({})", trait_name),
1440-
format: codemap::MacroAttribute,
1439+
format: codemap::MacroAttribute(format!("derive({})", trait_name)),
14411440
span: Some(self.span),
14421441
allow_internal_unstable: false,
14431442
}

src/libsyntax/ext/expand.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use ext::build::AstBuilder;
1919
use attr;
2020
use attr::AttrMetaMethods;
2121
use codemap;
22-
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, CompilerExpansion};
22+
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
23+
use codemap::{CompilerExpansion, CompilerExpansionFormat};
2324
use ext::base::*;
2425
use feature_gate::{self, Features, GatedCfg};
2526
use fold;
@@ -43,12 +44,12 @@ fn mk_core_path(fld: &mut MacroExpander,
4344
}
4445

4546
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
46-
fn push_compiler_expansion(fld: &mut MacroExpander, span: Span, expansion_desc: &str) {
47+
fn push_compiler_expansion(fld: &mut MacroExpander, span: Span,
48+
expansion_type: CompilerExpansionFormat) {
4749
fld.cx.bt_push(ExpnInfo {
4850
call_site: span,
4951
callee: NameAndSpan {
50-
name: expansion_desc.to_string(),
51-
format: CompilerExpansion,
52+
format: CompilerExpansion(expansion_type),
5253

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

114-
push_compiler_expansion(fld, expr_span, "placement-in expansion");
115+
push_compiler_expansion(fld, expr_span, CompilerExpansionFormat::PlacementIn);
115116

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

226-
push_compiler_expansion(fld, span, "while let expansion");
227+
push_compiler_expansion(fld, span, CompilerExpansionFormat::WhileLet);
227228

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

265-
push_compiler_expansion(fld, span, "if let expansion");
266+
push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
266267

267268
// `<pat> => <body>`
268269
let pat_arm = {
@@ -334,7 +335,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
334335
ast::ExprIf(cond, blk, elseopt) => {
335336
let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
336337
ast::ExprIfLet(..) => {
337-
push_compiler_expansion(fld, span, "if let expansion");
338+
push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
338339
// wrap the if-let expr in a block
339340
let span = els.span;
340341
let blk = P(ast::Block {
@@ -378,7 +379,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
378379
// result
379380
// }
380381

381-
push_compiler_expansion(fld, span, "for loop expansion");
382+
push_compiler_expansion(fld, span, CompilerExpansionFormat::ForLoop);
382383

383384
let span = fld.new_span(span);
384385

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

460461
ast::ExprClosure(capture_clause, fn_decl, block) => {
461-
push_compiler_expansion(fld, span, "closure expansion");
462+
push_compiler_expansion(fld, span, CompilerExpansionFormat::Closure);
462463
let (rewritten_fn_decl, rewritten_block)
463464
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
464465
let new_node = ast::ExprClosure(capture_clause,
@@ -542,8 +543,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
542543
fld.cx.bt_push(ExpnInfo {
543544
call_site: span,
544545
callee: NameAndSpan {
545-
name: extname.to_string(),
546-
format: MacroBang,
546+
format: MacroBang(extname.to_string()),
547547
span: exp_span,
548548
allow_internal_unstable: allow_internal_unstable,
549549
},
@@ -721,8 +721,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
721721
fld.cx.bt_push(ExpnInfo {
722722
call_site: it.span,
723723
callee: NameAndSpan {
724-
name: extname.to_string(),
725-
format: MacroBang,
724+
format: MacroBang(extname.to_string()),
726725
span: span,
727726
allow_internal_unstable: allow_internal_unstable,
728727
}
@@ -741,8 +740,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
741740
fld.cx.bt_push(ExpnInfo {
742741
call_site: it.span,
743742
callee: NameAndSpan {
744-
name: extname.to_string(),
745-
format: MacroBang,
743+
format: MacroBang(extname.to_string()),
746744
span: span,
747745
allow_internal_unstable: allow_internal_unstable,
748746
}
@@ -762,8 +760,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
762760
fld.cx.bt_push(ExpnInfo {
763761
call_site: it.span,
764762
callee: NameAndSpan {
765-
name: extname.to_string(),
766-
format: MacroBang,
763+
format: MacroBang(extname.to_string()),
767764
span: None,
768765
// `macro_rules!` doesn't directly allow
769766
// unstable (this is orthogonal to whether
@@ -1090,8 +1087,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
10901087
fld.cx.bt_push(ExpnInfo {
10911088
call_site: span,
10921089
callee: NameAndSpan {
1093-
name: extname.to_string(),
1094-
format: MacroBang,
1090+
format: MacroBang(extname.to_string()),
10951091
span: tt_span,
10961092
allow_internal_unstable: allow_internal_unstable,
10971093
}
@@ -1302,8 +1298,7 @@ fn expand_decorators(a: Annotatable,
13021298
fld.cx.bt_push(ExpnInfo {
13031299
call_site: attr.span,
13041300
callee: NameAndSpan {
1305-
name: mname.to_string(),
1306-
format: MacroAttribute,
1301+
format: MacroAttribute(mname.to_string()),
13071302
span: Some(attr.span),
13081303
// attributes can do whatever they like,
13091304
// for now.
@@ -1330,8 +1325,7 @@ fn expand_decorators(a: Annotatable,
13301325
fld.cx.bt_push(ExpnInfo {
13311326
call_site: attr.span,
13321327
callee: NameAndSpan {
1333-
name: mname.to_string(),
1334-
format: MacroAttribute,
1328+
format: MacroAttribute(mname.to_string()),
13351329
span: Some(attr.span),
13361330
// attributes can do whatever they like,
13371331
// for now.
@@ -1381,8 +1375,7 @@ fn expand_item_multi_modifier(mut it: Annotatable,
13811375
fld.cx.bt_push(ExpnInfo {
13821376
call_site: attr.span,
13831377
callee: NameAndSpan {
1384-
name: mname.to_string(),
1385-
format: MacroAttribute,
1378+
format: MacroAttribute(mname.to_string()),
13861379
span: Some(attr.span),
13871380
// attributes can do whatever they like,
13881381
// for now
@@ -1430,8 +1423,7 @@ fn expand_item_modifiers(mut it: P<ast::Item>,
14301423
fld.cx.bt_push(ExpnInfo {
14311424
call_site: attr.span,
14321425
callee: NameAndSpan {
1433-
name: mname.to_string(),
1434-
format: MacroAttribute,
1426+
format: MacroAttribute(mname.to_string()),
14351427
span: Some(attr.span),
14361428
// attributes can do whatever they like,
14371429
// for now

src/libsyntax/std_inject.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
2727
let info = ExpnInfo {
2828
call_site: DUMMY_SP,
2929
callee: NameAndSpan {
30-
name: "std_inject".to_string(),
31-
format: MacroAttribute,
30+
format: MacroAttribute("std_inject".to_string()),
3231
span: None,
3332
allow_internal_unstable: true,
3433
}

src/libsyntax/test.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ fn generate_test_harness(sess: &ParseSess,
265265
cx.ext_cx.bt_push(ExpnInfo {
266266
call_site: DUMMY_SP,
267267
callee: NameAndSpan {
268-
name: "test".to_string(),
269-
format: MacroAttribute,
268+
format: MacroAttribute("test".to_string()),
270269
span: None,
271270
allow_internal_unstable: false,
272271
}
@@ -298,8 +297,7 @@ fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
298297
let info = ExpnInfo {
299298
call_site: DUMMY_SP,
300299
callee: NameAndSpan {
301-
name: "test".to_string(),
302-
format: MacroAttribute,
300+
format: MacroAttribute("test".to_string()),
303301
span: None,
304302
allow_internal_unstable: true,
305303
}

0 commit comments

Comments
 (0)