Skip to content

Commit 1b74f04

Browse files
committed
Use impls for intra doc links as well
1 parent 3b1d2e3 commit 1b74f04

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ impl PrimitiveType {
12671267
}
12681268
}
12691269

1270-
pub fn impls(&self, tcx: TyCtxt<'_>) -> &SmallVec<[DefId; 4]> {
1270+
pub fn impls(&self, tcx: TyCtxt<'_>) -> &'static SmallVec<[DefId; 4]> {
12711271
Self::all_impls(tcx).get(self).expect("missing impl for primitive type")
12721272
}
12731273

src/librustdoc/passes/collect_intra_doc_links.rs

+18-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_span::hygiene::MacroKind;
1616
use rustc_span::symbol::Ident;
1717
use rustc_span::symbol::Symbol;
1818
use rustc_span::DUMMY_SP;
19+
use smallvec::SmallVec;
1920

2021
use std::cell::Cell;
2122
use std::ops::Range;
@@ -270,18 +271,21 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
270271
.ok_or(ErrorKind::ResolutionFailure)?;
271272

272273
if let Some(prim) = is_primitive(&path, TypeNS) {
273-
let did = primitive_impl(cx, &path).ok_or(ErrorKind::ResolutionFailure)?;
274-
return cx
275-
.tcx
276-
.associated_items(did)
277-
.filter_by_name_unhygienic(item_name)
278-
.next()
279-
.and_then(|item| match item.kind {
280-
ty::AssocKind::Fn => Some("method"),
281-
_ => None,
282-
})
283-
.map(|out| (prim, Some(format!("{}#{}.{}", path, out, item_name))))
284-
.ok_or(ErrorKind::ResolutionFailure);
274+
for &impl_ in primitive_impl(cx, &path).ok_or(ErrorKind::ResolutionFailure)? {
275+
let link = cx
276+
.tcx
277+
.associated_items(impl_)
278+
.find_by_name_and_namespace(cx.tcx, Ident::with_dummy_span(item_name), ns, impl_)
279+
.and_then(|item| match item.kind {
280+
ty::AssocKind::Fn => Some("method"),
281+
_ => None,
282+
})
283+
.map(|out| (prim, Some(format!("{}#{}.{}", path, out, item_name))));
284+
if let Some(link) = link {
285+
return Ok(link);
286+
}
287+
}
288+
return Err(ErrorKind::ResolutionFailure);
285289
}
286290

287291
let (_, ty_res) = cx
@@ -1227,26 +1231,6 @@ fn is_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
12271231
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1) } else { None }
12281232
}
12291233

1230-
fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
1231-
let tcx = cx.tcx;
1232-
match path_str {
1233-
"u8" => tcx.lang_items().u8_impl(),
1234-
"u16" => tcx.lang_items().u16_impl(),
1235-
"u32" => tcx.lang_items().u32_impl(),
1236-
"u64" => tcx.lang_items().u64_impl(),
1237-
"u128" => tcx.lang_items().u128_impl(),
1238-
"usize" => tcx.lang_items().usize_impl(),
1239-
"i8" => tcx.lang_items().i8_impl(),
1240-
"i16" => tcx.lang_items().i16_impl(),
1241-
"i32" => tcx.lang_items().i32_impl(),
1242-
"i64" => tcx.lang_items().i64_impl(),
1243-
"i128" => tcx.lang_items().i128_impl(),
1244-
"isize" => tcx.lang_items().isize_impl(),
1245-
"f32" => tcx.lang_items().f32_impl(),
1246-
"f64" => tcx.lang_items().f64_impl(),
1247-
"str" => tcx.lang_items().str_impl(),
1248-
"bool" => tcx.lang_items().bool_impl(),
1249-
"char" => tcx.lang_items().char_impl(),
1250-
_ => None,
1251-
}
1234+
fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<&'static SmallVec<[DefId; 4]>> {
1235+
Some(PrimitiveType::from_symbol(Symbol::intern(path_str))?.impls(cx.tcx))
12521236
}

0 commit comments

Comments
 (0)