Skip to content

Commit 10bd61d

Browse files
Create new Item::is_fake_item method as equivalent to check for is_primitive, is_keyword and is_attribute methods
1 parent 38e8963 commit 10bd61d

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ impl IntoDiagArg for ProcMacroKind {
9999
}
100100
}
101101

102+
#[derive(Clone, Copy)]
103+
enum DocFakeItemKind {
104+
Attribute,
105+
Keyword,
106+
}
107+
108+
impl DocFakeItemKind {
109+
fn name(self) -> &'static str {
110+
match self {
111+
Self::Attribute => "attribute",
112+
Self::Keyword => "keyword",
113+
}
114+
}
115+
}
116+
102117
struct CheckAttrVisitor<'tcx> {
103118
tcx: TyCtxt<'tcx>,
104119

@@ -855,7 +870,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
855870
&self,
856871
meta: &MetaItemInner,
857872
hir_id: HirId,
858-
is_keyword: bool,
873+
attr_kind: DocFakeItemKind,
859874
) {
860875
fn is_doc_keyword(s: Symbol) -> bool {
861876
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
@@ -868,13 +883,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
868883
rustc_feature::BUILTIN_ATTRIBUTE_MAP.contains_key(&s)
869884
}
870885

871-
fn get_attr_name(is_keyword: bool) -> &'static str {
872-
if is_keyword { "keyword" } else { "attribute" }
873-
}
874-
875886
let value = match meta.value_str() {
876887
Some(value) if value != sym::empty => value,
877-
_ => return self.doc_attr_str_error(meta, get_attr_name(is_keyword)),
888+
_ => return self.doc_attr_str_error(meta, attr_kind.name()),
878889
};
879890

880891
let item_kind = match self.tcx.hir_node(hir_id) {
@@ -886,31 +897,36 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
886897
if !module.item_ids.is_empty() {
887898
self.dcx().emit_err(errors::DocKeywordAttributeEmptyMod {
888899
span: meta.span(),
889-
attr_name: get_attr_name(is_keyword),
900+
attr_name: attr_kind.name(),
890901
});
891902
return;
892903
}
893904
}
894905
_ => {
895906
self.dcx().emit_err(errors::DocKeywordAttributeNotMod {
896907
span: meta.span(),
897-
attr_name: get_attr_name(is_keyword),
908+
attr_name: attr_kind.name(),
898909
});
899910
return;
900911
}
901912
}
902-
if is_keyword {
903-
if !is_doc_keyword(value) {
904-
self.dcx().emit_err(errors::DocKeywordNotKeyword {
905-
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
906-
keyword: value,
907-
});
913+
match attr_kind {
914+
DocFakeItemKind::Keyword => {
915+
if !is_doc_keyword(value) {
916+
self.dcx().emit_err(errors::DocKeywordNotKeyword {
917+
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
918+
keyword: value,
919+
});
920+
}
921+
}
922+
DocFakeItemKind::Attribute => {
923+
if !is_builtin_attr(value) {
924+
self.dcx().emit_err(errors::DocAttributeNotAttribute {
925+
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
926+
attribute: value,
927+
});
928+
}
908929
}
909-
} else if !is_builtin_attr(value) {
910-
self.dcx().emit_err(errors::DocAttributeNotAttribute {
911-
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
912-
attribute: value,
913-
});
914930
}
915931
}
916932

@@ -1170,13 +1186,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11701186

11711187
Some(sym::keyword) => {
11721188
if self.check_attr_not_crate_level(meta, hir_id, "keyword") {
1173-
self.check_doc_keyword_and_attribute(meta, hir_id, true);
1189+
self.check_doc_keyword_and_attribute(
1190+
meta,
1191+
hir_id,
1192+
DocFakeItemKind::Keyword,
1193+
);
11741194
}
11751195
}
11761196

11771197
Some(sym::attribute) => {
11781198
if self.check_attr_not_crate_level(meta, hir_id, "attribute") {
1179-
self.check_doc_keyword_and_attribute(meta, hir_id, false);
1199+
self.check_doc_keyword_and_attribute(
1200+
meta,
1201+
hir_id,
1202+
DocFakeItemKind::Attribute,
1203+
);
11801204
}
11811205
}
11821206

src/librustdoc/clean/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,17 @@ impl Item {
608608
pub(crate) fn is_attribute(&self) -> bool {
609609
self.type_() == ItemType::Attribute
610610
}
611+
/// Returns `true` if the item kind is one of the following:
612+
///
613+
/// * `ItemType::Primitive`
614+
/// * `ItemType::Keyword`
615+
/// * `ItemType::Attribute`
616+
///
617+
/// They are considered fake because they only exist thanks to their
618+
/// `#[doc(primitive|keyword|attribute)]` attribute.
619+
pub(crate) fn is_fake_item(&self) -> bool {
620+
matches!(self.type_(), ItemType::Primitive | ItemType::Keyword | ItemType::Attribute)
621+
}
611622
pub(crate) fn is_stripped(&self) -> bool {
612623
match self.kind {
613624
StrippedItem(..) => true,

src/librustdoc/html/render/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'tcx> Context<'tcx> {
218218
} else {
219219
it.name.as_ref().unwrap().as_str()
220220
};
221-
if !it.is_primitive() && !it.is_keyword() && !it.is_attribute() {
221+
if !it.is_fake_item() {
222222
if !is_module {
223223
title.push_str(" in ");
224224
}

src/librustdoc/html/render/print_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item) -> impl fmt::Disp
194194
let src_href =
195195
if cx.info.include_sources && !item.is_primitive() { cx.src_href(item) } else { None };
196196

197-
let path_components = if item.is_primitive() || item.is_keyword() || item.is_attribute() {
197+
let path_components = if item.is_fake_item() {
198198
vec![]
199199
} else {
200200
let cur = &cx.current;

0 commit comments

Comments
 (0)