Skip to content

Commit 04e6a85

Browse files
committed
change rustdoc to use hir::Attribute
1 parent 538265a commit 04e6a85

File tree

10 files changed

+50
-43
lines changed

10 files changed

+50
-43
lines changed

src/librustdoc/clean/mod.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub(crate) use self::types::*;
6060
pub(crate) use self::utils::{krate, register_res, synthesize_auto_trait_and_blanket_impls};
6161
use crate::core::DocContext;
6262
use crate::formats::item_type::ItemType;
63+
use crate::rustc_attr::AttributeExt;
6364
use crate::visit_ast::Module as DocModule;
6465

6566
pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
@@ -200,7 +201,7 @@ fn generate_item_with_correct_attrs(
200201
};
201202

202203
let cfg = attrs.cfg(cx.tcx, &cx.cache.hidden_cfg);
203-
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
204+
let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
204205

205206
let name = renamed.or(Some(name));
206207
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
@@ -1030,7 +1031,7 @@ fn clean_fn_or_proc_macro<'tcx>(
10301031
/// This is needed to make it more "readable" when documenting functions using
10311032
/// `rustc_legacy_const_generics`. More information in
10321033
/// <https://github.com/rust-lang/rust/issues/83167>.
1033-
fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[ast::Attribute]) {
1034+
fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[hir::Attribute]) {
10341035
for meta_item_list in attrs
10351036
.iter()
10361037
.filter(|a| a.has_name(sym::rustc_legacy_const_generics))
@@ -2582,7 +2583,7 @@ fn get_all_import_attributes<'hir>(
25822583
import_def_id: LocalDefId,
25832584
target_def_id: DefId,
25842585
is_inline: bool,
2585-
) -> Vec<(Cow<'hir, ast::Attribute>, Option<DefId>)> {
2586+
) -> Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)> {
25862587
let mut attrs = Vec::new();
25872588
let mut first = true;
25882589
for def_id in reexport_chain(cx.tcx, import_def_id, target_def_id)
@@ -2635,9 +2636,9 @@ fn filter_doc_attr_ident(ident: Symbol, is_inline: bool) -> bool {
26352636

26362637
/// Remove attributes from `normal` that should not be inherited by `use` re-export.
26372638
/// Before calling this function, make sure `normal` is a `#[doc]` attribute.
2638-
fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
2639-
match normal.item.args {
2640-
ast::AttrArgs::Delimited(ref mut args) => {
2639+
fn filter_doc_attr(args: &mut hir::AttrArgs, is_inline: bool) {
2640+
match args {
2641+
hir::AttrArgs::Delimited(ref mut args) => {
26412642
let tokens = filter_tokens_from_list(&args.tokens, |token| {
26422643
!matches!(
26432644
token,
@@ -2655,7 +2656,7 @@ fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
26552656
});
26562657
args.tokens = TokenStream::new(tokens);
26572658
}
2658-
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {}
2659+
hir::AttrArgs::Empty | hir::AttrArgs::Eq { .. } => {}
26592660
}
26602661
}
26612662

@@ -2680,23 +2681,23 @@ fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
26802681
/// * `doc(no_inline)`
26812682
/// * `doc(hidden)`
26822683
fn add_without_unwanted_attributes<'hir>(
2683-
attrs: &mut Vec<(Cow<'hir, ast::Attribute>, Option<DefId>)>,
2684-
new_attrs: &'hir [ast::Attribute],
2684+
attrs: &mut Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)>,
2685+
new_attrs: &'hir [hir::Attribute],
26852686
is_inline: bool,
26862687
import_parent: Option<DefId>,
26872688
) {
26882689
for attr in new_attrs {
2689-
if matches!(attr.kind, ast::AttrKind::DocComment(..)) {
2690+
if matches!(attr.kind, hir::AttrKind::DocComment(..)) {
26902691
attrs.push((Cow::Borrowed(attr), import_parent));
26912692
continue;
26922693
}
26932694
let mut attr = attr.clone();
26942695
match attr.kind {
2695-
ast::AttrKind::Normal(ref mut normal) => {
2696-
if let [ident] = &*normal.item.path.segments {
2697-
let ident = ident.ident.name;
2696+
hir::AttrKind::Normal(ref mut normal) => {
2697+
if let [ident] = &*normal.path.segments {
2698+
let ident = ident.name;
26982699
if ident == sym::doc {
2699-
filter_doc_attr(normal, is_inline);
2700+
filter_doc_attr(&mut normal.args, is_inline);
27002701
attrs.push((Cow::Owned(attr), import_parent));
27012702
} else if is_inline || ident != sym::cfg {
27022703
// If it's not a `cfg()` attribute, we keep it.

src/librustdoc/clean/types.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::sync::{Arc, OnceLock as OnceCell};
55
use std::{fmt, iter};
66

77
use arrayvec::ArrayVec;
8-
use rustc_ast::MetaItemInner;
9-
use rustc_ast_pretty::pprust;
108
use rustc_attr::{ConstStability, Deprecation, Stability, StableSince};
119
use rustc_const_eval::const_eval::is_unstable_const_fn;
1210
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
@@ -47,6 +45,7 @@ use crate::formats::cache::Cache;
4745
use crate::formats::item_type::ItemType;
4846
use crate::html::render::Context;
4947
use crate::passes::collect_intra_doc_links::UrlFragment;
48+
use crate::rustc_attr::AttributeExt;
5049

5150
#[cfg(test)]
5251
mod tests;
@@ -454,14 +453,14 @@ impl Item {
454453
kind: ItemKind,
455454
cx: &mut DocContext<'_>,
456455
) -> Item {
457-
let ast_attrs = cx.tcx.get_attrs_unchecked(def_id);
456+
let hir_attrs = cx.tcx.get_attrs_unchecked(def_id);
458457

459458
Self::from_def_id_and_attrs_and_parts(
460459
def_id,
461460
name,
462461
kind,
463-
Attributes::from_ast(ast_attrs),
464-
ast_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
462+
Attributes::from_hir(hir_attrs),
463+
hir_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
465464
)
466465
}
467466

@@ -745,10 +744,10 @@ impl Item {
745744
.iter()
746745
.filter_map(|attr| {
747746
if keep_as_is {
748-
Some(pprust::attribute_to_string(attr))
747+
Some(rustc_hir_pretty::attribute_to_string(&tcx, attr))
749748
} else if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
750749
Some(
751-
pprust::attribute_to_string(attr)
750+
rustc_hir_pretty::attribute_to_string(&tcx, attr)
752751
.replace("\\\n", "")
753752
.replace('\n', "")
754753
.replace(" ", " "),
@@ -956,7 +955,7 @@ pub(crate) trait AttributesExt {
956955
type AttributeIterator<'a>: Iterator<Item = ast::MetaItemInner>
957956
where
958957
Self: 'a;
959-
type Attributes<'a>: Iterator<Item = &'a ast::Attribute>
958+
type Attributes<'a>: Iterator<Item = &'a hir::Attribute>
960959
where
961960
Self: 'a;
962961

@@ -1010,7 +1009,7 @@ pub(crate) trait AttributesExt {
10101009
// #[doc]
10111010
if attr.doc_str().is_none() && attr.has_name(sym::doc) {
10121011
// #[doc(...)]
1013-
if let Some(list) = attr.meta().as_ref().and_then(|mi| mi.meta_item_list()) {
1012+
if let Some(list) = attr.meta_item_list() {
10141013
for item in list {
10151014
// #[doc(hidden)]
10161015
if !item.has_name(sym::cfg) {
@@ -1043,7 +1042,7 @@ pub(crate) trait AttributesExt {
10431042
let mut meta = attr.meta_item().unwrap().clone();
10441043
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
10451044

1046-
if let Ok(feat_cfg) = Cfg::parse(&MetaItemInner::MetaItem(meta)) {
1045+
if let Ok(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)) {
10471046
cfg &= feat_cfg;
10481047
}
10491048
}
@@ -1054,14 +1053,14 @@ pub(crate) trait AttributesExt {
10541053
}
10551054
}
10561055

1057-
impl AttributesExt for [ast::Attribute] {
1056+
impl AttributesExt for [hir::Attribute] {
10581057
type AttributeIterator<'a> = impl Iterator<Item = ast::MetaItemInner> + 'a;
1059-
type Attributes<'a> = impl Iterator<Item = &'a ast::Attribute> + 'a;
1058+
type Attributes<'a> = impl Iterator<Item = &'a hir::Attribute> + 'a;
10601059

10611060
fn lists(&self, name: Symbol) -> Self::AttributeIterator<'_> {
10621061
self.iter()
10631062
.filter(move |attr| attr.has_name(name))
1064-
.filter_map(ast::Attribute::meta_item_list)
1063+
.filter_map(ast::attr::AttributeExt::meta_item_list)
10651064
.flatten()
10661065
}
10671066

@@ -1070,20 +1069,20 @@ impl AttributesExt for [ast::Attribute] {
10701069
}
10711070
}
10721071

1073-
impl AttributesExt for [(Cow<'_, ast::Attribute>, Option<DefId>)] {
1072+
impl AttributesExt for [(Cow<'_, hir::Attribute>, Option<DefId>)] {
10741073
type AttributeIterator<'a>
10751074
= impl Iterator<Item = ast::MetaItemInner> + 'a
10761075
where
10771076
Self: 'a;
10781077
type Attributes<'a>
1079-
= impl Iterator<Item = &'a ast::Attribute> + 'a
1078+
= impl Iterator<Item = &'a hir::Attribute> + 'a
10801079
where
10811080
Self: 'a;
10821081

10831082
fn lists(&self, name: Symbol) -> Self::AttributeIterator<'_> {
10841083
AttributesExt::iter(self)
10851084
.filter(move |attr| attr.has_name(name))
1086-
.filter_map(ast::Attribute::meta_item_list)
1085+
.filter_map(hir::Attribute::meta_item_list)
10871086
.flatten()
10881087
}
10891088

@@ -1153,7 +1152,7 @@ pub struct RenderedLink {
11531152
#[derive(Clone, Debug, Default)]
11541153
pub(crate) struct Attributes {
11551154
pub(crate) doc_strings: Vec<DocFragment>,
1156-
pub(crate) other_attrs: ast::AttrVec,
1155+
pub(crate) other_attrs: ast::AttrVec<hir::Attribute>,
11571156
}
11581157

11591158
impl Attributes {
@@ -1181,22 +1180,22 @@ impl Attributes {
11811180
self.has_doc_flag(sym::hidden)
11821181
}
11831182

1184-
pub(crate) fn from_ast(attrs: &[ast::Attribute]) -> Attributes {
1185-
Attributes::from_ast_iter(attrs.iter().map(|attr| (attr, None)), false)
1183+
pub(crate) fn from_hir(attrs: &[hir::Attribute]) -> Attributes {
1184+
Attributes::from_hir_iter(attrs.iter().map(|attr| (attr, None)), false)
11861185
}
11871186

1188-
pub(crate) fn from_ast_with_additional(
1189-
attrs: &[ast::Attribute],
1190-
(additional_attrs, def_id): (&[ast::Attribute], DefId),
1187+
pub(crate) fn from_hir_with_additional(
1188+
attrs: &[hir::Attribute],
1189+
(additional_attrs, def_id): (&[hir::Attribute], DefId),
11911190
) -> Attributes {
11921191
// Additional documentation should be shown before the original documentation.
11931192
let attrs1 = additional_attrs.iter().map(|attr| (attr, Some(def_id)));
11941193
let attrs2 = attrs.iter().map(|attr| (attr, None));
1195-
Attributes::from_ast_iter(attrs1.chain(attrs2), false)
1194+
Attributes::from_hir_iter(attrs1.chain(attrs2), false)
11961195
}
11971196

1198-
pub(crate) fn from_ast_iter<'a>(
1199-
attrs: impl Iterator<Item = (&'a ast::Attribute, Option<DefId>)>,
1197+
pub(crate) fn from_hir_iter<'a>(
1198+
attrs: impl Iterator<Item = (&'a hir::Attribute, Option<DefId>)>,
12001199
doc_only: bool,
12011200
) -> Attributes {
12021201
let (doc_strings, other_attrs) = attrs_to_doc_fragments(attrs, doc_only);

src/librustdoc/clean/utils.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::clean::{
2525
};
2626
use crate::core::DocContext;
2727
use crate::html::format::visibility_to_src_with_space;
28+
use crate::rustc_attr::AttributeExt;
2829

2930
#[cfg(test)]
3031
mod tests;
@@ -586,7 +587,7 @@ pub(crate) fn has_doc_flag(tcx: TyCtxt<'_>, did: DefId, flag: Symbol) -> bool {
586587
}
587588

588589
pub(crate) fn attrs_have_doc_flag<'a>(
589-
mut attrs: impl Iterator<Item = &'a ast::Attribute>,
590+
mut attrs: impl Iterator<Item = &'a hir::Attribute>,
590591
flag: Symbol,
591592
) -> bool {
592593
attrs

src/librustdoc/doctest.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use std::{panic, str};
1313

1414
pub(crate) use make::DocTestBuilder;
1515
pub(crate) use markdown::test as test_markdown;
16-
use rustc_ast as ast;
1716
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
1817
use rustc_errors::{ColorConfig, DiagCtxtHandle, ErrorGuaranteed, FatalError};
18+
use rustc_hir as hir;
1919
use rustc_hir::CRATE_HIR_ID;
2020
use rustc_hir::def_id::LOCAL_CRATE;
2121
use rustc_interface::interface;
@@ -32,6 +32,7 @@ use self::rust::HirCollector;
3232
use crate::config::Options as RustdocOptions;
3333
use crate::html::markdown::{ErrorCodes, Ignore, LangString, MdRelLine};
3434
use crate::lint::init_lints;
35+
use crate::rustc_attr::AttributeExt;
3536

3637
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
3738
#[derive(Clone)]
@@ -332,7 +333,7 @@ pub(crate) fn run_tests(
332333
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
333334
fn scrape_test_config(
334335
crate_name: String,
335-
attrs: &[ast::Attribute],
336+
attrs: &[hir::Attribute],
336337
args_file: PathBuf,
337338
) -> GlobalTestOptions {
338339
use rustc_ast_pretty::pprust;

src/librustdoc/doctest/make.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tracing::debug;
1818

1919
use super::GlobalTestOptions;
2020
use crate::html::markdown::LangString;
21+
use crate::rustc_attr::AttributeExt;
2122

2223
/// This struct contains information about the doctest itself which is then used to generate
2324
/// doctest source code appropriately.

src/librustdoc/doctest/rust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'tcx> HirCollector<'tcx> {
110110

111111
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
112112
// anything else, this will combine them for us.
113-
let attrs = Attributes::from_ast(ast_attrs);
113+
let attrs = Attributes::from_hir(ast_attrs);
114114
if let Some(doc) = attrs.opt_doc_value() {
115115
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
116116
self.collector.position = span;

src/librustdoc/json/conversions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::formats::FormatRenderer;
2020
use crate::formats::item_type::ItemType;
2121
use crate::json::JsonRenderer;
2222
use crate::passes::collect_intra_doc_links::UrlFragment;
23+
use crate::rustc_attr::AttributeExt;
2324

2425
impl JsonRenderer<'_> {
2526
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {

src/librustdoc/passes/collect_trait_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use super::Pass;
1212
use crate::clean::*;
1313
use crate::core::DocContext;
1414
use crate::formats::cache::Cache;
15+
use crate::rustc_attr::AttributeExt;
1516
use crate::visit::DocVisitor;
1617

1718
pub(crate) const COLLECT_TRAIT_IMPLS: Pass = Pass {

src/librustdoc/passes/strip_hidden.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::clean::{Item, ItemIdSet};
1313
use crate::core::DocContext;
1414
use crate::fold::{DocFolder, strip_item};
1515
use crate::passes::{ImplStripper, Pass};
16+
use crate::rustc_attr::AttributeExt;
1617

1718
pub(crate) const STRIP_HIDDEN: Pass = Pass {
1819
name: "strip-hidden",

src/librustdoc/visit_ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::clean::cfg::Cfg;
2121
use crate::clean::utils::{inherits_doc_hidden, should_ignore_res};
2222
use crate::clean::{AttributesExt, NestedAttributesExt, reexport_chain};
2323
use crate::core;
24+
use crate::rustc_attr::AttributeExt;
2425

2526
/// This module is used to store stuff from Rust's AST in a more convenient
2627
/// manner (and with prettier names) before cleaning.

0 commit comments

Comments
 (0)