Skip to content

Commit 5ea65c0

Browse files
committed
Auto merge of #30074 - jonas-schievink:macro-doc, r=sanxiyn
Fixes #17616 New docs for `panic!`: ```rust macro_rules! panic { () => { ... }; ($msg:expr) => { ... }; ($fmt:expr, $($arg:tt)+) => { ... }; } ``` New docs for `assert!`: ```rust macro_rules! assert { ( $ cond : expr ) => { ... }; ( $ cond : expr , $ ( $ arg : tt ) + ) => { ... }; } ``` <sup>not pretty, but at least it's not worse 😂
2 parents 9e05951 + ff339bd commit 5ea65c0

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,15 +2663,18 @@ pub struct Macro {
26632663

26642664
impl Clean<Item> for doctree::Macro {
26652665
fn clean(&self, cx: &DocContext) -> Item {
2666+
let name = format!("{}!", self.name.clean(cx));
26662667
Item {
2667-
name: Some(format!("{}!", self.name.clean(cx))),
2668+
name: Some(name.clone()),
26682669
attrs: self.attrs.clean(cx),
26692670
source: self.whence.clean(cx),
26702671
visibility: hir::Public.clean(cx),
26712672
stability: self.stab.clean(cx),
26722673
def_id: cx.map.local_def_id(self.id),
26732674
inner: MacroItem(Macro {
2674-
source: self.whence.to_src(cx),
2675+
source: format!("macro_rules! {} {{\n{}}}",
2676+
name.trim_right_matches('!'), self.matchers.iter().map(|span|
2677+
format!(" {} => {{ ... }};\n", span.to_src(cx))).collect::<String>()),
26752678
imported_from: self.imported_from.clean(cx),
26762679
}),
26772680
}

src/librustdoc/doctree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ pub struct Macro {
213213
pub id: ast::NodeId,
214214
pub attrs: Vec<ast::Attribute>,
215215
pub whence: Span,
216+
pub matchers: Vec<Span>,
216217
pub stab: Option<attr::Stability>,
217218
pub imported_from: Option<Name>,
218219
}

src/librustdoc/visit_ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
400400

401401
// convert each exported_macro into a doc item
402402
fn visit_macro(&self, def: &hir::MacroDef) -> Macro {
403+
// Extract the spans of all matchers. They represent the "interface" of the macro.
404+
let matchers = def.body.chunks(4).map(|arm| arm[0].get_span()).collect();
405+
403406
Macro {
404407
id: def.id,
405408
attrs: def.attrs.clone(),
406409
name: def.name,
407410
whence: def.span,
411+
matchers: matchers,
408412
stab: self.stability(def.id),
409413
imported_from: def.imported_from,
410414
}

src/test/rustdoc/macros.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @has macros/macro.my_macro!.html //pre 'macro_rules! my_macro {'
12+
// @has - //pre '() => { ... };'
13+
// @has - //pre '($a:tt) => { ... };'
14+
// @has - //pre '($e:expr) => { ... };'
15+
#[macro_export]
16+
macro_rules! my_macro {
17+
() => [];
18+
($a:tt) => ();
19+
($e:expr) => {};
20+
}

0 commit comments

Comments
 (0)