Skip to content

Commit b4f1dfd

Browse files
author
Timothée Delabrouille
committed
Removed usage of Attributes in FnDecl and ExternalCrate. Relocate part of the fields in Attributes, as functions in AttributesExt.
refacto use from_def_id_and_attrs_and_parts instead of an old trick most of josha suggestions + check if def_id is not fake before using it in a query Removed usage of Attributes in FnDecl and ExternalCrate. Relocate part of the Attributes fields as functions in AttributesExt.
1 parent 5da10c0 commit b4f1dfd

File tree

11 files changed

+64
-53
lines changed

11 files changed

+64
-53
lines changed

src/librustdoc/clean/inline.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ crate fn try_inline(
124124
let attrs = box merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
125125

126126
cx.inlined.insert(did);
127-
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
128-
ret.push(clean::Item { attrs, ..what_rustc_thinks });
127+
ret.push(clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, attrs, cx));
129128
Some(ret)
130129
}
131130

src/librustdoc/clean/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
8484
}
8585

8686
impl Clean<ExternalCrate> for CrateNum {
87-
fn clean(&self, cx: &mut DocContext<'_>) -> ExternalCrate {
88-
let root = DefId { krate: *self, index: CRATE_DEF_INDEX };
89-
ExternalCrate { crate_num: *self, attrs: cx.tcx.get_attrs(root).clean(cx) }
87+
fn clean(&self, _cx: &mut DocContext<'_>) -> ExternalCrate {
88+
ExternalCrate { crate_num: *self }
9089
}
9190
}
9291

@@ -850,7 +849,6 @@ where
850849
inputs: (self.0.inputs, self.1).clean(cx),
851850
output: self.0.output.clean(cx),
852851
c_variadic: self.0.c_variadic,
853-
attrs: Attributes::default(),
854852
}
855853
}
856854
}
@@ -862,7 +860,6 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
862860

