Skip to content

Commit 88866a4

Browse files
committed
rustdoc: Show type parameters on external paths
This removes the internal type representation of an `External` type and instead relies on passing around DefId structures and interpreting them accordingly. Progress on #9539, but there's still the problem of a crate => url mapping.
1 parent 8973d7c commit 88866a4

File tree

4 files changed

+34
-59
lines changed

4 files changed

+34
-59
lines changed

src/librustdoc/clean.rs

+6-36
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,11 @@ impl Clean<TraitMethod> for ast::trait_method {
540540
#[deriving(Clone, Encodable, Decodable)]
541541
pub enum Type {
542542
/// structs/enums/traits (anything that'd be an ast::ty_path)
543-
ResolvedPath { path: Path, typarams: Option<~[TyParamBound]>, id: ast::NodeId },
544-
/// Reference to an item in an external crate (fully qualified path)
545-
External(~str, ~str),
543+
ResolvedPath {
544+
path: Path,
545+
typarams: Option<~[TyParamBound]>,
546+
did: ast::DefId
547+
},
546548
// I have no idea how to usefully use this.
547549
TyParamBinder(ast::NodeId),
548550
/// For parameterized types, so the consumer of the JSON don't go looking
@@ -1148,39 +1150,7 @@ fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
11481150
},
11491151
x => fail!("resolved type maps to a weird def %?", x),
11501152
};
1151-
1152-
if def_id.crate != ast::CRATE_NODE_ID {
1153-
use rustc::metadata::decoder::*;
1154-
1155-
let sess = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess;
1156-
let cratedata = ::rustc::metadata::cstore::get_crate_data(sess.cstore, def_id.crate);
1157-
let doc = lookup_item(def_id.node, cratedata.data);
1158-
let path = syntax::ast_map::path_to_str_with_sep(item_path(doc), "::", sess.intr());
1159-
let ty = match def_like_to_def(item_to_def_like(doc, def_id, def_id.crate)) {
1160-
DefFn(*) => ~"fn",
1161-
DefTy(*) => ~"enum",
1162-
DefTrait(*) => ~"trait",
1163-
DefPrimTy(p) => match p {
1164-
ty_str => ~"str",
1165-
ty_bool => ~"bool",
1166-
ty_int(t) => match t.to_str() {
1167-
~"" => ~"i",
1168-
s => s
1169-
},
1170-
ty_uint(t) => t.to_str(),
1171-
ty_float(t) => t.to_str(),
1172-
ty_char => ~"char",
1173-
},
1174-
DefTyParam(*) => ~"generic",
1175-
DefStruct(*) => ~"struct",
1176-
DefTyParamBinder(*) => ~"typaram_binder",
1177-
x => fail!("resolved external maps to a weird def %?", x),
1178-
};
1179-
let cname = cratedata.name.to_owned();
1180-
External(cname + "::" + path, ty)
1181-
} else {
1182-
ResolvedPath {path: path.clone(), typarams: tpbs, id: def_id.node}
1183-
}
1153+
ResolvedPath{ path: path, typarams: tpbs, did: def_id }
11841154
}
11851155

11861156
fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {

src/librustdoc/html/format.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl fmt::Default for clean::Path {
9797
}
9898
}
9999

