Skip to content

Commit 5a77d25

Browse files
committed
repair macro docs
In f1ad425, I changed the handling of macros, to prevent macro invocations from occurring in fully expanded source. Instead, I added a side table. It contained only the spans of the macros, because this was the only information required in order to make macro export work. However, librustdoc was also affected by this change, since it extracts macro information in a similar way. As a result of the earlier change, exported macros were no longer documented. In order to repair this, I've adjusted the side table to contain whole items, rather than just the spans.
1 parent 92b5bf8 commit 5a77d25

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,8 +1612,8 @@ fn encode_macro_defs(ecx: &EncodeContext,
16121612
krate: &Crate,
16131613
ebml_w: &mut Encoder) {
16141614
ebml_w.start_tag(tag_exported_macros);
1615-
for span in krate.exported_macros.iter() {
1616-
encode_macro_def(ecx, ebml_w, span);
1615+
for item in krate.exported_macros.iter() {
1616+
encode_macro_def(ecx, ebml_w, &item.span);
16171617
}
16181618
ebml_w.end_tag();
16191619
}

src/librustdoc/visit_ast.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ use std::gc::{Gc, GC};
2626
use core;
2727
use doctree::*;
2828

29+
// looks to me like the first two of these are actually
30+
// output parameters, maybe only mutated once; perhaps
31+
// better simply to have the visit method return a tuple
32+
// containing them?
33+
34+
// also, is there some reason that this doesn't use the 'visit'
35+
// framework from syntax?
36+
2937
pub struct RustdocVisitor<'a> {
3038
pub module: Module,
3139
pub attrs: Vec<ast::Attribute>,
@@ -64,6 +72,9 @@ impl<'a> RustdocVisitor<'a> {
6472
ast::CRATE_NODE_ID,
6573
&krate.module,
6674
None);
75+
// attach the crate's exported macros to the top-level module:
76+
self.module.macros = krate.exported_macros.iter()
77+
.map(|it| self.visit_macro(*it)).collect();
6778
self.module.is_crate = true;
6879
}
6980

@@ -323,15 +334,20 @@ impl<'a> RustdocVisitor<'a> {
323334
ast::ItemForeignMod(ref fm) => {
324335
om.foreigns.push(fm.clone());
325336
}
326-
ast::ItemMac(ref _m) => {
327-
om.macros.push(Macro {
328-
id: item.id,
329-
attrs: item.attrs.iter().map(|x| *x).collect(),
330-
name: item.ident,
331-
where: item.span,
332-
stab: self.stability(item.id),
333-
})
337+
ast::ItemMac(_) => {
338+
fail!("rustdoc: macros should be gone, after expansion");
334339
}
335340
}
336341
}
342+
343+
// convert each exported_macro into a doc item
344+
fn visit_macro(&self, item: &ast::Item) -> Macro {
345+
Macro {
346+
id: item.id,
347+
attrs: item.attrs.iter().map(|x| *x).collect(),
348+
name: item.ident,
349+
where: item.span,
350+
stab: self.stability(item.id),
351+
}
352+
}
337353
}

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub struct Crate {
256256
pub attrs: Vec<Attribute>,
257257
pub config: CrateConfig,
258258
pub span: Span,
259-
pub exported_macros: Vec<Span>
259+
pub exported_macros: Vec<Gc<Item>>
260260
}
261261

262262
pub type MetaItem = Spanned<MetaItem_>;

src/libsyntax/ext/base.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ pub type IdentMacroExpanderFn =
104104
/// just into the compiler's internal macro table, for `make_def`).
105105
pub trait MacResult {
106106
/// Define a new macro.
107-
// this should go away; the idea that a macro might expand into
108-
// either a macro definition or an expression, depending on what
109-
// the context wants, is kind of silly.
107+
// this particular flavor should go away; the idea that a macro might
108+
// expand into either a macro definition or an expression, depending
109+
// on what the context wants, is kind of silly.
110110
fn make_def(&self) -> Option<MacroDef> {
111111
None
112112
}
@@ -428,7 +428,7 @@ pub struct ExtCtxt<'a> {
428428

429429
pub mod_path: Vec<ast::Ident> ,
430430
pub trace_mac: bool,
431-
pub exported_macros: Vec<codemap::Span>
431+
pub exported_macros: Vec<Gc<ast::Item>>
432432
}
433433

434434
impl<'a> ExtCtxt<'a> {
@@ -559,9 +559,6 @@ impl<'a> ExtCtxt<'a> {
559559
pub fn name_of(&self, st: &str) -> ast::Name {
560560
token::intern(st)
561561
}
562-
pub fn push_exported_macro(&mut self, span: codemap::Span) {
563-
self.exported_macros.push(span);
564-
}
565562
}
566563

567564
/// Extract a string literal from the macro expanded version of `expr`,

src/libsyntax/ext/expand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
536536
// create issue to recommend refactoring here?
537537
fld.extsbox.insert(intern(name.as_slice()), ext);
538538
if attr::contains_name(it.attrs.as_slice(), "macro_export") {
539-
fld.cx.push_exported_macro(it.span);
539+
fld.cx.exported_macros.push(it);
540540
}
541541
SmallVector::zero()
542542
}
@@ -1029,7 +1029,7 @@ pub struct ExportedMacros {
10291029
pub fn expand_crate(parse_sess: &parse::ParseSess,
10301030
cfg: ExpansionConfig,
10311031
// these are the macros being imported to this crate:
1032-
macros: Vec<ExportedMacros>,
1032+
imported_macros: Vec<ExportedMacros>,
10331033
user_exts: Vec<NamedSyntaxExtension>,
10341034
c: Crate) -> Crate {
10351035
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
@@ -1038,7 +1038,7 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
10381038
cx: &mut cx,
10391039
};
10401040

1041-
for ExportedMacros { crate_name, macros } in macros.move_iter() {
1041+
for ExportedMacros { crate_name, macros } in imported_macros.move_iter() {
10421042
let name = format!("<{} macros>", token::get_ident(crate_name))
10431043
.into_string();
10441044

src/libsyntax/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
751751
attrs: c.attrs.iter().map(|x| folder.fold_attribute(*x)).collect(),
752752
config: c.config.iter().map(|x| fold_meta_item_(*x, folder)).collect(),
753753
span: folder.new_span(c.span),
754-
exported_macros: c.exported_macros.iter().map(|sp| folder.new_span(*sp)).collect(),
754+
exported_macros: c.exported_macros
755755
}
756756
}
757757

0 commit comments

Comments
 (0)