Skip to content

Commit 4ce84fa

Browse files
committed
auto merge of #11720 : sfackler/rust/macro-export-source, r=alexcrichton
The old method of serializing the AST gives totally bogus spans if the expansion of an imported macro causes compilation errors. The best solution seems to be to serialize the actual textual macro definition and load it the same way the std-macros are. I'm not totally confident that getting the source from the CodeMap will always do the right thing, but it seems to work in simple cases.
2 parents cd8ee78 + d908e97 commit 4ce84fa

File tree

10 files changed

+31
-15
lines changed

10 files changed

+31
-15
lines changed

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl CrateLoader for Loader {
420420
}
421421
}
422422

423-
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[@ast::Item] {
423+
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[~str] {
424424
csearch::get_exported_macros(self.env.sess.cstore, cnum)
425425
}
426426

src/librustc/metadata/csearch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub fn get_macro_registrar_fn(cstore: @cstore::CStore,
307307

308308
pub fn get_exported_macros(cstore: @cstore::CStore,
309309
crate_num: ast::CrateNum)
310-
-> ~[@ast::Item] {
310+
-> ~[~str] {
311311
let cdata = cstore.get_crate_data(crate_num);
312312
decoder::get_exported_macros(cdata)
313313
}

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use metadata::tydecode::{parse_ty_data, parse_def_id,
2323
use middle::ty::{ImplContainer, TraitContainer};
2424
use middle::ty;
2525
use middle::typeck;
26-
use middle::astencode;
2726
use middle::astencode::vtable_decoder_helpers;
2827

2928
use std::at_vec;
@@ -1282,12 +1281,12 @@ pub fn get_macro_registrar_fn(cdata: Cmd) -> Option<ast::DefId> {
12821281
.map(|doc| item_def_id(doc, cdata))
12831282
}
12841283

1285-
pub fn get_exported_macros(cdata: Cmd) -> ~[@ast::Item] {
1284+
pub fn get_exported_macros(cdata: Cmd) -> ~[~str] {
12861285
let macros = reader::get_doc(reader::Doc(cdata.data()),
12871286
tag_exported_macros);
12881287
let mut result = ~[];
12891288
reader::tagged_docs(macros, tag_macro_def, |macro_doc| {
1290-
result.push(astencode::decode_exported_macro(macro_doc));
1289+
result.push(macro_doc.as_str());
12911290
true
12921291
});
12931292
result

src/librustc/metadata/encoder.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use syntax::ast_map;
3737
use syntax::ast_util::*;
3838
use syntax::attr;
3939
use syntax::attr::AttrMetaMethods;
40+
use syntax::codemap;
4041
use syntax::diagnostic::SpanHandler;
4142
use syntax::parse::token::special_idents;
4243
use syntax::ast_util;
@@ -71,6 +72,7 @@ pub struct EncodeParams<'a> {
7172
cstore: @cstore::CStore,
7273
encode_inlined_item: encode_inlined_item<'a>,
7374
reachable: @RefCell<HashSet<ast::NodeId>>,
75+
codemap: @codemap::CodeMap,
7476
}
7577

7678
struct Stats {
@@ -101,6 +103,7 @@ pub struct EncodeContext<'a> {
101103
encode_inlined_item: encode_inlined_item<'a>,
102104
type_abbrevs: abbrev_map,
103105
reachable: @RefCell<HashSet<ast::NodeId>>,
106+
codemap: @codemap::CodeMap,
104107
}
105108

106109
pub fn reachable(ecx: &EncodeContext, id: NodeId) -> bool {
@@ -1714,8 +1717,10 @@ impl<'a, 'b> Visitor<()> for MacroDefVisitor<'a, 'b> {
17141717
fn visit_item(&mut self, item: &Item, _: ()) {
17151718
match item.node {
17161719
ItemMac(..) => {
1720+
let def = self.ecx.codemap.span_to_snippet(item.span)
1721+
.expect("Unable to find source for macro");
17171722
self.ebml_w.start_tag(tag_macro_def);
1718-
astencode::encode_exported_macro(self.ebml_w, item);
1723+
self.ebml_w.wr_str(def);
17191724
self.ebml_w.end_tag();
17201725
}
17211726
_ => {}
@@ -1881,6 +1886,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
18811886
link_meta,
18821887
reachable,
18831888
non_inlineable_statics,
1889+
codemap,
18841890
..
18851891
} = parms;
18861892
let type_abbrevs = @RefCell::new(HashMap::new());
@@ -1897,6 +1903,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
18971903
encode_inlined_item: encode_inlined_item,
18981904
type_abbrevs: type_abbrevs,
18991905
reachable: reachable,
1906+
codemap: codemap,
19001907
};
19011908

19021909
let mut ebml_w = writer::Encoder(wr);

src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ pub fn crate_ctxt_to_encode_parms<'r>(cx: &'r CrateContext, ie: encoder::encode_
26782678
cstore: cx.sess.cstore,
26792679
encode_inlined_item: ie,
26802680
reachable: cx.reachable,
2681+
codemap: cx.sess.codemap,
26812682
}
26822683
}
26832684

src/librustpkg/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'a> base::CrateLoader for CrateLoader<'a> {
623623
self.loader.load_crate(crate)
624624
}
625625

626-
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[@ast::Item] {
626+
fn get_exported_macros(&mut self, cnum: ast::CrateNum) -> ~[~str] {
627627
self.loader.get_exported_macros(cnum)
628628
}
629629

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub struct MacroCrate {
299299

300300
pub trait CrateLoader {
301301
fn load_crate(&mut self, crate: &ast::ViewItem) -> MacroCrate;
302-
fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[@ast::Item];
302+
fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[~str];
303303
fn get_registrar_symbol(&mut self, crate_num: ast::CrateNum) -> Option<~str>;
304304
}
305305

src/libsyntax/ext/expand.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,20 @@ pub fn expand_view_item(vi: &ast::ViewItem,
406406
fn load_extern_macros(crate: &ast::ViewItem, fld: &mut MacroExpander) {
407407
let MacroCrate { lib, cnum } = fld.cx.loader.load_crate(crate);
408408

409+
let crate_name = match crate.node {
410+
ast::ViewItemExternMod(ref name, _, _) => token::ident_to_str(name),
411+
_ => unreachable!(),
412+
};
413+
let name = format!("<{} macros>", crate_name).to_managed();
414+
409415
let exported_macros = fld.cx.loader.get_exported_macros(cnum);
410-
for &it in exported_macros.iter() {
411-
expand_item_mac(it, fld);
416+
for source in exported_macros.iter() {
417+
let item = parse::parse_item_from_source_str(name,
418+
source.to_managed(),
419+
fld.cx.cfg(),
420+
fld.cx.parse_sess())
421+
.expect("expected a serialized item");
422+
expand_item_mac(item, fld);
412423
}
413424

414425
let path = match lib {
@@ -944,7 +955,6 @@ pub fn inject_std_macros(parse_sess: @parse::ParseSess,
944955
let sm = match parse_item_from_source_str(@"<std-macros>",
945956
std_macros(),
946957
cfg.clone(),
947-
~[],
948958
parse_sess) {
949959
Some(item) => item,
950960
None => fail!("expected core macros to parse correctly")
@@ -1212,7 +1222,7 @@ mod test {
12121222
fail!("lolwut")
12131223
}
12141224

1215-
fn get_exported_macros(&mut self, _: ast::CrateNum) -> ~[@ast::Item] {
1225+
fn get_exported_macros(&mut self, _: ast::CrateNum) -> ~[~str] {
12161226
fail!("lolwut")
12171227
}
12181228

@@ -1289,7 +1299,7 @@ mod test {
12891299
let item_ast = parse::parse_item_from_source_str(
12901300
@"<test>",
12911301
src,
1292-
cfg,~[],sess);
1302+
cfg,sess);
12931303
match item_ast {
12941304
Some(_) => (), // success
12951305
None => fail!("expected this to parse")

src/libsyntax/ext/quote.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ pub mod rt {
250250
@"<quote expansion>",
251251
s,
252252
self.cfg(),
253-
~[],
254253
self.parse_sess());
255254
match res {
256255
Some(ast) => ast,

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ pub fn parse_item_from_source_str(
130130
name: @str,
131131
source: @str,
132132
cfg: ast::CrateConfig,
133-
attrs: ~[ast::Attribute],
134133
sess: @ParseSess
135134
) -> Option<@ast::Item> {
136135
let mut p = new_parser_from_source_str(sess, cfg, name, source);
136+
let attrs = p.parse_outer_attributes();
137137
maybe_aborted(p.parse_item(attrs),p)
138138
}
139139

0 commit comments

Comments
 (0)