Skip to content

Commit defe3e9

Browse files
committed
rustdoc: Render/hyperlink primitive types
This piggybacks on the previous commits to add impls of primitives directly.
1 parent dda69ab commit defe3e9

File tree

9 files changed

+197
-51
lines changed

9 files changed

+197
-51
lines changed

src/librustc/middle/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5017,7 +5017,7 @@ impl substs {
50175017
}
50185018
}
50195019

5020-
/// Register a primitive type as having it's lang-item implementation in this
5020+
/// Register a primitive type as having its lang-item implementation in this
50215021
/// crate. This method will create a DefId if one doesn't already exist for it.
50225022
pub fn add_local_prim_did(tcx: ctxt, t: t) -> ast::DefId {
50235023
let mut map = tcx.prim_dids.borrow_mut();
@@ -5032,7 +5032,7 @@ pub fn add_local_prim_did(tcx: ctxt, t: t) -> ast::DefId {
50325032
}
50335033

50345034
/// Fetch the DefId of the lang-item implementation of a primitive type. This
5035-
/// may not succeed (as the local crate or linked crates may not provided an
5035+
/// may not succeed (as the local crate or linked crates may not provide an
50365036
/// implementation).
50375037
pub fn maybe_prim_did(tcx: ctxt, t: t) -> Option<ast::DefId> {
50385038
let map = tcx.prim_dids.borrow();

src/librustc/middle/typeck/coherence.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn get_base_type(inference_context: @InferCtxt,
8888

8989
ty_bot |
9090
ty_str(..) | ty_vec(..) | ty_bare_fn(..) | ty_closure(..) | ty_tup(..) |
91-
ty_infer(..) | ty_param(..) | ty_self(..) | ty_type | ty_opaque_box |
91+
ty_infer(..) | ty_param(..) | ty_self(..) | ty_type |
9292
ty_opaque_closure_ptr(..) | ty_unboxed_vec(..) | ty_err | ty_box(_) |
9393
ty_uniq(_) | ty_ptr(_) | ty_rptr(_, _) => {
9494
debug!("(getting base type) no base type; found {:?}",
@@ -147,40 +147,40 @@ pub fn get_base_type_def_id(inference_context: @InferCtxt,
147147
ty_char => { if li.char_impl() != Some(did) { return None } }
148148
ty_nil => { if li.nil_impl() != Some(did) { return None } }
149149
ty_bool => { if li.bool_impl() != Some(did) { return None } }
150-
ty_float(ast::ty_f32) => {
150+
ty_float(ast::TyF32) => {
151151
if li.f32_impl() != Some(did) { return None }
152152
}
153-
ty_float(ast::ty_f64) => {
153+
ty_float(ast::TyF64) => {
154154
if li.f64_impl() != Some(did) { return None }
155155
}
156-
ty_uint(ast::ty_u) => {
156+
ty_uint(ast::TyU) => {
157157
if li.uint_impl() != Some(did) { return None }
158158
}
159-
ty_int(ast::ty_i) => {
159+
ty_int(ast::TyI) => {
160160
if li.int_impl() != Some(did) { return None }
161161
}
162-
ty_int(ast::ty_i8) => {
162+
ty_int(ast::TyI8) => {
163163
if li.i8_impl() != Some(did) { return None }
164164
}
165-
ty_int(ast::ty_i16) => {
165+
ty_int(ast::TyI16) => {
166166
if li.i16_impl() != Some(did) { return None }
167167
}
168-
ty_int(ast::ty_i32) => {
168+
ty_int(ast::TyI32) => {
169169
if li.i32_impl() != Some(did) { return None }
170170
}
171-
ty_int(ast::ty_i64) => {
171+
ty_int(ast::TyI64) => {
172172
if li.i64_impl() != Some(did) { return None }
173173
}
174-
ty_uint(ast::ty_u8) => {
174+
ty_uint(ast::TyU8) => {
175175
if li.u8_impl() != Some(did) { return None }
176176
}
177-
ty_uint(ast::ty_u16) => {
177+
ty_uint(ast::TyU16) => {
178178
if li.u16_impl() != Some(did) { return None }
179179
}
180-
ty_uint(ast::ty_u32) => {
180+
ty_uint(ast::TyU32) => {
181181
if li.u32_impl() != Some(did) { return None }
182182
}
183-
ty_uint(ast::ty_u64) => {
183+
ty_uint(ast::TyU64) => {
184184
if li.u64_impl() != Some(did) { return None }
185185
}
186186

@@ -635,12 +635,12 @@ impl CoherenceChecker {
635635
Some(_) => false,
636636
}
637637
}
638-
DefPrimTy(ast::ty_str) => false,
638+
DefPrimTy(ast::TyStr) => false,
639639
DefPrimTy(..) => true,
640640
_ => false
641641
}
642642
}
643-
ast::ty_nil => true,
643+
ast::TyNil => true,
644644
_ => false
645645
}
646646
}

src/librustdoc/clean.rs

+134-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use syntax::codemap::Pos;
2424
use rustc::metadata::cstore;
2525
use rustc::metadata::csearch;
2626
use rustc::metadata::decoder;
27+
use rustc::middle::ty;
2728

2829
use std;
2930
use std::hashmap::HashMap;
@@ -182,6 +183,7 @@ pub enum ItemEnum {
182183
VariantItem(Variant),
183184
ForeignFunctionItem(Function),
184185
ForeignStaticItem(Static),
186+
PrimitiveType(ast::PrimTy),
185187
}
186188

187189
#[deriving(Clone, Encodable, Decodable)]
@@ -196,6 +198,8 @@ impl Clean<Item> for doctree::Module {
196198
} else {
197199
~""
198200
};
201+
let impls = self.impls.clean();
202+
let impls = impls.move_iter().flat_map(|i| i.move_iter()).collect();
199203
Item {
200204
name: Some(name),
201205
attrs: self.attrs.clean(),
@@ -207,7 +211,7 @@ impl Clean<Item> for doctree::Module {
207211
self.fns.clean(), self.foreigns.clean().concat_vec(),
208212
self.mods.clean(), self.typedefs.clean(),
209213
self.statics.clean(), self.traits.clean(),
210-
self.impls.clean(), self.view_items.clean()].concat_vec()
214+
impls, self.view_items.clean()].concat_vec()
211215
})
212216
}
213217
}
@@ -615,6 +619,7 @@ pub enum TypeKind {
615619
TypeEnum,
616620
TypeTrait,
617621
TypeFunction,
622+
TypePrimitive,
618623
}
619624

620625
impl Clean<Type> for ast::Ty {
@@ -961,23 +966,100 @@ pub struct Impl {
961966
trait_: Option<Type>,
962967
for_: Type,
963968
methods: ~[Item],
969+
is_primitive_impl: bool,
964970
}
965971

966-
impl Clean<Item> for doctree::Impl {
967-
fn clean(&self) -> Item {
968-
Item {
972+
impl Clean<~[Item]> for doctree::Impl {
973+
fn clean(&self) -> ~[Item] {
974+
let tcx = local_data::get(super::ctxtkey, |x| *x.unwrap()).tycx;
975+
let is_primitive_impl = match tcx {
976+
Some(tcx) => {
977+
let did = Some(ast::DefId {
978+
node: self.id,
979+
crate: ast::LOCAL_CRATE,
980+
});
981+
did == tcx.lang_items.char_impl() ||
982+
did == tcx.lang_items.bool_impl() ||
983+
did == tcx.lang_items.u8_impl() ||
984+
did == tcx.lang_items.u16_impl() ||
985+
did == tcx.lang_items.u32_impl() ||
986+
did == tcx.lang_items.u64_impl() ||
987+
did == tcx.lang_items.uint_impl() ||
988+
did == tcx.lang_items.i8_impl() ||
989+
did == tcx.lang_items.i16_impl() ||
990+
did == tcx.lang_items.i32_impl() ||
991+
did == tcx.lang_items.i64_impl() ||
992+
did == tcx.lang_items.int_impl() ||
993+
did == tcx.lang_items.f32_impl() ||
994+
did == tcx.lang_items.f64_impl()
995+
}
996+
None => false
997+
};
998+
let mut items = ~[];
999+
if is_primitive_impl {
1000+
items.push(primitive_item(self));
1001+
}
1002+
items.push(Item {
9691003
name: None,
970-
attrs: self.attrs.clean(),
1004+
attrs: if is_primitive_impl {~[]} else {self.attrs.clean()},
9711005
source: self.where.clean(),
9721006
id: self.id,
9731007
visibility: self.vis.clean(),
9741008
inner: ImplItem(Impl {
1009+
is_primitive_impl: is_primitive_impl,
9751010
generics: self.generics.clean(),
9761011
trait_: self.trait_.clean(),
9771012
for_: self.for_.clean(),
9781013
methods: self.methods.clean(),
9791014
}),
1015+
});
1016+
return items;
1017+
}
1018+
}
1019+
1020+
fn primitive_item(imp: &doctree::Impl) -> Item {
1021+
let tcx = local_data::get(super::ctxtkey, |x| *x.unwrap()).tycx.unwrap();
1022+
let def_map = tcx.def_map.borrow();
1023+
let id = match imp.for_.node {
1024+
ast::TyPath(_, _, id) => id,
1025+
_ => fail!("not a primitive path"),
1026+
};
1027+
let d = def_map.get().get(&id);
1028+
1029+
macro_rules! primitive( ($li:ident, $prim:expr) => ({
1030+
match tycx.lang_items.$li() {
1031+
Some(did) => (did, TypePrimitive),
1032+
None => return if $prim == ast::TyBool {Bool} else {Primitive($prim)}
9801033
}
1034+
}) )
1035+
1036+
let (ty, name, prim_ty) = match *d {
1037+
ast::DefPrimTy(p) => match p {
1038+
ast::TyBool => (ty::mk_bool(), "bool", p),
1039+
ast::TyChar => (ty::mk_char(), "char", p),
1040+
ast::TyFloat(ast::TyF32) => (ty::mk_f32(), "f32", p),
1041+
ast::TyFloat(ast::TyF64) => (ty::mk_f64(), "f64", p),
1042+
ast::TyUint(ast::TyU) => (ty::mk_uint(), "uint", p),
1043+
ast::TyUint(ast::TyU8) => (ty::mk_u8(), "u8", p),
1044+
ast::TyUint(ast::TyU16) => (ty::mk_u16(), "u16", p),
1045+
ast::TyUint(ast::TyU32) => (ty::mk_u32(), "u32", p),
1046+
ast::TyUint(ast::TyU64) => (ty::mk_u64(), "u64", p),
1047+
ast::TyInt(ast::TyI) => (ty::mk_int(), "int", p),
1048+
ast::TyInt(ast::TyI8) => (ty::mk_i8(), "i8", p),
1049+
ast::TyInt(ast::TyI16) => (ty::mk_i16(), "i16", p),
1050+
ast::TyInt(ast::TyI32) => (ty::mk_i32(), "i32", p),
1051+
ast::TyInt(ast::TyI64) => (ty::mk_i64(), "i64", p),
1052+
ast::TyStr => fail!("can't handle string primitive yet"),
1053+
},
1054+
x => fail!("resolved type maps to not a primitive {:?}", x),
1055+
};
1056+
Item {
1057+
name: Some(name.to_owned()),
1058+
attrs: imp.attrs.clean(),
1059+
source: imp.where.clean(),
1060+
id: ty::maybe_prim_did(tcx, ty).unwrap().node,
1061+
visibility: ast::Public.clean(),
1062+
inner: PrimitiveType(prim_ty),
9811063
}
9821064
}
9831065

@@ -1176,6 +1258,15 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
11761258
}
11771259
};
11781260

1261+
macro_rules! primitive( ($ty:expr, $prim:expr) => ({
1262+
match ty::maybe_prim_did(tycx, $ty) {
1263+
Some(did) => (did, TypePrimitive),
1264+
None => {
1265+
return if $prim == ast::TyBool {Bool} else {Primitive($prim)}
1266+
}
1267+
}
1268+
}) )
1269+
11791270
let (def_id, kind) = match *d {
11801271
ast::DefFn(i, _) => (i, TypeFunction),
11811272
ast::DefSelf(i, _) | ast::DefSelfTy(i) => return Self(i),
@@ -1186,8 +1277,20 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
11861277
},
11871278
ast::DefPrimTy(p) => match p {
11881279
ast::TyStr => return String,
1189-
ast::TyBool => return Bool,
1190-
_ => return Primitive(p)
1280+
ast::TyBool => primitive!(ty::mk_bool(), p),
1281+
ast::TyChar => primitive!(ty::mk_char(), p),
1282+
ast::TyFloat(ast::TyF32) => primitive!(ty::mk_f32(), p),
1283+
ast::TyFloat(ast::TyF64) => primitive!(ty::mk_f64(), p),
1284+
ast::TyUint(ast::TyU) => primitive!(ty::mk_uint(), p),
1285+
ast::TyUint(ast::TyU8) => primitive!(ty::mk_u8(), p),
1286+
ast::TyUint(ast::TyU16) => primitive!(ty::mk_u16(), p),
1287+
ast::TyUint(ast::TyU32) => primitive!(ty::mk_u32(), p),
1288+
ast::TyUint(ast::TyU64) => primitive!(ty::mk_u64(), p),
1289+
ast::TyInt(ast::TyI) => primitive!(ty::mk_int(), p),
1290+
ast::TyInt(ast::TyI8) => primitive!(ty::mk_i8(), p),
1291+
ast::TyInt(ast::TyI16) => primitive!(ty::mk_i16(), p),
1292+
ast::TyInt(ast::TyI32) => primitive!(ty::mk_i32(), p),
1293+
ast::TyInt(ast::TyI64) => primitive!(ty::mk_i64(), p),
11911294
},
11921295
ast::DefTyParam(i, _) => return Generic(i.node),
11931296
ast::DefStruct(i) => (i, TypeStruct),
@@ -1200,7 +1303,30 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
12001303
if ast_util::is_local(def_id) {
12011304
ResolvedPath{ path: path, typarams: tpbs, id: def_id.node }
12021305
} else {
1203-
let fqn = csearch::get_item_path(tycx, def_id);
1306+
let fqn = match *d {
1307+
ast::DefPrimTy(p) => {
1308+
let did = match p {
1309+
ast::TyBool => tycx.lang_items.bool_impl(),
1310+
ast::TyChar => tycx.lang_items.char_impl(),
1311+
ast::TyFloat(ast::TyF32) => tycx.lang_items.f32_impl(),
1312+
ast::TyFloat(ast::TyF64) => tycx.lang_items.f64_impl(),
1313+
ast::TyInt(ast::TyI) => tycx.lang_items.int_impl(),
1314+
ast::TyInt(ast::TyI8) => tycx.lang_items.i8_impl(),
1315+
ast::TyInt(ast::TyI16) => tycx.lang_items.i16_impl(),
1316+
ast::TyInt(ast::TyI32) => tycx.lang_items.i32_impl(),
1317+
ast::TyInt(ast::TyI64) => tycx.lang_items.i64_impl(),
1318+
ast::TyUint(ast::TyU) => tycx.lang_items.uint_impl(),
1319+
ast::TyUint(ast::TyU8) => tycx.lang_items.u8_impl(),
1320+
ast::TyUint(ast::TyU16) => tycx.lang_items.u16_impl(),
1321+
ast::TyUint(ast::TyU32) => tycx.lang_items.u32_impl(),
1322+
ast::TyUint(ast::TyU64) => tycx.lang_items.u64_impl(),
1323+
1324+
_ => None,
1325+
};
1326+
csearch::get_item_path(tycx, did.unwrap())
1327+
}
1328+
_ => csearch::get_item_path(tycx, def_id)
1329+
};
12041330
let fqn = fqn.move_iter().map(|i| {
12051331
match i {
12061332
ast_map::PathMod(id) | ast_map::PathName(id) |

src/librustdoc/html/format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn external_path(w: &mut io::Writer, p: &clean::Path, print_all: bool,
155155
clean::TypeEnum => "enum",
156156
clean::TypeFunction => "fn",
157157
clean::TypeTrait => "trait",
158+
clean::TypePrimitive => "primitive",
158159
}))
159160
})
160161
}

0 commit comments

Comments
 (0)