diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-06-14 12:41:30 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-08-28 18:24:58 +0200 |
| commit | 10bd61dcf2e5a7dbe95b4b0588a432a444f907a9 (patch) | |
| tree | d0d989264697b361b671f0a03a9a95a11fd9b49a | |
| parent | 38e8963b148fef9fb9c4263cc49d448985bb9982 (diff) | |
| download | rust-10bd61dcf2e5a7dbe95b4b0588a432a444f907a9.tar.gz rust-10bd61dcf2e5a7dbe95b4b0588a432a444f907a9.zip | |
Create new `Item::is_fake_item` method as equivalent to check for `is_primitive`, `is_keyword` and `is_attribute` methods
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 66 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 11 | ||||
| -rw-r--r-- | src/librustdoc/html/render/context.rs | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/render/print_item.rs | 2 |
4 files changed, 58 insertions, 23 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index fa7591b5113..410c39fbb41 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -99,6 +99,21 @@ impl IntoDiagArg for ProcMacroKind { } } +#[derive(Clone, Copy)] +enum DocFakeItemKind { + Attribute, + Keyword, +} + +impl DocFakeItemKind { + fn name(self) -> &'static str { + match self { + Self::Attribute => "attribute", + Self::Keyword => "keyword", + } + } +} + struct CheckAttrVisitor<'tcx> { tcx: TyCtxt<'tcx>, @@ -855,7 +870,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { &self, meta: &MetaItemInner, hir_id: HirId, - is_keyword: bool, + attr_kind: DocFakeItemKind, ) { fn is_doc_keyword(s: Symbol) -> bool { // FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we @@ -868,13 +883,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { rustc_feature::BUILTIN_ATTRIBUTE_MAP.contains_key(&s) } - fn get_attr_name(is_keyword: bool) -> &'static str { - if is_keyword { "keyword" } else { "attribute" } - } - let value = match meta.value_str() { Some(value) if value != sym::empty => value, - _ => return self.doc_attr_str_error(meta, get_attr_name(is_keyword)), + _ => return self.doc_attr_str_error(meta, attr_kind.name()), }; let item_kind = match self.tcx.hir_node(hir_id) { @@ -886,7 +897,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { if !module.item_ids.is_empty() { self.dcx().emit_err(errors::DocKeywordAttributeEmptyMod { span: meta.span(), - attr_name: get_attr_name(is_keyword), + attr_name: attr_kind.name(), }); return; } @@ -894,23 +905,28 @@ impl<'tcx> CheckAttrVisitor<'tcx> { _ => { self.dcx().emit_err(errors::DocKeywordAttributeNotMod { span: meta.span(), - attr_name: get_attr_name(is_keyword), + attr_name: attr_kind.name(), }); return; } } - if is_keyword { - if !is_doc_keyword(value) { - self.dcx().emit_err(errors::DocKeywordNotKeyword { - span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()), - keyword: value, - }); + match attr_kind { + DocFakeItemKind::Keyword => { + if !is_doc_keyword(value) { + self.dcx().emit_err(errors::DocKeywordNotKeyword { + span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()), + keyword: value, + }); + } + } + DocFakeItemKind::Attribute => { + if !is_builtin_attr(value) { + self.dcx().emit_err(errors::DocAttributeNotAttribute { + span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()), + attribute: value, + }); + } } - } else if !is_builtin_attr(value) { - self.dcx().emit_err(errors::DocAttributeNotAttribute { - span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()), - attribute: value, - }); } } @@ -1170,13 +1186,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> { Some(sym::keyword) => { if self.check_attr_not_crate_level(meta, hir_id, "keyword") { - self.check_doc_keyword_and_attribute(meta, hir_id, true); + self.check_doc_keyword_and_attribute( + meta, + hir_id, + DocFakeItemKind::Keyword, + ); } } Some(sym::attribute) => { if self.check_attr_not_crate_level(meta, hir_id, "attribute") { - self.check_doc_keyword_and_attribute(meta, hir_id, false); + self.check_doc_keyword_and_attribute( + meta, + hir_id, + DocFakeItemKind::Attribute, + ); } } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 1502ec9bd78..fcff15650ce 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -608,6 +608,17 @@ impl Item { pub(crate) fn is_attribute(&self) -> bool { self.type_() == ItemType::Attribute } + /// Returns `true` if the item kind is one of the following: + /// + /// * `ItemType::Primitive` + /// * `ItemType::Keyword` + /// * `ItemType::Attribute` + /// + /// They are considered fake because they only exist thanks to their + /// `#[doc(primitive|keyword|attribute)]` attribute. + pub(crate) fn is_fake_item(&self) -> bool { + matches!(self.type_(), ItemType::Primitive | ItemType::Keyword | ItemType::Attribute) + } pub(crate) fn is_stripped(&self) -> bool { match self.kind { StrippedItem(..) => true, diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index cafbcf7e8dd..5f92ab2fada 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -218,7 +218,7 @@ impl<'tcx> Context<'tcx> { } else { it.name.as_ref().unwrap().as_str() }; - if !it.is_primitive() && !it.is_keyword() && !it.is_attribute() { + if !it.is_fake_item() { if !is_module { title.push_str(" in "); } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 530e6da4ee3..afa438f2596 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -194,7 +194,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Disp let src_href = if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None }; - let path_components = if item.is_primitive() || item.is_keyword() || item.is_attribute() { + let path_components = if item.is_fake_item() { vec![] } else { let cur = &cx.current; |
