Skip to content

Commit 08c5d0c

Browse files
Improve code and apply suggestions
1 parent ea83de7 commit 08c5d0c

File tree

9 files changed

+41
-28
lines changed

9 files changed

+41
-28
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ fn build_macro(
780780
source: utils::display_macro_source(cx, name, &def),
781781
macro_rules: def.macro_rules,
782782
},
783-
None,
783+
MacroKinds::BANG,
784784
),
785785
None,
786786
),
@@ -804,7 +804,7 @@ fn build_macro(
804804
source: utils::display_macro_source(cx, name, &def),
805805
macro_rules: def.macro_rules,
806806
},
807-
Some(macro_kinds),
807+
macro_kinds,
808808
);
809809
let mut ret = vec![];
810810
for kind in macro_kinds.iter().filter(|kind| *kind != MacroKinds::BANG) {

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,7 @@ fn clean_maybe_renamed_item<'tcx>(
28492849
source: display_macro_source(cx, name, macro_def),
28502850
macro_rules: macro_def.macro_rules,
28512851
},
2852-
None,
2852+
MacroKinds::BANG,
28532853
),
28542854
MacroKinds::ATTR => clean_proc_macro(item, &mut name, MacroKind::Attr, cx),
28552855
MacroKinds::DERIVE => clean_proc_macro(item, &mut name, MacroKind::Derive, cx),
@@ -2859,7 +2859,7 @@ fn clean_maybe_renamed_item<'tcx>(
28592859
source: display_macro_source(cx, name, macro_def),
28602860
macro_rules: macro_def.macro_rules,
28612861
},
2862-
Some(kinds),
2862+
kinds,
28632863
);
28642864
let mac = generate_item_with_correct_attrs(
28652865
cx,

src/librustdoc/clean/types.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,12 @@ pub(crate) enum ItemKind {
965965
ForeignStaticItem(Static, hir::Safety),
966966
/// `type`s from an extern block
967967
ForeignTypeItem,
968-
MacroItem(Macro, Option<MacroKinds>),
968+
MacroItem(Macro, MacroKinds),
969+
/// This is NOT an attribute proc-macro but a bang macro with support for being used as an
970+
/// attribute macro.
969971
AttrMacroItem,
972+
/// This is NOT an attribute proc-macro but a bang macro with support for being used as a
973+
/// derive macro.
970974
DeriveMacroItem,
971975
ProcMacroItem(ProcMacro),
972976
PrimitiveItem(PrimitiveType),

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ fn is_default_id(id: &str) -> bool {
20352035
| "crate-search"
20362036
| "crate-search-div"
20372037
// This is the list of IDs used in HTML generated in Rust (including the ones
2038-
// used in tera template files).
2038+
// used in askama template files).
20392039
| "themeStyle"
20402040
| "settings-menu"
20412041
| "help-button"
@@ -2074,7 +2074,7 @@ fn is_default_id(id: &str) -> bool {
20742074
| "blanket-implementations-list"
20752075
| "deref-methods"
20762076
| "layout"
2077-
| "aliased-type"
2077+
| "aliased-type",
20782078
)
20792079
}
20802080

src/librustdoc/html/render/context.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,18 @@ impl SharedContext<'_> {
168168

169169
struct SidebarItem {
170170
name: String,
171-
is_actually_macro: bool,
171+
/// Bang macros can now be used as attribute/derive macros, making it tricky to correctly
172+
/// handle all their cases at once, which means that even if they are categorized as
173+
/// derive/attribute macros, they should still link to a "macro_rules" URL.
174+
is_macro_rules: bool,
172175
}
173176

174177
impl serde::Serialize for SidebarItem {
175178
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
176179
where
177180
S: serde::Serializer,
178181
{
179-
if self.is_actually_macro {
182+
if self.is_macro_rules {
180183
let mut seq = serializer.serialize_seq(Some(2))?;
181184
seq.serialize_element(&self.name)?;
182185
seq.serialize_element(&1)?;
@@ -348,7 +351,7 @@ impl<'tcx> Context<'tcx> {
348351
let name = name.to_string();
349352
map.entry(type_)
350353
.or_default()
351-
.push(SidebarItem { name, is_actually_macro: item.is_macro_placeholder() });
354+
.push(SidebarItem { name, is_macro_rules: item.is_macro_placeholder() });
352355
}
353356
}
354357

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,7 @@ impl ItemSection {
26002600
Self::ForeignTypes => "foreign-types",
26012601
Self::Keywords => "keywords",
26022602
Self::Attributes => "attributes",
2603-
Self::AttributeMacros => "attributes",
2603+
Self::AttributeMacros => "attribute-macros",
26042604
Self::DeriveMacros => "derives",
26052605
Self::TraitAliases => "trait-aliases",
26062606
}

src/librustdoc/html/render/print_item.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,15 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
406406

407407
for type_ in types {
408408
let my_section = item_ty_to_section(type_);
409-
let section_id = my_section.id();
410-
let tag =
411-
if section_id == "reexports" { REEXPORTS_TABLE_OPEN } else { ITEM_TABLE_OPEN };
409+
let tag = if my_section == super::ItemSection::Reexports {
410+
REEXPORTS_TABLE_OPEN
411+
} else {
412+
ITEM_TABLE_OPEN
413+
};
412414
write!(
413415
w,
414416
"{}",
415-
write_section_heading(my_section.name(), &cx.derive_id(section_id), None, tag)
417+
write_section_heading(my_section.name(), &cx.derive_id(my_section.id()), None, tag)
416418
)?;
417419

418420
for (_, myitem) in &not_stripped_items[&type_] {
@@ -1889,7 +1891,7 @@ fn item_macro(
18891891
cx: &Context<'_>,
18901892
it: &clean::Item,
18911893
t: &clean::Macro,
1892-
kinds: Option<MacroKinds>,
1894+
kinds: MacroKinds,
18931895
) -> impl fmt::Display {
18941896
fmt::from_fn(move |w| {
18951897
wrap_item(w, |w| {
@@ -1900,7 +1902,7 @@ fn item_macro(
19001902
}
19011903
write!(w, "{}", Escape(&t.source))
19021904
})?;
1903-
if let Some(kinds) = kinds {
1905+
if kinds != MacroKinds::BANG {
19041906
write!(
19051907
w,
19061908
"<h3 class='macro-info'>ⓘ This is {} {}</h3>",

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare global {
2727
/** Make the current theme easy to find */
2828
currentTheme: HTMLLinkElement|null;
2929
/** Generated in `render/context.rs` */
30-
SIDEBAR_ITEMS?: { [key: string]: string[] };
30+
SIDEBAR_ITEMS?: { [key: string]: (string | [string, 1])[] };
3131
/** Notable trait data */
3232
NOTABLE_TRAITS?: { [key: string]: string };
3333
CURRENT_TOOLTIP_ELEMENT?: HTMLElement & { TOOLTIP_BASE: HTMLElement };

src/librustdoc/json/conversions.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ impl JsonRenderer<'_> {
5252
let clean::ItemInner { name, item_id, .. } = *item.inner;
5353
let id = self.id_from_item(item);
5454
let inner = match item.kind {
55-
clean::KeywordItem | clean::AttributeItem => return None,
55+
clean::KeywordItem
56+
| clean::AttributeItem
57+
// Placeholder so no need to handle it.
58+
| clean::AttrMacroItem
59+
// Placeholder so no need to handle it.
60+
| clean::DeriveMacroItem => return None,
5661
clean::StrippedItem(ref inner) => {
5762
match &**inner {
5863
// We document stripped modules as with `Module::is_stripped` set to
@@ -61,12 +66,12 @@ impl JsonRenderer<'_> {
6166
clean::ModuleItem(_)
6267
if self.imported_items.contains(&item_id.expect_def_id()) =>
6368
{
64-
from_clean_item(item, self)?
69+
from_clean_item(item, self)
6570
}
6671
_ => return None,
6772
}
6873
}
69-
_ => from_clean_item(item, self)?,
74+
_ => from_clean_item(item, self),
7075
};
7176
Some(Item {
7277
id,
@@ -268,13 +273,13 @@ impl FromClean<clean::AssocItemConstraintKind> for AssocItemConstraintKind {
268273
}
269274
}
270275

271-
fn from_clean_item(item: &clean::Item, renderer: &JsonRenderer<'_>) -> Option<ItemEnum> {
276+
fn from_clean_item(item: &clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
272277
use clean::ItemKind::*;
273278
let name = item.name;
274279
let is_crate = item.is_crate();
275280
let header = item.fn_header(renderer.tcx);
276281

277-
Some(match &item.inner.kind {
282+
match &item.inner.kind {
278283
ModuleItem(m) => {
279284
ItemEnum::Module(Module { is_crate, items: renderer.ids(&m.items), is_stripped: false })
280285
}
@@ -309,8 +314,6 @@ fn from_clean_item(item: &clean::Item, renderer: &JsonRenderer<'_>) -> Option<It
309314
const_: ci.kind.into_json(renderer),
310315
},
311316
MacroItem(m, _) => ItemEnum::Macro(m.source.clone()),
312-
// They are just placeholders so no need to handle them.
313-
AttrMacroItem | DeriveMacroItem => return None,
314317
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_json(renderer)),
315318
PrimitiveItem(p) => {
316319
ItemEnum::Primitive(Primitive {
@@ -337,8 +340,9 @@ fn from_clean_item(item: &clean::Item, renderer: &JsonRenderer<'_>) -> Option<It
337340
bounds: b.into_json(renderer),
338341
type_: Some(t.item_type.as_ref().unwrap_or(&t.type_).into_json(renderer)),
339342
},
340-
// `convert_item` early returns `None` for stripped items, keywords and attributes.
341-
KeywordItem | AttributeItem => unreachable!(),
343+
// `convert_item` early returns `None` for stripped items, keywords, attributes and
344+
// "special" macro rules.
345+
KeywordItem | AttributeItem | AttrMacroItem | DeriveMacroItem => unreachable!(),
342346
StrippedItem(inner) => {
343347
match inner.as_ref() {
344348
ModuleItem(m) => ItemEnum::Module(Module {
@@ -354,7 +358,7 @@ fn from_clean_item(item: &clean::Item, renderer: &JsonRenderer<'_>) -> Option<It
354358
name: name.as_ref().unwrap().to_string(),
355359
rename: src.map(|x| x.to_string()),
356360
},
357-
})
361+
}
358362
}
359363

360364
impl FromClean<clean::Struct> for Struct {

0 commit comments

Comments
 (0)