Skip to content

Commit 1d04201

Browse files
authored
Auto merge of #36078 - ollie27:rustdoc_search_assocconst, r=alexcrichton
rustdoc: Fix associated consts in search results Associated consts can appear in none trait impls so need to be treated like methods when generating the search index. Fixes #36031
2 parents e1d0de8 + b56d61c commit 1d04201

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/librustdoc/html/render.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub struct Cache {
272272
// then the fully qualified name of the structure isn't presented in `paths`
273273
// yet when its implementation methods are being indexed. Caches such methods
274274
// and their parent id here and indexes them at the end of crate parsing.
275-
orphan_methods: Vec<(DefId, clean::Item)>,
275+
orphan_impl_items: Vec<(DefId, clean::Item)>,
276276
}
277277

278278
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
@@ -529,7 +529,7 @@ pub fn run(mut krate: clean::Crate,
529529
seen_mod: false,
530530
stripped_mod: false,
531531
access_levels: krate.access_levels.clone(),
532-
orphan_methods: Vec::new(),
532+
orphan_impl_items: Vec::new(),
533533
traits: mem::replace(&mut krate.external_traits, FnvHashMap()),
534534
deref_trait_did: deref_trait_did,
535535
typarams: external_typarams,
@@ -581,12 +581,12 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
581581
let mut crate_paths = Vec::<Json>::new();
582582

583583
let Cache { ref mut search_index,
584-
ref orphan_methods,
584+
ref orphan_impl_items,
585585
ref mut paths, .. } = *cache;
586586

587-
// Attach all orphan methods to the type's definition if the type
587+
// Attach all orphan items to the type's definition if the type
588588
// has since been learned.
589-
for &(did, ref item) in orphan_methods {
589+
for &(did, ref item) in orphan_impl_items {
590590
if let Some(&(ref fqp, _)) = paths.get(&did) {
591591
search_index.push(IndexItem {
592592
ty: item_type(item),
@@ -1024,23 +1024,22 @@ impl DocFolder for Cache {
10241024

10251025
// Index this method for searching later on
10261026
if let Some(ref s) = item.name {
1027-
let (parent, is_method) = match item.inner {
1027+
let (parent, is_inherent_impl_item) = match item.inner {
10281028
clean::StrippedItem(..) => ((None, None), false),
10291029
clean::AssociatedConstItem(..) |
10301030
clean::TypedefItem(_, true) if self.parent_is_trait_impl => {
10311031
// skip associated items in trait impls
10321032
((None, None), false)
10331033
}
10341034
clean::AssociatedTypeItem(..) |
1035-
clean::AssociatedConstItem(..) |
10361035
clean::TyMethodItem(..) |
10371036
clean::StructFieldItem(..) |
10381037
clean::VariantItem(..) => {
10391038
((Some(*self.parent_stack.last().unwrap()),
10401039
Some(&self.stack[..self.stack.len() - 1])),
10411040
false)
10421041
}
1043-
clean::MethodItem(..) => {
1042+
clean::MethodItem(..) | clean::AssociatedConstItem(..) => {
10441043
if self.parent_stack.is_empty() {
10451044
((None, None), false)
10461045
} else {
@@ -1066,7 +1065,7 @@ impl DocFolder for Cache {
10661065
};
10671066

10681067
match parent {
1069-
(parent, Some(path)) if is_method || (!self.stripped_mod) => {
1068+
(parent, Some(path)) if is_inherent_impl_item || (!self.stripped_mod) => {
10701069
debug_assert!(!item.is_stripped());
10711070

10721071
// A crate has a module at its root, containing all items,
@@ -1084,10 +1083,10 @@ impl DocFolder for Cache {
10841083
});
10851084
}
10861085
}
1087-
(Some(parent), None) if is_method => {
1086+
(Some(parent), None) if is_inherent_impl_item => {
10881087
// We have a parent, but we don't know where they're
10891088
// defined yet. Wait for later to index this item.
1090-
self.orphan_methods.push((parent, item.clone()));
1089+
self.orphan_impl_items.push((parent, item.clone()));
10911090
}
10921091
_ => {}
10931092
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(associated_consts)]
12+
13+
pub trait Foo {
14+
const FOO: usize;
15+
}
16+
17+
pub struct Bar;
18+
19+
impl Bar {
20+
pub const BAR: usize = 3;
21+
}

src/test/rustdoc/issue-36031.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-36031.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
#![crate_name = "foo"]
16+
17+
extern crate issue_36031;
18+
19+
pub use issue_36031::Foo;

0 commit comments

Comments
 (0)