Skip to content

Commit ba64edb

Browse files
committed
Auto merge of #50533 - GuillaumeGomez:rustdoc-prim-auto, r=QuietMisdreavus
add auto-impl for primitive type Part of #50431. I have no clue how to test this though with the rustdoc test suite... r? @QuietMisdreavus
2 parents df40e61 + 564511e commit ba64edb

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

src/librustdoc/clean/auto_trait.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,29 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
3535
AdtKind::Enum => Def::Enum,
3636
AdtKind::Union => Def::Union,
3737
}
38-
_ => panic!("Unexpected type {:?}", def_id),
38+
ty::TyInt(_) |
39+
ty::TyUint(_) |
40+
ty::TyFloat(_) |
41+
ty::TyStr |
42+
ty::TyBool |
43+
ty::TyChar => return self.get_auto_trait_impls(def_id, &move |_: DefId| {
44+
match ty.sty {
45+
ty::TyInt(x) => Def::PrimTy(hir::TyInt(x)),
46+
ty::TyUint(x) => Def::PrimTy(hir::TyUint(x)),
47+
ty::TyFloat(x) => Def::PrimTy(hir::TyFloat(x)),
48+
ty::TyStr => Def::PrimTy(hir::TyStr),
49+
ty::TyBool => Def::PrimTy(hir::TyBool),
50+
ty::TyChar => Def::PrimTy(hir::TyChar),
51+
_ => unreachable!(),
52+
}
53+
}, None),
54+
_ => {
55+
debug!("Unexpected type {:?}", def_id);
56+
return Vec::new()
57+
}
3958
};
4059

41-
self.get_auto_trait_impls(def_id, def_ctor, None)
60+
self.get_auto_trait_impls(def_id, &def_ctor, None)
4261
}
4362

4463
pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> {
@@ -52,25 +71,26 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
5271
_ => panic!("Unexpected type {:?} {:?}", item, id),
5372
};
5473

55-
self.get_auto_trait_impls(did, def_ctor, Some(name))
74+
self.get_auto_trait_impls(did, &def_ctor, Some(name))
5675
}
5776

58-
pub fn get_auto_trait_impls(
77+
pub fn get_auto_trait_impls<F>(
5978
&self,
6079
def_id: DefId,
61-
def_ctor: fn(DefId) -> Def,
80+
def_ctor: &F,
6281
name: Option<String>,
63-
) -> Vec<Item> {
82+
) -> Vec<Item>
83+
where F: Fn(DefId) -> Def {
6484
if self.cx
6585
.tcx
6686
.get_attrs(def_id)
6787
.lists("doc")
6888
.has_word("hidden")
6989
{
7090
debug!(
71-
"get_auto_trait_impls(def_id={:?}, def_ctor={:?}): item has doc('hidden'), \
91+
"get_auto_trait_impls(def_id={:?}, def_ctor=...): item has doc('hidden'), \
7292
aborting",
73-
def_id, def_ctor
93+
def_id
7494
);
7595
return Vec::new();
7696
}
@@ -79,8 +99,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
7999
let generics = self.cx.tcx.generics_of(def_id);
80100

81101
debug!(
82-
"get_auto_trait_impls(def_id={:?}, def_ctor={:?}, generics={:?}",
83-
def_id, def_ctor, generics
102+
"get_auto_trait_impls(def_id={:?}, def_ctor=..., generics={:?}",
103+
def_id, generics
84104
);
85105
let auto_traits: Vec<_> = self.cx
86106
.send_trait
@@ -110,23 +130,24 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
110130
auto_traits
111131
}
112132

113-
fn get_auto_trait_impl_for(
133+
fn get_auto_trait_impl_for<F>(
114134
&self,
115135
def_id: DefId,
116136
name: Option<String>,
117137
generics: ty::Generics,
118-
def_ctor: fn(DefId) -> Def,
138+
def_ctor: &F,
119139
trait_def_id: DefId,
120-
) -> Option<Item> {
140+
) -> Option<Item>
141+
where F: Fn(DefId) -> Def {
121142
if !self.cx
122143
.generated_synthetics
123144
.borrow_mut()
124145
.insert((def_id, trait_def_id))
125146
{
126147
debug!(
127-
"get_auto_trait_impl_for(def_id={:?}, generics={:?}, def_ctor={:?}, \
148+
"get_auto_trait_impl_for(def_id={:?}, generics={:?}, def_ctor=..., \
128149
trait_def_id={:?}): already generated, aborting",
129-
def_id, generics, def_ctor, trait_def_id
150+
def_id, generics, trait_def_id
130151
);
131152
return None;
132153
}

src/librustdoc/clean/inline.rs

+8
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ pub fn build_impls(cx: &DocContext, did: DefId, auto_traits: bool) -> Vec<clean:
302302
for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
303303
if !def_id.is_local() {
304304
build_impl(cx, def_id, &mut impls);
305+
306+
let auto_impls = get_auto_traits_with_def_id(cx, def_id);
307+
let mut renderinfo = cx.renderinfo.borrow_mut();
308+
309+
let new_impls: Vec<clean::Item> = auto_impls.into_iter()
310+
.filter(|i| renderinfo.inlined.insert(i.def_id)).collect();
311+
312+
impls.extend(new_impls);
305313
}
306314
}
307315

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4183,7 +4183,8 @@ pub fn path_to_def(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
41834183
}
41844184
}
41854185

4186-
fn get_path_for_type(tcx: TyCtxt, def_id: DefId, def_ctor: fn(DefId) -> Def) -> hir::Path {
4186+
fn get_path_for_type<F>(tcx: TyCtxt, def_id: DefId, def_ctor: F) -> hir::Path
4187+
where F: Fn(DefId) -> Def {
41874188
struct AbsolutePathBuffer {
41884189
names: Vec<String>,
41894190
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 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+
#![crate_name = "foo"]
12+
pub use std::fs::File;
13+
14+
// @has 'foo/primitive.i16.html' '//h2[@id="synthetic-implementations"]' 'Auto Trait Implementation'
15+
#[doc(primitive = "i16")]
16+
/// I love poneys!
17+
mod prim {}

0 commit comments

Comments
 (0)