Skip to content

Commit 6cea220

Browse files
committed
auto merge of #15751 : jbclements/rust/macro-docs-repair, r=cmr
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. Note that the resulting macro documentation now appears at the top level of the crate.
2 parents 44a71de + 5a77d25 commit 6cea220

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
@@ -1624,8 +1624,8 @@ fn encode_macro_defs(ecx: &EncodeContext,
16241624
krate: &Crate,
16251625
ebml_w: &mut Encoder) {
16261626
ebml_w.start_tag(tag_exported_macros);
1627-
for span in krate.exported_macros.iter() {
1628-
encode_macro_def(ecx, ebml_w, span);
1627+
for item in krate.exported_macros.iter() {
1628+
encode_macro_def(ecx, ebml_w, &item.span);
16291629
}
16301630
ebml_w.end_tag();
16311631
}

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
}
@@ -431,7 +431,7 @@ pub struct ExtCtxt<'a> {
431431

432432
pub mod_path: Vec<ast::Ident> ,
433433
pub trace_mac: bool,
434-
pub exported_macros: Vec<codemap::Span>
434+
pub exported_macros: Vec<Gc<ast::Item>>
435435
}
436436

437437
impl<'a> ExtCtxt<'a> {
@@ -562,9 +562,6 @@ impl<'a> ExtCtxt<'a> {
562562
pub fn name_of(&self, st: &str) -> ast::Name {
563563
token::intern(st)
564564
}
565-
pub fn push_exported_macro(&mut self, span: codemap::Span) {
566-
self.exported_macros.push(span);
567-
}
568565
}
569566

570567
/// 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
}
@@ -1039,7 +1039,7 @@ pub struct ExportedMacros {
10391039
pub fn expand_crate(parse_sess: &parse::ParseSess,
10401040
cfg: ExpansionConfig,
10411041
// these are the macros being imported to this crate:
1042-
macros: Vec<ExportedMacros>,
1042+
imported_macros: Vec<ExportedMacros>,
10431043
user_exts: Vec<NamedSyntaxExtension>,
10441044
c: Crate) -> Crate {
10451045
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
@@ -1048,7 +1048,7 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
10481048
cx: &mut cx,
10491049
};
10501050

1051-
for ExportedMacros { crate_name, macros } in macros.move_iter() {
1051+
for ExportedMacros { crate_name, macros } in imported_macros.move_iter() {
10521052
let name = format!("<{} macros>", token::get_ident(crate_name))
10531053
.into_string();
10541054

src/libsyntax/fold.rs

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

0 commit comments

Comments
 (0)