Skip to content

Commit 77d164d

Browse files
committed
rustdoc: Index inherent methods on primitives
The set of types which can have an inherent impl changed slightly and rustdoc just needed to catch up to understand what it means to see a `impl str`! Closes #23511
1 parent 1b568ba commit 77d164d

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

src/librustdoc/clean/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,21 @@ pub enum TypeKind {
14281428
TypeTypedef,
14291429
}
14301430

1431+
impl Type {
1432+
pub fn primitive_type(&self) -> Option<PrimitiveType> {
1433+
match *self {
1434+
Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
1435+
Vector(..) | BorrowedRef{ type_: box Vector(..), .. } => Some(Slice),
1436+
FixedVector(..) | BorrowedRef { type_: box FixedVector(..), .. } => {
1437+
Some(Array)
1438+
}
1439+
Tuple(..) => Some(PrimitiveTuple),
1440+
RawPointer(..) => Some(PrimitiveRawPointer),
1441+
_ => None,
1442+
}
1443+
}
1444+
}
1445+
14311446
impl PrimitiveType {
14321447
fn from_str(s: &str) -> Option<PrimitiveType> {
14331448
match s {

src/librustdoc/html/render.rs

+17-42
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,16 @@ impl DocFolder for Cache {
10251025
self.parent_stack.push(did);
10261026
true
10271027
}
1028-
_ => false
1028+
ref t => {
1029+
match t.primitive_type() {
1030+
Some(prim) => {
1031+
let did = ast_util::local_def(prim.to_node_id());
1032+
self.parent_stack.push(did);
1033+
true
1034+
}
1035+
_ => false,
1036+
}
1037+
}
10291038
}
10301039
}
10311040
_ => false
@@ -1037,11 +1046,6 @@ impl DocFolder for Cache {
10371046
Some(item) => {
10381047
match item {
10391048
clean::Item{ attrs, inner: clean::ImplItem(i), .. } => {
1040-
use clean::{Primitive, Vector, ResolvedPath, BorrowedRef};
1041-
use clean::PrimitiveType::{Array, Slice, PrimitiveTuple};
1042-
use clean::PrimitiveType::{PrimitiveRawPointer};
1043-
use clean::{FixedVector, Tuple, RawPointer};
1044-
10451049
// extract relevant documentation for this impl
10461050
let dox = match attrs.into_iter().find(|a| {
10471051
match *a {
@@ -1059,47 +1063,18 @@ impl DocFolder for Cache {
10591063
// Figure out the id of this impl. This may map to a
10601064
// primitive rather than always to a struct/enum.
10611065
let did = match i.for_ {
1062-
ResolvedPath { did, .. } |
1063-
BorrowedRef {
1064-
type_: box ResolvedPath { did, .. }, ..
1066+
clean::ResolvedPath { did, .. } |
1067+
clean::BorrowedRef {
1068+
type_: box clean::ResolvedPath { did, .. }, ..
10651069
} => {
10661070
Some(did)
10671071
}
10681072

1069-
// References to primitives are picked up as well to
1070-
// recognize implementations for &str, this may not
1071-
// be necessary in a DST world.
1072-
Primitive(p) |
1073-
BorrowedRef { type_: box Primitive(p), ..} =>
1074-
{
1075-
Some(ast_util::local_def(p.to_node_id()))
1073+
ref t => {
1074+
t.primitive_type().map(|p| {
1075+
ast_util::local_def(p.to_node_id())
1076+
})
10761077
}
1077-
1078-
FixedVector(..) |
1079-
BorrowedRef { type_: box FixedVector(..), .. } =>
1080-
{
1081-
Some(ast_util::local_def(Array.to_node_id()))
1082-
}
1083-
1084-
// In a DST world, we may only need Vector, but for
1085-
// now we also pick up borrowed references
1086-
Vector(..) |
1087-
BorrowedRef{ type_: box Vector(..), .. } =>
1088-
{
1089-
Some(ast_util::local_def(Slice.to_node_id()))
1090-
}
1091-
1092-
Tuple(..) => {
1093-
let id = PrimitiveTuple.to_node_id();
1094-
Some(ast_util::local_def(id))
1095-
}
1096-
1097-
RawPointer(..) => {
1098-
let id = PrimitiveRawPointer.to_node_id();
1099-
Some(ast_util::local_def(id))
1100-
}
1101-
1102-
_ => None,
11031078
};
11041079

11051080
if let Some(did) = did {

src/test/rustdoc/issue-23511.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 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(no_std, lang_items, core)]
12+
#![no_std]
13+
14+
extern crate core;
15+
16+
pub mod str {
17+
#![doc(primitive = "str")]
18+
19+
#[lang = "str"]
20+
impl str {
21+
// @has search-index.js foo
22+
pub fn foo(&self) {}
23+
}
24+
}

0 commit comments

Comments
 (0)