100-
fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
100+
fn resolved_path(w: &mut io::Writer, did: ast::DefId,
101101
path: &clean::Path, print_all: bool) {
102102
// The generics will get written to both the title and link
103103
let mut generics = ~"";
@@ -144,9 +144,10 @@ fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
144144

145145
do local_data::get(cache_key) |cache| {
146146
do cache.unwrap().read |cache| {
147-
match cache.paths.find(&id) {
147+
match cache.paths.find(&did.node) {
148148
// This is a documented path, link to it!
149-
Some(&(ref fqp, shortty)) => {
149+
// FIXME(#9539): this is_local check should not exist
150+
Some(&(ref fqp, shortty)) if ast_util::is_local(did) => {
150151
let fqn = fqp.connect("::");
151152
let same = loc.iter().zip(fqp.iter())
152153
.take_while(|&(a, b)| *a == *b).len();
@@ -180,7 +181,7 @@ fn resolved_path(w: &mut io::Writer, id: ast::NodeId,
180181
write!(w, "<a class='{}' href='{}' title='{}'>{}</a>{}",
181182
shortty, url, fqn, last.name, generics);
182183
}
183-
None => {
184+
_ => {
184185
if print_all {
185186
let amt = path.segments.len() - 1;
186187
for seg in path.segments.iter().take(amt) {
@@ -205,8 +206,8 @@ impl fmt::Default for clean::Type {
205206
}
206207
}
207208
}
208-
clean::ResolvedPath{id, typarams: ref typarams, path: ref path} => {
209-
resolved_path(f.buf, id, path, false);
209+
clean::ResolvedPath{did, typarams: ref typarams, path: ref path} => {
210+
resolved_path(f.buf, did, path, false);
210211
match *typarams {
211212
Some(ref params) => {
212213
f.buf.write("&lt;".as_bytes());
@@ -219,10 +220,6 @@ impl fmt::Default for clean::Type {
219220
None => {}
220221
}
221222
}
222-
// XXX: this should be a link
223-
clean::External(ref a, _) => {
224-
write!(f.buf, "{}", *a);
225-
}
226223
clean::Self(*) => f.buf.write("Self".as_bytes()),
227224
clean::Primitive(prim) => {
228225
let s = match prim {
@@ -421,8 +418,8 @@ impl fmt::Default for clean::ViewPath {
421418
impl fmt::Default for clean::ImportSource {
422419
fn fmt(v: &clean::ImportSource, f: &mut fmt::Formatter) {
423420
match v.did {
424-
Some(did) if ast_util::is_local(did) => {
425-
resolved_path(f.buf, did.node, &v.path, true);
421+
Some(did) => {
422+
resolved_path(f.buf, did, &v.path, true);
426423
}
427424
_ => {
428425
for (i, seg) in v.path.segments.iter().enumerate() {
@@ -437,7 +434,7 @@ impl fmt::Default for clean::ImportSource {
437434
impl fmt::Default for clean::ViewListIdent {
438435
fn fmt(v: &clean::ViewListIdent, f: &mut fmt::Formatter) {
439436
match v.source {
440-
Some(did) if ast_util::is_local(did) => {
437+
Some(did) => {
441438
let path = clean::Path {
442439
global: false,
443440
segments: ~[clean::PathSegment {
@@ -446,7 +443,7 @@ impl fmt::Default for clean::ViewListIdent {
446443
types: ~[],
447444
}]
448445
};
449-
resolved_path(f.buf, did.node, &path, false);
446+
resolved_path(f.buf, did, &path, false);
450447
}
451448
_ => write!(f.buf, "{}", v.name),
452449
}

src/librustdoc/html/render.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use extra::json::ToJson;
3030
use extra::sort;
3131

3232
use syntax::ast;
33+
use syntax::ast_util::is_local;
3334
use syntax::attr;
3435

3536
use clean;
@@ -325,7 +326,8 @@ impl DocFolder for Cache {
325326
match item.inner {
326327
clean::ImplItem(ref i) => {
327328
match i.trait_ {
328-
Some(clean::ResolvedPath{ id, _ }) => {
329+
Some(clean::ResolvedPath{ did, _ }) if is_local(did) => {
330+
let id = did.node;
329331
let v = do self.implementors.find_or_insert_with(id) |_|{
330332
~[]
331333
};
@@ -412,8 +414,8 @@ impl DocFolder for Cache {
412414
}
413415
clean::ImplItem(ref i) => {
414416
match i.for_ {
415-
clean::ResolvedPath{ id, _ } => {
416-
self.parent_stack.push(id); true
417+
clean::ResolvedPath{ did, _ } if is_local(did) => {
418+
self.parent_stack.push(did.node); true
417419
}
418420
_ => false
419421
}
@@ -428,7 +430,8 @@ impl DocFolder for Cache {
428430
match item.inner {
429431
clean::ImplItem(i) => {
430432
match i.for_ {
431-
clean::ResolvedPath { id, _ } => {
433+
clean::ResolvedPath { did, _ } if is_local(did) => {
434+
let id = did.node;
432435
let v = do self.impls.find_or_insert_with(id) |_| {
433436
~[]
434437
};
@@ -1179,7 +1182,7 @@ fn render_impl(w: &mut io::Writer, i: &clean::Impl) {
11791182
Some(ref ty) => {
11801183
write!(w, "{} for ", *ty);
11811184
match *ty {
1182-
clean::ResolvedPath { id, _ } => Some(id),
1185+
clean::ResolvedPath { did, _ } => Some(did),
11831186
_ => None,
11841187
}
11851188
}
@@ -1201,7 +1204,11 @@ fn render_impl(w: &mut io::Writer, i: &clean::Impl) {
12011204
}
12021205

12031206
// No documentation? Attempt to slurp in the trait's documentation
1204-
let trait_id = match trait_id { Some(id) => id, None => loop };
1207+
let trait_id = match trait_id {
1208+
None => loop,
1209+
Some(id) if is_local(id) => loop,
1210+
Some(id) => id.node,
1211+
};
12051212
do local_data::get(cache_key) |cache| {
12061213
do cache.unwrap().read |cache| {
12071214
let name = meth.name.get_ref().as_slice();
@@ -1307,7 +1314,7 @@ impl<'self> fmt::Default for Source<'self> {
13071314
}
13081315
write!(fmt.buf, "<pre class='line-numbers'>");
13091316
for i in range(1, lines + 1) {
1310-
write!(fmt.buf, "<span id='{0}'>{0:1$u}</span>\n", i, cols);
1317+
write!(fmt.buf, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols);
13111318
}
13121319
write!(fmt.buf, "</pre>");
13131320
write!(fmt.buf, "<pre class='rust'>");

src/librustdoc/passes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::uint;
1313
use std::hashmap::HashSet;
1414

1515
use syntax::ast;
16+
use syntax::ast_util::is_local;
1617

1718
use clean;
1819
use clean::Item;
@@ -130,8 +131,8 @@ pub fn strip_private(mut crate: clean::Crate) -> plugins::PluginResult {
130131
match i.inner {
131132
clean::ImplItem(ref imp) => {
132133
match imp.trait_ {
133-
Some(clean::ResolvedPath{ id, _ }) => {
134-
if !self.contains(&id) {
134+
Some(clean::ResolvedPath{ did, _ }) => {
135+
if is_local(did) && !self.contains(&did.node) {
135136
return None;
136137
}
137138
}

0 commit comments

Comments
 (0)