Skip to content

Commit dda418c

Browse files
authored
Rollup merge of rust-lang#58642 - tspiteri:intra-rustdoc-prim-method, r=GuillaumeGomez
rustdoc: support methods on primitives in intra-doc links Fixes rust-lang#58598.
2 parents 2841c45 + bba0ea2 commit dda418c

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/librustdoc/clean/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,13 @@ impl Attributes {
974974
"https://doc.rust-lang.org/nightly",
975975
};
976976
// This is a primitive so the url is done "by hand".
977+
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
977978
Some((s.clone(),
978-
format!("{}{}std/primitive.{}.html",
979+
format!("{}{}std/primitive.{}.html{}",
979980
url,
980981
if !url.ends_with('/') { "/" } else { "" },
981-
fragment)))
982+
&fragment[..tail],
983+
&fragment[tail..])))
982984
} else {
983985
panic!("This isn't a primitive?!");
984986
}

src/librustdoc/passes/collect_intra_doc_links.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc::lint as lint;
22
use rustc::hir;
33
use rustc::hir::def::Def;
4+
use rustc::hir::def_id::DefId;
45
use rustc::ty;
56
use syntax;
67
use syntax::ast::{self, Ident, NodeId};
@@ -126,6 +127,17 @@ impl<'a, 'tcx, 'rcx> LinkCollector<'a, 'tcx, 'rcx> {
126127
path = name.clone();
127128
}
128129
}
130+
if let Some(prim) = is_primitive(&path, false) {
131+
let did = primitive_impl(cx, &path).ok_or(())?;
132+
return cx.tcx.associated_items(did)
133+
.find(|item| item.ident.name == item_name)
134+
.and_then(|item| match item.kind {
135+
ty::AssociatedKind::Method => Some("method"),
136+
_ => None,
137+
})
138+
.map(|out| (prim, Some(format!("{}#{}.{}", path, out, item_name))))
139+
.ok_or(());
140+
}
129141

130142
// FIXME: `with_scope` requires the `NodeId` of a module.
131143
let ty = cx.resolver.borrow_mut()
@@ -589,3 +601,26 @@ fn is_primitive(path_str: &str, is_val: bool) -> Option<Def> {
589601
PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1)
590602
}
591603
}
604+
605+
fn primitive_impl(cx: &DocContext, path_str: &str) -> Option<DefId> {
606+
let tcx = cx.tcx;
607+
match path_str {
608+
"u8" => tcx.lang_items().u8_impl(),
609+
"u16" => tcx.lang_items().u16_impl(),
610+
"u32" => tcx.lang_items().u32_impl(),
611+
"u64" => tcx.lang_items().u64_impl(),
612+
"u128" => tcx.lang_items().u128_impl(),
613+
"usize" => tcx.lang_items().usize_impl(),
614+
"i8" => tcx.lang_items().i8_impl(),
615+
"i16" => tcx.lang_items().i16_impl(),
616+
"i32" => tcx.lang_items().i32_impl(),
617+
"i64" => tcx.lang_items().i64_impl(),
618+
"i128" => tcx.lang_items().i128_impl(),
619+
"isize" => tcx.lang_items().isize_impl(),
620+
"f32" => tcx.lang_items().f32_impl(),
621+
"f64" => tcx.lang_items().f64_impl(),
622+
"str" => tcx.lang_items().str_impl(),
623+
"char" => tcx.lang_items().char_impl(),
624+
_ => None,
625+
}
626+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![deny(intra_doc_link_resolution_failure)]
2+
3+
//! A [`char`] and its [`char::len_utf8`].

0 commit comments

Comments
 (0)