Skip to content

Commit 260f17e

Browse files
committed
fix rustdoc
1 parent 36a3ebd commit 260f17e

File tree

4 files changed

+60
-57
lines changed

4 files changed

+60
-57
lines changed

src/librustdoc/clean/types.rs

+57-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cell::RefCell;
22
use std::default::Default;
33
use std::hash::Hash;
4+
use std::iter;
45
use std::lazy::SyncOnceCell as OnceCell;
56
use std::path::PathBuf;
67
use std::rc::Rc;
@@ -22,6 +23,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}
2223
use rustc_hir::lang_items::LangItem;
2324
use rustc_hir::{BodyId, Mutability};
2425
use rustc_index::vec::IndexVec;
26+
use rustc_middle::ty::fast_reject::SimplifiedType;
2527
use rustc_middle::ty::{self, TyCtxt};
2628
use rustc_session::Session;
2729
use rustc_span::hygiene::MacroKind;
@@ -1625,6 +1627,7 @@ crate enum PrimitiveType {
16251627
Never,
16261628
}
16271629

1630+
type SimplifiedTypes = FxHashMap<PrimitiveType, ArrayVec<SimplifiedType, 2>>;
16281631
impl PrimitiveType {
16291632
crate fn from_hir(prim: hir::PrimTy) -> PrimitiveType {
16301633
use ast::{FloatTy, IntTy, UintTy};
@@ -1680,68 +1683,68 @@ impl PrimitiveType {
16801683
}
16811684
}
16821685

1683-
crate fn impls(&self, tcx: TyCtxt<'_>) -> &'static ArrayVec<DefId, 4> {
1684-
Self::all_impls(tcx).get(self).expect("missing impl for primitive type")
1685-
}
1686-
1687-
crate fn all_impls(tcx: TyCtxt<'_>) -> &'static FxHashMap<PrimitiveType, ArrayVec<DefId, 4>> {
1688-
static CELL: OnceCell<FxHashMap<PrimitiveType, ArrayVec<DefId, 4>>> = OnceCell::new();
1686+
crate fn simplified_types() -> &'static SimplifiedTypes {
1687+
use ty::fast_reject::SimplifiedTypeGen::*;
1688+
use ty::{FloatTy, IntTy, UintTy};
1689+
use PrimitiveType::*;
1690+
static CELL: OnceCell<SimplifiedTypes> = OnceCell::new();
16891691

1692+
let single = |x| iter::once(x).collect();
16901693
CELL.get_or_init(move || {
1691-
use self::PrimitiveType::*;
1692-
1693-
let single = |a: Option<DefId>| a.into_iter().collect();
1694-
let both = |a: Option<DefId>, b: Option<DefId>| -> ArrayVec<_, 4> {
1695-
a.into_iter().chain(b).collect()
1696-
};
1697-
1698-
let lang_items = tcx.lang_items();
16991694
map! {
1700-
Isize => single(lang_items.isize_impl()),
1701-
I8 => single(lang_items.i8_impl()),
1702-
I16 => single(lang_items.i16_impl()),
1703-
I32 => single(lang_items.i32_impl()),
1704-
I64 => single(lang_items.i64_impl()),
1705-
I128 => single(lang_items.i128_impl()),
1706-
Usize => single(lang_items.usize_impl()),
1707-
U8 => single(lang_items.u8_impl()),
1708-
U16 => single(lang_items.u16_impl()),
1709-
U32 => single(lang_items.u32_impl()),
1710-
U64 => single(lang_items.u64_impl()),
1711-
U128 => single(lang_items.u128_impl()),
1712-
F32 => both(lang_items.f32_impl(), lang_items.f32_runtime_impl()),
1713-
F64 => both(lang_items.f64_impl(), lang_items.f64_runtime_impl()),
1714-
Char => single(lang_items.char_impl()),
1715-
Bool => single(lang_items.bool_impl()),
1716-
Str => both(lang_items.str_impl(), lang_items.str_alloc_impl()),
1717-
Slice => {
1718-
lang_items
1719-
.slice_impl()
1720-
.into_iter()
1721-
.chain(lang_items.slice_u8_impl())
1722-
.chain(lang_items.slice_alloc_impl())
1723-
.chain(lang_items.slice_u8_alloc_impl())
1724-
.collect()
1725-
},
1726-
Array => single(lang_items.array_impl()),
1727-
Tuple => ArrayVec::new(),
1728-
Unit => ArrayVec::new(),
1729-
RawPointer => {
1730-
lang_items
1731-
.const_ptr_impl()
1732-
.into_iter()
1733-
.chain(lang_items.mut_ptr_impl())
1734-
.chain(lang_items.const_slice_ptr_impl())
1735-
.chain(lang_items.mut_slice_ptr_impl())
1736-
.collect()
1737-
},
1738-
Reference => ArrayVec::new(),
1695+
Isize => single(IntSimplifiedType(IntTy::Isize)),
1696+
I8 => single(IntSimplifiedType(IntTy::I8)),
1697+
I16 => single(IntSimplifiedType(IntTy::I16)),
1698+
I32 => single(IntSimplifiedType(IntTy::I32)),
1699+
I64 => single(IntSimplifiedType(IntTy::I64)),
1700+
I128 => single(IntSimplifiedType(IntTy::I128)),
1701+
Usize => single(UintSimplifiedType(UintTy::Usize)),
1702+
U8 => single(UintSimplifiedType(UintTy::U8)),
1703+
U16 => single(UintSimplifiedType(UintTy::U16)),
1704+
U32 => single(UintSimplifiedType(UintTy::U32)),
1705+
U64 => single(UintSimplifiedType(UintTy::U64)),
1706+
U128 => single(UintSimplifiedType(UintTy::U128)),
1707+
F32 => single(FloatSimplifiedType(FloatTy::F32)),
1708+
F64 => single(FloatSimplifiedType(FloatTy::F64)),
1709+
Str => single(StrSimplifiedType),
1710+
Bool => single(BoolSimplifiedType),
1711+
Char => single(CharSimplifiedType),
1712+
Array => single(ArraySimplifiedType),
1713+
Slice => single(SliceSimplifiedType),
1714+
// FIXME: If we ever add an inherent impl for tuples
1715+
// with different lengths, they won't show in rustdoc.
1716+
//
1717+
// Either manually update this arrayvec at this point
1718+
// or start with a more complex refactoring.
1719+
Tuple => [TupleSimplifiedType(2), TupleSimplifiedType(3)].into(),
1720+
Unit => single(TupleSimplifiedType(0)),
1721+
RawPointer => [PtrSimplifiedType(Mutability::Not), PtrSimplifiedType(Mutability::Mut)].into(),
1722+
Reference => [RefSimplifiedType(Mutability::Not), RefSimplifiedType(Mutability::Mut)].into(),
1723+
// FIXME: This will be wrong if we ever add inherent impls
1724+
// for function pointers.
17391725
Fn => ArrayVec::new(),
1740-
Never => ArrayVec::new(),
1726+
Never => single(NeverSimplifiedType),
17411727
}
17421728
})
17431729
}
17441730

1731+
crate fn impls<'tcx>(&self, tcx: TyCtxt<'tcx>) -> impl Iterator<Item = DefId> + 'tcx {
1732+
Self::simplified_types()
1733+
.get(self)
1734+
.into_iter()
1735+
.flatten()
1736+
.flat_map(move |&simp| tcx.incoherent_impls(simp))
1737+
.copied()
1738+
}
1739+
1740+
crate fn all_impls(tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> + '_ {
1741+
Self::simplified_types()
1742+
.values()
1743+
.flatten()
1744+
.flat_map(move |&simp| tcx.incoherent_impls(simp))
1745+
.copied()
1746+
}
1747+
17451748
crate fn as_sym(&self) -> Symbol {
17461749
use PrimitiveType::*;
17471750
match self {

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ crate fn build_deref_target_impls(cx: &mut DocContext<'_>, items: &[Item], ret:
184184

185185
if let Some(prim) = target.primitive_type() {
186186
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_primitive_inherent_impls");
187-
for &did in prim.impls(tcx).iter().filter(|did| !did.is_local()) {
187+
for did in prim.impls(tcx).filter(|did| !did.is_local()) {
188188
inline::build_impl(cx, None, did, None, ret);
189189
}
190190
} else if let Type::Path { path } = target {

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
467467
) -> Option<(Res, ItemFragment)> {
468468
let tcx = self.cx.tcx;
469469

470-
prim_ty.impls(tcx).into_iter().find_map(|&impl_| {
470+
prim_ty.impls(tcx).find_map(|impl_| {
471471
tcx.associated_items(impl_)
472472
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, impl_)
473473
.map(|item| {

src/librustdoc/passes/collect_trait_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
4545

4646
// Also try to inline primitive impls from other crates.
4747
cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
48-
for &def_id in PrimitiveType::all_impls(cx.tcx).values().flatten() {
48+
for def_id in PrimitiveType::all_impls(cx.tcx) {
4949
if !def_id.is_local() {
5050
inline::build_impl(cx, None, def_id, None, &mut new_items);
5151

0 commit comments

Comments
 (0)