Skip to content

Commit b56d61c

Browse files
committed
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.
1 parent acd3f79 commit b56d61c

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
@@ -271,7 +271,7 @@ pub struct Cache {
271271
// then the fully qualified name of the structure isn't presented in `paths`
272272
// yet when its implementation methods are being indexed. Caches such methods
273273
// and their parent id here and indexes them at the end of crate parsing.
274-
orphan_methods: Vec<(DefId, clean::Item)>,
274+
orphan_impl_items: Vec<(DefId, clean::Item)>,
275275
}
276276

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

582582
let Cache { ref mut search_index,
583-
ref orphan_methods,
583+
ref orphan_impl_items,
584584
ref mut paths, .. } = *cache;
585585

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

10241024
// Index this method for searching later on
10251025
if let Some(ref s) = item.name {
1026-
let (parent, is_method) = match item.inner {
1026+
let (parent, is_inherent_impl_item) = match item.inner {
10271027
clean::StrippedItem(..) => ((None, None), false),
10281028
clean::AssociatedConstItem(..) |
10291029
clean::TypedefItem(_, true) if self.parent_is_trait_impl => {
10301030
// skip associated items in trait impls
10311031
((None, None), false)
10321032
}
10331033
clean::AssociatedTypeItem(..) |
1034-
clean::AssociatedConstItem(..) |
10351034
clean::TyMethodItem(..) |
10361035
clean::StructFieldItem(..) |
10371036
clean::VariantItem(..) => {
10381037
((Some(*self.parent_stack.last().unwrap()),
10391038
Some(&self.stack[..self.stack.len() - 1])),
10401039
false)
10411040
}
1042-
clean::MethodItem(..) => {
1041+
clean::MethodItem(..) | clean::AssociatedConstItem(..) => {
10431042
if self.parent_stack.is_empty() {
10441043
((None, None), false)
10451044
} else {
@@ -1064,7 +1063,7 @@ impl DocFolder for Cache {
10641063
};
10651064

10661065
match parent {
1067-
(parent, Some(path)) if is_method || (!self.stripped_mod) => {
1066+
(parent, Some(path)) if is_inherent_impl_item || (!self.stripped_mod) => {
10681067
debug_assert!(!item.is_stripped());
10691068

10701069
// A crate has a module at its root, containing all items,
@@ -1082,10 +1081,10 @@ impl DocFolder for Cache {
10821081
});
10831082
}
10841083
}
1085-
(Some(parent), None) if is_method => {
1084+
(Some(parent), None) if is_inherent_impl_item => {
10861085
// We have a parent, but we don't know where they're
10871086
// defined yet. Wait for later to index this item.
1088-
self.orphan_methods.push((parent, item.clone()));
1087+
self.orphan_impl_items.push((parent, item.clone()));
10891088
}
10901089
_ => {}
10911090
}
+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)