Skip to content

Commit 1da7684

Browse files
Improve code and add more comments
1 parent 06238bd commit 1da7684

File tree

5 files changed

+36
-58
lines changed

5 files changed

+36
-58
lines changed

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ pub struct CfgHideShow {
510510
pub values: ThinVec<CfgInfo>,
511511
}
512512

513-
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
513+
#[derive(Clone, Debug, Default, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
514514
pub struct DocAttribute {
515515
pub aliases: FxIndexMap<Symbol, Span>,
516516
pub hidden: Option<Span>,
@@ -546,34 +546,6 @@ pub struct DocAttribute {
546546
pub no_crate_inject: Option<Span>,
547547
}
548548

549-
impl Default for DocAttribute {
550-
fn default() -> Self {
551-
Self {
552-
aliases: FxIndexMap::default(),
553-
hidden: None,
554-
inline: ThinVec::new(),
555-
cfg: ThinVec::new(),
556-
auto_cfg: ThinVec::new(),
557-
auto_cfg_change: ThinVec::new(),
558-
fake_variadic: None,
559-
keyword: None,
560-
attribute: None,
561-
masked: None,
562-
notable_trait: None,
563-
search_unbox: None,
564-
html_favicon_url: None,
565-
html_logo_url: None,
566-
html_playground_url: None,
567-
html_root_url: None,
568-
html_no_source: None,
569-
issue_tracker_base_url: None,
570-
rust_logo: None,
571-
test_attrs: ThinVec::new(),
572-
no_crate_inject: None,
573-
}
574-
}
575-
}
576-
577549
/// Represents parsed *built-in* inert attributes.
578550
///
579551
/// ## Overview

src/librustdoc/clean/cfg.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ fn is_all_cfg(cfg: &CfgEntry) -> bool {
5959
}
6060
}
6161

