Skip to content

Commit 87e98a1

Browse files
authored
Unrolled build for #152733
Rollup merge of #152733 - Ozzy1423:attr-doc, r=jdonszelmann Port #[rustc_doc_primitive] to the new attribute parser Tracking issue: #131229 r? @JonathanBrouwer
2 parents 41198cb + bd54cd6 commit 87e98a1

File tree

10 files changed

+59
-21
lines changed

10 files changed

+59
-21
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ impl AttributeExt for Attribute {
296296
}
297297
false
298298
}
299+
300+
fn is_rustc_doc_primitive(&self) -> bool {
301+
self.has_name(sym::rustc_doc_primitive)
302+
}
299303
}
300304

301305
impl Attribute {
@@ -935,6 +939,9 @@ pub trait AttributeExt: Debug {
935939

936940
/// Returns `true` is this attribute contains `doc(keyword)` or `doc(attribute)`.
937941
fn is_doc_keyword_or_attribute(&self) -> bool;
942+
943+
/// Returns `true` if this is a `#[rustc_doc_primitive]` attribute.
944+
fn is_rustc_doc_primitive(&self) -> bool;
938945
}
939946

940947
// FIXME(fn_delegation): use function delegation instead of manually forwarding

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,3 +1319,27 @@ impl<S: Stage> NoArgsAttributeParser<S> for PreludeImportParser {
13191319
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Use)]);
13201320
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::PreludeImport;
13211321
}
1322+
1323+
pub(crate) struct RustcDocPrimitiveParser;
1324+
1325+
impl<S: Stage> SingleAttributeParser<S> for RustcDocPrimitiveParser {
1326+
const PATH: &[Symbol] = &[sym::rustc_doc_primitive];
1327+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1328+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1329+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Mod)]);
1330+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "primitive name");
1331+
1332+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1333+
let Some(nv) = args.name_value() else {
1334+
cx.expected_name_value(args.span().unwrap_or(cx.attr_span), None);
1335+
return None;
1336+
};
1337+
1338+
let Some(value_str) = nv.value_as_str() else {
1339+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
1340+
return None;
1341+
};
1342+
1343+
Some(AttributeKind::RustcDocPrimitive(cx.attr_span, value_str))
1344+
}
1345+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ attribute_parsers!(
205205
Single<RustcDefPath>,
206206
Single<RustcDeprecatedSafe2024Parser>,
207207
Single<RustcDiagnosticItemParser>,
208+
Single<RustcDocPrimitiveParser>,
208209
Single<RustcForceInlineParser>,
209210
Single<RustcIfThisChangedParser>,
210211
Single<RustcLayoutScalarValidRangeEndParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,9 @@ pub enum AttributeKind {
11691169
/// Represents `#[rustc_do_not_const_check]`
11701170
RustcDoNotConstCheck,
11711171

1172+
/// Represents `#[rustc_doc_primitive = ...]`
1173+
RustcDocPrimitive(Span, Symbol),
1174+
11721175
/// Represents `#[rustc_dummy]`.
11731176
RustcDummy,
11741177

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl AttributeKind {
117117
RustcDeprecatedSafe2024 { .. } => Yes,
118118
RustcDiagnosticItem(..) => Yes,
119119
RustcDoNotConstCheck => Yes,
120+
RustcDocPrimitive(..) => Yes,
120121
RustcDummy => No,
121122
RustcDumpDefParents => No,
122123
RustcDumpItemBounds => No,

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,10 @@ impl AttributeExt for Attribute {
14671467
fn is_doc_keyword_or_attribute(&self) -> bool {
14681468
matches!(self, Attribute::Parsed(AttributeKind::Doc(d)) if d.attribute.is_some() || d.keyword.is_some())
14691469
}
1470+
1471+
fn is_rustc_doc_primitive(&self) -> bool {
1472+
matches!(self, Attribute::Parsed(AttributeKind::RustcDocPrimitive(..)))
1473+
}
14701474
}
14711475

14721476
// FIXME(fn_delegation): use function delegation instead of manually forwarding

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
312312
| AttributeKind::RustcDeprecatedSafe2024 {..}
313313
| AttributeKind::RustcDiagnosticItem(..)
314314
| AttributeKind::RustcDoNotConstCheck
315+
| AttributeKind::RustcDocPrimitive(..)
315316
| AttributeKind::RustcDummy
316317
| AttributeKind::RustcDumpDefParents
317318
| AttributeKind::RustcDumpItemBounds
@@ -402,7 +403,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
402403
// internal
403404
| sym::rustc_inherit_overflow_checks
404405
| sym::rustc_on_unimplemented
405-
| sym::rustc_doc_primitive
406406
| sym::rustc_layout
407407
| sym::rustc_autodiff
408408
| sym::rustc_capture_analysis

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub fn inner_docs(attrs: &[impl AttributeExt]) -> bool {
367367
/// Has `#[rustc_doc_primitive]` or `#[doc(keyword)]` or `#[doc(attribute)]`.
368368
pub fn has_primitive_or_keyword_or_attribute_docs(attrs: &[impl AttributeExt]) -> bool {
369369
for attr in attrs {
370-
if attr.has_name(sym::rustc_doc_primitive) || attr.is_doc_keyword_or_attribute() {
370+
if attr.is_rustc_doc_primitive() || attr.is_doc_keyword_or_attribute() {
371371
return true;
372372
}
373373
}

src/librustdoc/clean/types.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,16 @@ impl ExternalCrate {
308308
// duplicately for the same primitive. This is handled later on when
309309
// rendering by delegating everything to a hash map.
310310
fn as_primitive(def_id: DefId, tcx: TyCtxt<'_>) -> Option<(DefId, PrimitiveType)> {
311-
tcx.get_attrs(def_id, sym::rustc_doc_primitive).next().map(|attr| {
312-
let attr_value = attr.value_str().expect("syntax should already be validated");
313-
let Some(prim) = PrimitiveType::from_symbol(attr_value) else {
314-
span_bug!(
315-
attr.span(),
316-
"primitive `{attr_value}` is not a member of `PrimitiveType`"
317-
);
318-
};
319-
320-
(def_id, prim)
321-
})
311+
let Some((attr_span, prim_sym)) = find_attr!(
312+
tcx.get_all_attrs(def_id),
313+
AttributeKind::RustcDocPrimitive(span, prim) => (*span, *prim)
314+
) else {
315+
return None;
316+
};
317+
let Some(prim) = PrimitiveType::from_symbol(prim_sym) else {
318+
span_bug!(attr_span, "primitive `{prim_sym}` is not a member of `PrimitiveType`");
319+
};
320+
Some((def_id, prim))
322321
}
323322

324323
self.mapped_root_modules(tcx, as_primitive)

src/librustdoc/json/ids.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//! other phases think of as an "item".
77
88
use rustc_data_structures::fx::FxHashMap;
9-
use rustc_hir::def::DefKind;
9+
use rustc_hir::attrs::AttributeKind;
1010
use rustc_hir::def_id::DefId;
11-
use rustc_span::{Symbol, sym};
11+
use rustc_hir::find_attr;
12+
use rustc_span::Symbol;
1213
use rustdoc_json_types as types;
1314

1415
use super::JsonRenderer;
@@ -88,12 +89,10 @@ impl JsonRenderer<'_> {
8889
// We need this workaround because primitive types' DefId actually refers to
8990
// their parent module, which isn't present in the output JSON items. So
9091
// instead, we directly get the primitive symbol
91-
if matches!(self.tcx.def_kind(def_id), DefKind::Mod)
92-
&& let Some(prim) = self
93-
.tcx
94-
.get_attrs(def_id, sym::rustc_doc_primitive)
95-
.find_map(|attr| attr.value_str())
96-
{
92+
if let Some(prim) = find_attr!(
93+
self.tcx.get_all_attrs(def_id),
94+
AttributeKind::RustcDocPrimitive(_, prim) => *prim
95+
) {
9796
Some(prim)
9897
} else {
9998
self.tcx.opt_item_name(def_id)

0 commit comments

Comments
 (0)