863861
FnDecl {
864862
output: Return(sig.skip_binder().output().clean(cx)),
865-
attrs: Attributes::default(),
866863
c_variadic: sig.skip_binder().c_variadic,
867864
inputs: Arguments {
868865
values: sig

src/librustdoc/clean/types.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ crate struct TraitWithExtraInfo {
7373
#[derive(Clone, Debug)]
7474
crate struct ExternalCrate {
7575
crate crate_num: CrateNum,
76-
crate attrs: Attributes,
7776
}
7877

7978
impl ExternalCrate {
@@ -663,12 +662,35 @@ impl<'a> Iterator for ListAttributesIter<'a> {
663662
crate trait AttributesExt {
664663
/// Finds an attribute as List and returns the list of attributes nested inside.
665664
fn lists(&self, name: Symbol) -> ListAttributesIter<'_>;
665+
666+
fn span(&self) -> Option<rustc_span::Span>;
667+
668+
fn inner_docs(&self) -> bool;
669+
670+
fn other_attrs(&self) -> Vec<ast::Attribute>;
666671
}
667672

668673
impl AttributesExt for [ast::Attribute] {
669674
fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
670675
ListAttributesIter { attrs: self.iter(), current_list: Vec::new().into_iter(), name }
671676
}
677+
678+
/// Return the span of the first doc-comment, if it exists.
679+
fn span(&self) -> Option<rustc_span::Span> {
680+
self.iter().find(|attr| attr.doc_str().is_some()).map(|attr| attr.span)
681+
}
682+
683+
/// Returns whether the first doc-comment is an inner attribute.
684+
///
685+
//// If there are no doc-comments, return true.
686+
/// FIXME(#78591): Support both inner and outer attributes on the same item.
687+
fn inner_docs(&self) -> bool {
688+
self.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == AttrStyle::Inner)
689+
}
690+
691+
fn other_attrs(&self) -> Vec<ast::Attribute> {
692+
self.iter().filter(|attr| attr.doc_str().is_none()).cloned().collect()
693+
}
672694
}
673695

674696
crate trait NestedAttributesExt {
@@ -778,8 +800,6 @@ crate struct Attributes {
778800
crate doc_strings: Vec<DocFragment>,
779801
crate other_attrs: Vec<ast::Attribute>,
780802
crate cfg: Option<Arc<Cfg>>,
781-
crate span: Option<rustc_span::Span>,
782-
crate inner_docs: bool,
783803
}
784804

785805
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
@@ -811,6 +831,10 @@ pub struct RenderedLink {
811831
}
812832

813833
impl Attributes {
834+
crate fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
835+
self.other_attrs.lists(name)
836+
}
837+
814838
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
815839
crate fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
816840
use rustc_ast::NestedMetaItem::MetaItem;
@@ -895,7 +919,6 @@ impl Attributes {
895919
additional_attrs: Option<(&[ast::Attribute], DefId)>,
896920
) -> Attributes {
897921
let mut doc_strings: Vec<DocFragment> = vec![];
898-
let mut sp = None;
899922
let mut cfg = Cfg::True;
900923
let mut doc_line = 0;
901924

@@ -940,9 +963,6 @@ impl Attributes {
940963

941964
doc_strings.push(frag);
942965

943-
if sp.is_none() {
944-
sp = Some(attr.span);
945-
}
946966
None
947967
} else {
948968
if attr.has_name(sym::doc) {
@@ -1001,17 +1021,10 @@ impl Attributes {
10011021
}
10021022
}
10031023

1004-
let inner_docs = attrs
1005-
.iter()
1006-
.find(|a| a.doc_str().is_some())
1007-
.map_or(true, |a| a.style == AttrStyle::Inner);
1008-
10091024
Attributes {
10101025
doc_strings,
10111026
other_attrs,
10121027
cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
1013-
span: sp,
1014-
inner_docs,
10151028
}
10161029
}
10171030

@@ -1079,7 +1092,6 @@ impl PartialEq for Attributes {
10791092
fn eq(&self, rhs: &Self) -> bool {
10801093
self.doc_strings == rhs.doc_strings
10811094
&& self.cfg == rhs.cfg
1082-
&& self.span == rhs.span
10831095
&& self
10841096
.other_attrs
10851097
.iter()
@@ -1094,19 +1106,12 @@ impl Hash for Attributes {
10941106
fn hash<H: Hasher>(&self, hasher: &mut H) {
10951107
self.doc_strings.hash(hasher);
10961108
self.cfg.hash(hasher);
1097-
self.span.hash(hasher);
10981109
for attr in &self.other_attrs {
10991110
attr.id.hash(hasher);
11001111
}
11011112
}
11021113
}
11031114

1104-
impl AttributesExt for Attributes {
1105-
fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
1106-
self.other_attrs.lists(name)
1107-
}
1108-
}
1109-
11101115
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
11111116
crate enum GenericBound {
11121117
TraitBound(PolyTrait, hir::TraitBoundModifier),
@@ -1269,7 +1274,6 @@ crate struct FnDecl {
12691274
crate inputs: Arguments,
12701275
crate output: FnRetTy,
12711276
crate c_variadic: bool,
1272-
crate attrs: Attributes,
12731277
}
12741278

12751279
impl FnDecl {

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::rc::Rc;
3232

3333
use crate::clean;
3434
use crate::clean::inline::build_external_trait;
35-
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
35+
use crate::clean::{TraitWithExtraInfo, MAX_DEF_IDX};
3636
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3737
use crate::formats::cache::Cache;
3838
use crate::passes::{self, Condition::*, ConditionalPass};

src/librustdoc/doctest.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::str;
2626
use std::sync::atomic::{AtomicUsize, Ordering};
2727
use std::sync::{Arc, Mutex};
2828

29-
use crate::clean::Attributes;
29+
use crate::clean::{types::AttributesExt, Attributes};
3030
use crate::config::Options;
3131
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
3232
use crate::lint::init_lints;
@@ -1092,8 +1092,9 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
10921092
sp: Span,
10931093
nested: F,
10941094
) {
1095-
let attrs = self.tcx.hir().attrs(hir_id);
1096-
let mut attrs = Attributes::from_ast(self.sess.diagnostic(), attrs, None);
1095+
let ast_attrs = self.tcx.hir().attrs(hir_id);
1096+
1097+
let mut attrs = Attributes::from_ast(self.sess.diagnostic(), ast_attrs, None);
10971098
if let Some(ref cfg) = attrs.cfg {
10981099
if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
10991100
return;
@@ -1110,8 +1111,8 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
11101111
// anything else, this will combine them for us.
11111112
if let Some(doc) = attrs.collapsed_doc_value() {
11121113
// Use the outermost invocation, so that doctest names come from where the docs were written.
1113-
let span = attrs
1114-
.span
1114+
let span = ast_attrs
1115+
.span()
11151116
.map(|span| span.ctxt().outer_expn().expansion_cause().unwrap_or(span))
11161117
.unwrap_or(DUMMY_SP);
11171118
self.collector.set_position(span);

src/librustdoc/formats/cache.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ impl Cache {
164164
};
165165
let name = e.name(tcx);
166166
let extern_url = extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
167-
self.extern_locations
168-
.insert(n, (name, src_root, extern_location(e, extern_url, &dst, tcx)));
169-
170167
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
168+
self.extern_locations.insert(
169+
n,
170+
(name, src_root, extern_location(e, extern_url, tcx.get_attrs(did), &dst, tcx)),
171+
);
172+
171173
self.external_paths.insert(did, (vec![name.to_string()], ItemType::Module));
172174
}
173175

src/librustdoc/html/render/cache.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use std::collections::BTreeMap;
22
use std::path::Path;
33

4+
use rustc_ast::ast;
45
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
56
use rustc_middle::ty::TyCtxt;
67
use rustc_span::symbol::{sym, Symbol};
78
use serde::ser::{Serialize, SerializeStruct, Serializer};
89

10+
use crate::clean;
911
use crate::clean::types::{
10-
FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
12+
AttributesExt, FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, TypeKind,
13+
WherePredicate,
1114
};
12-
use crate::clean::{self, AttributesExt};
1315
use crate::formats::cache::Cache;
1416
use crate::formats::item_type::ItemType;
1517
use crate::html::markdown::short_markdown_summary;
@@ -30,6 +32,7 @@ crate enum ExternalLocation {
3032
crate fn extern_location(
3133
e: &clean::ExternalCrate,
3234
extern_url: Option<&str>,
35+
ast_attrs: &[ast::Attribute],
3336
dst: &Path,
3437
tcx: TyCtxt<'_>,
3538
) -> ExternalLocation {
@@ -50,7 +53,7 @@ crate fn extern_location(
5053

5154
// Failing that, see if there's an attribute specifying where to find this
5255
// external crate
53-
e.attrs
56+
ast_attrs
5457
.lists(sym::doc)
5558
.filter(|a| a.has_name(sym::html_root_url))
5659
.filter_map(|a| a.value_str())

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::print_item::{full_path, item_path, print_item};
1818
use super::write_shared::write_shared;
1919
use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS};
2020

21-
use crate::clean::{self, AttributesExt};
21+
use crate::clean;
2222
use crate::config::RenderOptions;
2323
use crate::docfs::{DocFS, PathError};
2424
use crate::error::Error;

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
421421

422422
impl FromWithTcx<clean::FnDecl> for FnDecl {
423423
fn from_tcx(decl: clean::FnDecl, tcx: TyCtxt<'_>) -> Self {
424-
let clean::FnDecl { inputs, output, c_variadic, attrs: _ } = decl;
424+
let clean::FnDecl { inputs, output, c_variadic } = decl;
425425
FnDecl {
426426
inputs: inputs
427427
.values

src/librustdoc/passes/collect_intra_doc_links.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [RFC 1946]: https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
44
5+
use clean::AttributesExt;
56
use rustc_ast as ast;
67
use rustc_data_structures::{fx::FxHashMap, stable_set::FxHashSet};
78
use rustc_errors::{Applicability, DiagnosticBuilder};
@@ -853,7 +854,12 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
853854
}
854855
});
855856

856-
if item.is_mod() && item.attrs.inner_docs {
857+
let inner_docs = match self_id {
858+
Some(did) => self.cx.tcx.get_attrs(did).inner_docs(),
859+
None => false,
860+
};
861+
862+
if item.is_mod() && inner_docs {
857863
self.mod_ids.push(item.def_id);
858864
}
859865

@@ -880,7 +886,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
880886
}
881887

882888
Some(if item.is_mod() {
883-
if !item.attrs.inner_docs {
889+
if !inner_docs {
884890
self.mod_ids.push(item.def_id);
885891
}
886892

@@ -1050,6 +1056,8 @@ impl LinkCollector<'_, '_> {
10501056
};
10511057
let mut path_str = &*path_str;
10521058

1059+
let inner_docs = self.cx.tcx.get_attrs(item.def_id).inner_docs();
1060+
10531061
// In order to correctly resolve intra-doc links we need to
10541062
// pick a base AST node to work from. If the documentation for
10551063
// this module came from an inner comment (//!) then we anchor
@@ -1061,11 +1069,8 @@ impl LinkCollector<'_, '_> {
10611069
// we've already pushed this node onto the resolution stack but
10621070
// for outer comments we explicitly try and resolve against the
10631071
// parent_node first.
1064-
let base_node = if item.is_mod() && item.attrs.inner_docs {
1065-
self.mod_ids.last().copied()
1066-
} else {
1067-
parent_node
1068-
};
1072+
let base_node =
1073+
if item.is_mod() && inner_docs { self.mod_ids.last().copied() } else { parent_node };
10691074

10701075
let mut module_id = if let Some(id) = base_node {
10711076
id

src/librustdoc/passes/strip_hidden.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_hir::def_id::DefIdSet;
22
use rustc_span::symbol::sym;
33
use std::mem;
44

5-
use crate::clean::Item;
6-
use crate::clean::{self, AttributesExt, NestedAttributesExt};
5+
use crate::clean;
6+
use crate::clean::{Item, NestedAttributesExt};
77
use crate::core::DocContext;
88
use crate::fold::{DocFolder, StripItem};
99
use crate::passes::{ImplStripper, Pass};

0 commit comments

Comments
 (0)