62-
fn strip_hidden(cfg: &CfgEntry, hidden: &FxHashSet<SimpleCfg>) -> Option<CfgEntry> {
62+
fn strip_hidden(cfg: &CfgEntry, hidden: &FxHashSet<NameValueCfg>) -> Option<CfgEntry> {
6363
match cfg {
6464
CfgEntry::Bool(..) => Some(cfg.clone()),
6565
CfgEntry::NameValue { .. } => {
66-
if !hidden.contains(&SimpleCfg::from(cfg)) {
66+
if !hidden.contains(&NameValueCfg::from(cfg)) {
6767
Some(cfg.clone())
6868
} else {
6969
None
@@ -109,7 +109,7 @@ impl Cfg {
109109
/// Parses a `MetaItemInner` into a `Cfg`.
110110
fn parse_nested(
111111
nested_cfg: &MetaItemInner,
112-
exclude: &FxHashSet<SimpleCfg>,
112+
exclude: &FxHashSet<NameValueCfg>,
113113
) -> Result<Option<Cfg>, InvalidCfgError> {
114114
match nested_cfg {
115115
MetaItemInner::MetaItem(cfg) => Cfg::parse_without(cfg, exclude),
@@ -124,7 +124,7 @@ impl Cfg {
124124

125125
fn parse_without(
126126
cfg: &MetaItem,
127-
exclude: &FxHashSet<SimpleCfg>,
127+
exclude: &FxHashSet<NameValueCfg>,
128128
) -> Result<Option<Cfg>, InvalidCfgError> {
129129
let name = match cfg.ident() {
130130
Some(ident) => ident.name,
@@ -137,7 +137,7 @@ impl Cfg {
137137
};
138138
match cfg.kind {
139139
MetaItemKind::Word => {
140-
if exclude.contains(&SimpleCfg::new(name)) {
140+
if exclude.contains(&NameValueCfg::new(name)) {
141141
Ok(None)
142142
} else {
143143
Ok(Some(Cfg(CfgEntry::NameValue {
@@ -150,7 +150,7 @@ impl Cfg {
150150
}
151151
MetaItemKind::NameValue(ref lit) => match lit.kind {
152152
LitKind::Str(value, _) => {
153-
if exclude.contains(&SimpleCfg::new_value(name, value)) {
153+
if exclude.contains(&NameValueCfg::new_value(name, value)) {
154154
Ok(None)
155155
} else {
156156
Ok(Some(Cfg(CfgEntry::NameValue {
@@ -666,12 +666,12 @@ impl fmt::Display for Display<'_> {
666666
}
667667

668668
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
669-
struct SimpleCfg {
669+
struct NameValueCfg {
670670
name: Symbol,
671671
value: Option<Symbol>,
672672
}
673673

674-
impl SimpleCfg {
674+
impl NameValueCfg {
675675
fn new(name: Symbol) -> Self {
676676
Self { name, value: None }
677677
}
@@ -681,18 +681,18 @@ impl SimpleCfg {
681681
}
682682
}
683683

684-
impl<'a> From<&'a CfgEntry> for SimpleCfg {
684+
impl<'a> From<&'a CfgEntry> for NameValueCfg {
685685
fn from(cfg: &'a CfgEntry) -> Self {
686686
match cfg {
687687
CfgEntry::NameValue { name, value, .. } => {
688-
SimpleCfg { name: *name, value: (*value).map(|(v, _)| v) }
688+
NameValueCfg { name: *name, value: (*value).map(|(v, _)| v) }
689689
}
690-
_ => SimpleCfg { name: sym::empty, value: None },
690+
_ => NameValueCfg { name: sym::empty, value: None },
691691
}
692692
}
693693
}
694694

695-
impl<'a> From<&'a attrs::CfgInfo> for SimpleCfg {
695+
impl<'a> From<&'a attrs::CfgInfo> for NameValueCfg {
696696
fn from(cfg: &'a attrs::CfgInfo) -> Self {
697697
Self { name: cfg.name, value: cfg.value.map(|(value, _)| value) }
698698
}
@@ -703,7 +703,7 @@ impl<'a> From<&'a attrs::CfgInfo> for SimpleCfg {
703703
pub(crate) struct CfgInfo {
704704
/// List of currently active `doc(auto_cfg(hide(...)))` cfgs, minus currently active
705705
/// `doc(auto_cfg(show(...)))` cfgs.
706-
hidden_cfg: FxHashSet<SimpleCfg>,
706+
hidden_cfg: FxHashSet<NameValueCfg>,
707707
/// Current computed `cfg`. Each time we enter a new item, this field is updated as well while
708708
/// taking into account the `hidden_cfg` information.
709709
current_cfg: Cfg,
@@ -719,9 +719,9 @@ impl Default for CfgInfo {
719719
fn default() -> Self {
720720
Self {
721721
hidden_cfg: FxHashSet::from_iter([
722-
SimpleCfg::new(sym::test),
723-
SimpleCfg::new(sym::doc),
724-
SimpleCfg::new(sym::doctest),
722+
NameValueCfg::new(sym::test),
723+
NameValueCfg::new(sym::doc),
724+
NameValueCfg::new(sym::doctest),
725725
]),
726726
current_cfg: Cfg(CfgEntry::Bool(true, DUMMY_SP)),
727727
auto_cfg_active: true,
@@ -761,7 +761,7 @@ fn handle_auto_cfg_hide_show(
761761
new_hide_attrs: &mut FxHashMap<(Symbol, Option<Symbol>), rustc_span::Span>,
762762
) {
763763
for value in &attr.values {
764-
let simple = SimpleCfg::from(value);
764+
let simple = NameValueCfg::from(value);
765765
if attr.kind == HideOrShow::Show {
766766
if let Some(span) = new_hide_attrs.get(&(simple.name, simple.value)) {
767767
show_hide_show_conflict_error(tcx, attr_span, *span);

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,34 +229,28 @@ impl ExternalCrate {
229229
}
230230

231231
pub(crate) fn keywords(&self, tcx: TyCtxt<'_>) -> impl Iterator<Item = (DefId, Symbol)> {
232-
self.retrieve_keywords_or_documented_attributes(tcx, true)
232+
self.retrieve_keywords_or_documented_attributes(tcx, |d| d.keyword.map(|(v, _)| v))
233233
}
234234
pub(crate) fn documented_attributes(
235235
&self,
236236
tcx: TyCtxt<'_>,
237237
) -> impl Iterator<Item = (DefId, Symbol)> {
238-
self.retrieve_keywords_or_documented_attributes(tcx, false)
238+
self.retrieve_keywords_or_documented_attributes(tcx, |d| d.attribute.map(|(v, _)| v))
239239
}
240240

241-
fn retrieve_keywords_or_documented_attributes(
241+
fn retrieve_keywords_or_documented_attributes<F: Fn(&DocAttribute) -> Option<Symbol>>(
242242
&self,
243243
tcx: TyCtxt<'_>,
244-
look_for_keyword: bool,
244+
callback: F,
245245
) -> impl Iterator<Item = (DefId, Symbol)> {
246246
let as_target = move |did: DefId, tcx: TyCtxt<'_>| -> Option<(DefId, Symbol)> {
247247
tcx.get_all_attrs(did)
248248
.iter()
249249
.find_map(|attr| match attr {
250-
Attribute::Parsed(AttributeKind::Doc(d)) => {
251-
if look_for_keyword {
252-
d.keyword
253-
} else {
254-
d.attribute
255-
}
256-
}
250+
Attribute::Parsed(AttributeKind::Doc(d)) => callback(d),
257251
_ => None,
258252
})
259-
.map(|(value, _)| (did, value))
253+
.map(|value| (did, value))
260254
};
261255
self.mapped_root_modules(tcx, as_target)
262256
}

src/librustdoc/doctest/rust.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,22 @@ impl HirCollector<'_> {
143143
if let TokenTree::Ident(i) = token {
144144
let i = i.to_string();
145145
let peek = iter.peek();
146+
// From this ident, we can have things like:
147+
//
148+
// * Group: `allow(...)`
149+
// * Name/value: `crate_name = "..."`
150+
// * Tokens: `html_no_url`
151+
//
152+
// So we peek next element to know what case we are in.
146153
match peek {
147154
Some(TokenTree::Group(g)) => {
148155
let g = g.to_string();
149156
iter.next();
150157
// Add the additional attributes to the global_crate_attrs vector
151158
self.collector.global_crate_attrs.push(format!("{i}{g}"));
152159
}
160+
// If next item is `=`, it means it's a name value so we will need
161+
// to get the value as well.
153162
Some(TokenTree::Punct(p)) if p.as_char() == '=' => {
154163
let p = p.to_string();
155164
iter.next();

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
6868
while let Some(did) = parent {
6969
attr_buf.extend(tcx.get_all_attrs(did).iter().filter_map(|attr| match attr {
7070
Attribute::Parsed(AttributeKind::Doc(d)) if !d.cfg.is_empty() => {
71+
// The only doc attributes we're interested into for trait impls are the
72+
// `cfg`s for the `doc_cfg` feature. So we create a new empty `DocAttribute`
73+
// and then only clone the actual `DocAttribute::cfg` field.
7174
let mut new_attr = DocAttribute::default();
7275
new_attr.cfg = d.cfg.clone();
7376
Some(Attribute::Parsed(AttributeKind::Doc(Box::new(new_attr))))

0 commit comments

Comments
 (0)