Skip to content

Commit 3ef9336

Browse files
committed
syntax: print expansion info from #[attribute] macros in the correct
format. Previously, any attempt to use this information from inside something like #[deriving(Foo)] would result in it printing like `deriving(Foo)!`.
1 parent 09a8794 commit 3ef9336

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/librustc/front/test.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::vec;
1818
use syntax::ast_util::*;
1919
use syntax::attr::AttrMetaMethods;
2020
use syntax::attr;
21-
use syntax::codemap::{dummy_sp, Span, ExpnInfo, NameAndSpan};
21+
use syntax::codemap::{dummy_sp, Span, ExpnInfo, NameAndSpan, MacroAttribute};
2222
use syntax::codemap;
2323
use syntax::ext::base::ExtCtxt;
2424
use syntax::fold::ast_fold;
@@ -158,6 +158,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate)
158158
call_site: dummy_sp(),
159159
callee: NameAndSpan {
160160
name: @"test",
161+
format: MacroAttribute,
161162
span: None
162163
}
163164
});

src/libsyntax/codemap.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,22 @@ pub struct LocWithOpt {
161161
// used to be structural records. Better names, anyone?
162162
pub struct FileMapAndLine {fm: @FileMap, line: uint}
163163
pub struct FileMapAndBytePos {fm: @FileMap, pos: BytePos}
164+
164165
#[deriving(IterBytes)]
165-
pub struct NameAndSpan {name: @str, span: Option<Span>}
166+
pub enum MacroFormat {
167+
// e.g. #[deriving(...)] <item>
168+
MacroAttribute,
169+
// e.g. `format!()`
170+
MacroBang
171+
}
172+
173+
#[deriving(IterBytes)]
174+
pub struct NameAndSpan {
175+
name: @str,
176+
// the format with which the macro was invoked.
177+
format: MacroFormat,
178+
span: Option<Span>
179+
}
166180

167181
/// Extra information for tracking macro expansion of spans
168182
#[deriving(IterBytes)]

src/libsyntax/diagnostic.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,13 @@ fn highlight_lines(cm: @codemap::CodeMap,
342342
fn print_macro_backtrace(cm: @codemap::CodeMap, sp: Span) {
343343
for ei in sp.expn_info.iter() {
344344
let ss = ei.callee.span.as_ref().map_default(~"", |span| cm.span_to_str(*span));
345+
let (pre, post) = match ei.callee.format {
346+
codemap::MacroAttribute => ("#[", "]"),
347+
codemap::MacroBang => ("", "!")
348+
};
349+
345350
print_diagnostic(ss, note,
346-
format!("in expansion of {}!", ei.callee.name));
351+
format!("in expansion of {}{}{}", pre, ei.callee.name, post));
347352
let ss = cm.span_to_str(ei.call_site);
348353
print_diagnostic(ss, note, "expansion site");
349354
print_macro_backtrace(cm, ei.call_site);

src/libsyntax/ext/expand.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ext::build::AstBuilder;
1818
use attr;
1919
use attr::AttrMetaMethods;
2020
use codemap;
21-
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan};
21+
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
2222
use ext::base::*;
2323
use fold::*;
2424
use opt_vec;
@@ -69,6 +69,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
6969
call_site: e.span,
7070
callee: NameAndSpan {
7171
name: extnamestr,
72+
format: MacroBang,
7273
span: exp_span,
7374
},
7475
});
@@ -257,6 +258,7 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv,
257258
call_site: attr.span,
258259
callee: NameAndSpan {
259260
name: mname,
261+
format: MacroAttribute,
260262
span: None
261263
}
262264
});
@@ -352,6 +354,7 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv,
352354
call_site: it.span,
353355
callee: NameAndSpan {
354356
name: extnamestr,
357+
format: MacroBang,
355358
span: span
356359
}
357360
});
@@ -370,6 +373,7 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv,
370373
call_site: it.span,
371374
callee: NameAndSpan {
372375
name: extnamestr,
376+
format: MacroBang,
373377
span: span
374378
}
375379
});
@@ -459,6 +463,7 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv,
459463
call_site: s.span,
460464
callee: NameAndSpan {
461465
name: extnamestr,
466+
format: MacroBang,
462467
span: exp_span,
463468
}
464469
});

0 commit comments

Comments
 (0)