Skip to content

Commit 0833d87

Browse files
committed
Rollup merge of rust-lang#28033 - Manishearth:compilerexpn, r=eddyb
We were using them for every expansion, instead of using `Name`. Also converted `CompilerExpansion` into an enum so its nicer to use and takes up less space. Will profile later, but this should be a small improvement in memory usage. r? @eddyb
2 parents aebe352 + 25cbb43 commit 0833d87

File tree

11 files changed

+84
-70
lines changed

11 files changed

+84
-70
lines changed

src/libsyntax/codemap.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use std::io::{self, Read};
2929

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

32+
use parse::token::intern;
33+
use ast::Name;
3234

3335
// _____________________________________________________________________________
3436
// Pos, BytePos, CharPos
@@ -257,21 +259,38 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
257259
//
258260

259261
/// The source of expansion.
260-
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
262+
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
261263
pub enum ExpnFormat {
262264
/// e.g. #[derive(...)] <item>
263-
MacroAttribute,
265+
MacroAttribute(Name),
264266
/// e.g. `format!()`
265-
MacroBang,
267+
MacroBang(Name),
266268
/// Syntax sugar expansion performed by the compiler (libsyntax::expand).
267-
CompilerExpansion,
269+
CompilerExpansion(CompilerExpansionFormat),
270+
}
271+
272+
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
273+
pub enum CompilerExpansionFormat {
274+
IfLet,
275+
PlacementIn,
276+
WhileLet,
277+
ForLoop,
278+
Closure,
268279
}
269280

281+
impl CompilerExpansionFormat {
282+
pub fn name(self) -> &'static str {
283+
match self {
284+
CompilerExpansionFormat::IfLet => "if let expansion",
285+
CompilerExpansionFormat::PlacementIn => "placement-in expansion",
286+
CompilerExpansionFormat::WhileLet => "while let expansion",
287+
CompilerExpansionFormat::ForLoop => "for loop expansion",
288+
CompilerExpansionFormat::Closure => "closure expansion",
289+
}
290+
}
291+
}
270292
#[derive(Clone, Hash, Debug)]
271293
pub struct NameAndSpan {
272-
/// The name of the macro that was invoked to create the thing
273-
/// with this Span.
274-
pub name: String,
275294
/// The format with which the macro was invoked.
276295
pub format: ExpnFormat,
277296
/// Whether the macro is allowed to use #[unstable]/feature-gated
@@ -284,6 +303,16 @@ pub struct NameAndSpan {
284303
pub span: Option<Span>
285304
}
286305

306+
impl NameAndSpan {
307+
pub fn name(&self) -> Name {
308+
match self.format {
309+
ExpnFormat::MacroAttribute(s) => s,
310+
ExpnFormat::MacroBang(s) => s,
311+
ExpnFormat::CompilerExpansion(ce) => intern(ce.name()),
312+
}
313+
}
314+
}
315+
287316
/// Extra information for tracking spans of macro and syntax sugar expansion
288317
#[derive(Hash, Debug)]
289318
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use codemap::Span;
1919
use ext::base;
2020
use ext::base::*;
2121
use feature_gate;
22-
use parse::token::InternedString;
22+
use parse::token::{intern, InternedString};
2323
use parse::token;
2424
use ptr::P;
2525

@@ -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(intern("asm")),
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ use codemap::Span;
205205
use diagnostic::SpanHandler;
206206
use fold::MoveMap;
207207
use owned_slice::OwnedSlice;
208-
use parse::token::InternedString;
208+
use parse::token::{intern, InternedString};
209209
use parse::token::special_idents;
210210
use ptr::P;
211211

@@ -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(intern(&format!("derive({})", trait_name))),
14411440
span: Some(self.span),
14421441
allow_internal_unstable: false,
14431442
}

src/libsyntax/ext/expand.rs

+26-34
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),
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),
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),
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),
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),
10951091
span: tt_span,
10961092
allow_internal_unstable: allow_internal_unstable,
10971093
}
@@ -1293,17 +1289,16 @@ fn expand_decorators(a: Annotatable,
12931289
new_attrs: &mut Vec<ast::Attribute>)
12941290
{
12951291
for attr in a.attrs() {
1296-
let mname = attr.name();
1297-
match fld.cx.syntax_env.find(&intern(&mname)) {
1292+
let mname = intern(&attr.name());
1293+
match fld.cx.syntax_env.find(&mname) {
12981294
Some(rc) => match *rc {
12991295
Decorator(ref dec) => {
13001296
attr::mark_used(&attr);
13011297

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),
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),
13351329
span: Some(attr.span),
13361330
// attributes can do whatever they like,
13371331
// for now.
@@ -1372,17 +1366,16 @@ fn expand_item_multi_modifier(mut it: Annotatable,
13721366
}
13731367

13741368
for attr in &modifiers {
1375-
let mname = attr.name();
1369+
let mname = intern(&attr.name());
13761370

1377-
match fld.cx.syntax_env.find(&intern(&mname)) {
1371+
match fld.cx.syntax_env.find(&mname) {
13781372
Some(rc) => match *rc {
13791373
MultiModifier(ref mac) => {
13801374
attr::mark_used(attr);
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),
13861379
span: Some(attr.span),
13871380
// attributes can do whatever they like,
13881381
// for now
@@ -1421,17 +1414,16 @@ fn expand_item_modifiers(mut it: P<ast::Item>,
14211414
}
14221415

14231416
for attr in &modifiers {
1424-
let mname = attr.name();
1417+
let mname = intern(&attr.name());
14251418

1426-
match fld.cx.syntax_env.find(&intern(&mname)) {
1419+
match fld.cx.syntax_env.find(&mname) {
14271420
Some(rc) => match *rc {
14281421
Modifier(ref mac) => {
14291422
attr::mark_used(attr);
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),
14351427
span: Some(attr.span),
14361428
// attributes can do whatever they like,
14371429
// for now

src/libsyntax/std_inject.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
1414
use codemap;
1515
use fold::Folder;
1616
use fold;
17-
use parse::token::InternedString;
18-
use parse::token::special_idents;
17+
use parse::token::{intern, InternedString, special_idents};
1918
use parse::{token, ParseSess};
2019
use ptr::P;
2120
use util::small_vector::SmallVector;
@@ -27,8 +26,7 @@ fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
2726
let info = ExpnInfo {
2827
call_site: DUMMY_SP,
2928
callee: NameAndSpan {
30-
name: "std_inject".to_string(),
31-
format: MacroAttribute,
29+
format: MacroAttribute(intern("std_inject")),
3230
span: None,
3331
allow_internal_unstable: true,
3432
}

0 commit comments

Comments
 (0)