Skip to content

Commit e850316

Browse files
committed
auto merge of #14068 : alexcrichton/rust/rustdoc-xcrate-links, r=brson
This should improve the libcore experience quite a bit when looking at the libstd documentation.
2 parents 061450d + 620b435 commit e850316

File tree

11 files changed

+153
-138
lines changed

11 files changed

+153
-138
lines changed

src/doc/rust.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ This document does not serve as a tutorial introduction to the
1717
language. Background familiarity with the language is assumed. A separate
1818
[tutorial] document is available to help acquire such background familiarity.
1919

20-
This document also does not serve as a reference to the [standard] or [extra]
21-
libraries included in the language distribution. Those libraries are
20+
This document also does not serve as a reference to the [standard]
21+
library included in the language distribution. Those libraries are
2222
documented separately by extracting documentation attributes from their
2323
source code.
2424

2525
[tutorial]: tutorial.html
2626
[standard]: std/index.html
27-
[extra]: extra/index.html
2827

2928
## Disclaimer
3029

src/libcore/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//!
2525
//! Functions return `Result` whenever errors are expected and
2626
//! recoverable. In the `std` crate `Result` is most prominently used
27-
//! for [I/O](../io/index.html).
27+
//! for [I/O](../../std/io/index.html).
2828
//!
2929
//! A simple function returning `Result` might be
3030
//! defined and used like so:

src/librustdoc/clean.rs

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,7 @@ pub enum Type {
670670
ResolvedPath {
671671
pub path: Path,
672672
pub typarams: Option<Vec<TyParamBound>>,
673-
pub id: ast::NodeId,
674-
},
675-
/// Same as above, but only external variants
676-
ExternalPath {
677-
pub path: Path,
678-
pub typarams: Option<Vec<TyParamBound>>,
679-
pub fqn: Vec<~str>,
680-
pub kind: TypeKind,
681-
pub krate: ast::CrateNum,
673+
pub did: ast::DefId,
682674
},
683675
// I have no idea how to usefully use this.
684676
TyParamBinder(ast::NodeId),
@@ -715,19 +707,18 @@ pub enum Type {
715707

716708
#[deriving(Clone, Encodable, Decodable)]
717709
pub enum TypeKind {
718-
TypeStruct,
719710
TypeEnum,
720-
TypeTrait,
721711
TypeFunction,
712+
TypeModule,
713+
TypeStatic,
714+
TypeStruct,
715+
TypeTrait,
716+
TypeVariant,
722717
}
723718

724719
impl Clean<Type> for ast::Ty {
725720
fn clean(&self) -> Type {
726721
use syntax::ast::*;
727-
debug!("cleaning type `{:?}`", self);
728-
let ctxt = super::ctxtkey.get().unwrap();
729-
let codemap = ctxt.sess().codemap();
730-
debug!("span corresponds to `{}`", codemap.span_to_str(self.span));
731722
match self.node {
732723
TyNil => Unit,
733724
TyPtr(ref m) => RawPointer(m.mutbl.clean(), box m.ty.clean()),
@@ -1153,7 +1144,7 @@ pub enum ViewPath {
11531144
// use source::*;
11541145
GlobImport(ImportSource),
11551146
// use source::{a, b, c};
1156-
ImportList(ImportSource, Vec<ViewListIdent> ),
1147+
ImportList(ImportSource, Vec<ViewListIdent>),
11571148
}
11581149

11591150
#[deriving(Clone, Encodable, Decodable)]
@@ -1298,48 +1289,47 @@ fn resolve_type(path: Path, tpbs: Option<Vec<TyParamBound> >,
12981289
core::NotTyped(_) => return Bool
12991290
};
13001291
debug!("searching for {:?} in defmap", id);
1301-
let d = match tycx.def_map.borrow().find(&id) {
1292+
let def = match tycx.def_map.borrow().find(&id) {
13021293
Some(&k) => k,
1303-
None => {
1304-
debug!("could not find {:?} in defmap (`{}`)", id, tycx.map.node_to_str(id));
1305-
fail!("Unexpected failure: unresolved id not in defmap (this is a bug!)")
1306-
}
1294+
None => fail!("unresolved id not in defmap")
13071295
};
13081296

1309-
let (def_id, kind) = match d {
1310-
ast::DefFn(i, _) => (i, TypeFunction),
1297+
match def {
13111298
ast::DefSelfTy(i) => return Self(i),
1312-
ast::DefTy(i) => (i, TypeEnum),
1313-
ast::DefTrait(i) => {
1314-
debug!("saw DefTrait in def_to_id");
1315-
(i, TypeTrait)
1316-
},
13171299
ast::DefPrimTy(p) => match p {
13181300
ast::TyStr => return String,
13191301
ast::TyBool => return Bool,
13201302
_ => return Primitive(p)
13211303
},
13221304
ast::DefTyParam(i, _) => return Generic(i.node),
1305+
ast::DefTyParamBinder(i) => return TyParamBinder(i),
1306+
_ => {}
1307+
};
1308+
let did = register_def(&**cx, def);
1309+
ResolvedPath { path: path, typarams: tpbs, did: did }
1310+
}
1311+
1312+
fn register_def(cx: &core::DocContext, def: ast::Def) -> ast::DefId {
1313+
let (did, kind) = match def {
1314+
ast::DefFn(i, _) => (i, TypeFunction),
1315+
ast::DefTy(i) => (i, TypeEnum),
1316+
ast::DefTrait(i) => (i, TypeTrait),
13231317
ast::DefStruct(i) => (i, TypeStruct),
1324-
ast::DefTyParamBinder(i) => {
1325-
debug!("found a typaram_binder, what is it? {}", i);
1326-
return TyParamBinder(i);
1327-
},
1328-
x => fail!("resolved type maps to a weird def {:?}", x),
1318+
ast::DefMod(i) => (i, TypeModule),
1319+
ast::DefStatic(i, _) => (i, TypeStatic),
1320+
ast::DefVariant(i, _, _) => (i, TypeEnum),
1321+
_ => return ast_util::def_id_of_def(def),
13291322
};
1330-
if ast_util::is_local(def_id) {
1331-
ResolvedPath{ path: path, typarams: tpbs, id: def_id.node }
1332-
} else {
1333-
let fqn = csearch::get_item_path(tycx, def_id);
1334-
let fqn = fqn.move_iter().map(|i| i.to_str()).collect();
1335-
ExternalPath {
1336-
path: path,
1337-
typarams: tpbs,
1338-
fqn: fqn,
1339-
kind: kind,
1340-
krate: def_id.krate,
1341-
}
1342-
}
1323+
if ast_util::is_local(did) { return did }
1324+
let tcx = match cx.maybe_typed {
1325+
core::Typed(ref t) => t,
1326+
core::NotTyped(_) => return did
1327+
};
1328+
let fqn = csearch::get_item_path(tcx, did);
1329+
let fqn = fqn.move_iter().map(|i| i.to_str()).collect();
1330+
debug!("recording {} => {}", did, fqn);
1331+
cx.external_paths.borrow_mut().get_mut_ref().insert(did, (fqn, kind));
1332+
return did;
13431333
}
13441334

13451335
fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
@@ -1353,7 +1343,7 @@ fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
13531343
let cx = super::ctxtkey.get().unwrap();
13541344
match cx.maybe_typed {
13551345
core::Typed(ref tcx) => {
1356-
tcx.def_map.borrow().find(&id).map(|&d| ast_util::def_id_of_def(d))
1346+
tcx.def_map.borrow().find(&id).map(|&def| register_def(&**cx, def))
13571347
}
13581348
core::NotTyped(_) => None
13591349
}

src/librustdoc/core.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax;
2020

2121
use std::cell::RefCell;
2222
use std::os;
23-
use collections::HashSet;
23+
use collections::{HashSet, HashMap};
2424

2525
use visit_ast::RustdocVisitor;
2626
use clean;
@@ -31,10 +31,14 @@ pub enum MaybeTyped {
3131
NotTyped(driver::session::Session)
3232
}
3333

34+
pub type ExternalPaths = RefCell<Option<HashMap<ast::DefId,
35+
(Vec<~str>, clean::TypeKind)>>>;
36+
3437
pub struct DocContext {
3538
pub krate: ast::Crate,
3639
pub maybe_typed: MaybeTyped,
3740
pub src: Path,
41+
pub external_paths: ExternalPaths,
3842
}
3943

4044
impl DocContext {
@@ -49,6 +53,7 @@ impl DocContext {
4953
pub struct CrateAnalysis {
5054
pub exported_items: privacy::ExportedItems,
5155
pub public_items: privacy::PublicItems,
56+
pub external_paths: ExternalPaths,
5257
}
5358

5459
/// Parses, resolves, and typechecks the given crate
@@ -98,9 +103,11 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<~str>)
98103
krate: krate,
99104
maybe_typed: Typed(ty_cx),
100105
src: cpath.clone(),
106+
external_paths: RefCell::new(Some(HashMap::new())),
101107
}, CrateAnalysis {
102108
exported_items: exported_items,
103109
public_items: public_items,
110+
external_paths: RefCell::new(None),
104111
})
105112
}
106113

@@ -116,5 +123,7 @@ pub fn run_core(libs: HashSet<Path>, cfgs: Vec<~str>, path: &Path)
116123
v.clean()
117124
};
118125

126+
let external_paths = ctxt.external_paths.borrow_mut().take();
127+
*analysis.external_paths.borrow_mut() = external_paths;
119128
(krate, analysis)
120129
}

src/librustdoc/html/format.rs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -146,41 +146,28 @@ impl fmt::Show for clean::Path {
146146

147147
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
148148
/// rendering function with the necessary arguments for linking to a local path.
149-
fn resolved_path(w: &mut io::Writer, id: ast::NodeId, p: &clean::Path,
149+
fn resolved_path(w: &mut io::Writer, did: ast::DefId, p: &clean::Path,
150150
print_all: bool) -> fmt::Result {
151151
path(w, p, print_all,
152-
|_cache, loc| { Some("../".repeat(loc.len())) },
152+
|cache, loc| {
153+
if ast_util::is_local(did) {
154+
Some("../".repeat(loc.len()))
155+
} else {
156+
match *cache.extern_locations.get(&did.krate) {
157+
render::Remote(ref s) => Some(s.clone()),
158+
render::Local => Some("../".repeat(loc.len())),
159+
render::Unknown => None,
160+
}
161+
}
162+
},
153163
|cache| {
154-
match cache.paths.find(&id) {
164+
match cache.paths.find(&did) {
155165
None => None,
156166
Some(&(ref fqp, shortty)) => Some((fqp.clone(), shortty))
157167
}
158168
})
159169
}
160170

161-
/// Used when rendering an `ExternalPath` structure. Like `resolved_path` this
162-
/// will invoke `path` with proper linking-style arguments.
163-
fn external_path(w: &mut io::Writer, p: &clean::Path, print_all: bool,
164-
fqn: &[~str], kind: clean::TypeKind,
165-
krate: ast::CrateNum) -> fmt::Result {
166-
path(w, p, print_all,
167-
|cache, loc| {
168-
match *cache.extern_locations.get(&krate) {
169-
render::Remote(ref s) => Some(s.clone()),
170-
render::Local => Some("../".repeat(loc.len())),
171-
render::Unknown => None,
172-
}
173-
},
174-
|_cache| {
175-
Some((Vec::from_slice(fqn), match kind {
176-
clean::TypeStruct => item_type::Struct,
177-
clean::TypeEnum => item_type::Enum,
178-
clean::TypeFunction => item_type::Function,
179-
clean::TypeTrait => item_type::Trait,
180-
}))
181-
})
182-
}
183-
184171
fn path(w: &mut io::Writer, path: &clean::Path, print_all: bool,
185172
root: |&render::Cache, &[~str]| -> Option<~str>,
186173
info: |&render::Cache| -> Option<(Vec<~str> , ItemType)>)
@@ -298,15 +285,9 @@ impl fmt::Show for clean::Type {
298285
let m = cache_key.get().unwrap();
299286
f.buf.write(m.typarams.get(&id).as_bytes())
300287
}
301-
clean::ResolvedPath{id, typarams: ref tp, path: ref path} => {
302-
try!(resolved_path(f.buf, id, path, false));
303-
tybounds(f.buf, tp)
304-
}
305-
clean::ExternalPath{path: ref path, typarams: ref tp,
306-
fqn: ref fqn, kind, krate} => {
307-
try!(external_path(f.buf, path, false, fqn.as_slice(), kind,
308-
krate))
309-
tybounds(f.buf, tp)
288+
clean::ResolvedPath{ did, ref typarams, ref path} => {
289+
try!(resolved_path(f.buf, did, path, false));
290+
tybounds(f.buf, typarams)
310291
}
311292
clean::Self(..) => f.buf.write("Self".as_bytes()),
312293
clean::Primitive(prim) => {
@@ -543,10 +524,7 @@ impl fmt::Show for clean::ViewPath {
543524
impl fmt::Show for clean::ImportSource {
544525
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
545526
match self.did {
546-
// FIXME: shouldn't be restricted to just local imports
547-
Some(did) if ast_util::is_local(did) => {
548-
resolved_path(f.buf, did.node, &self.path, true)
549-
}
527+
Some(did) => resolved_path(f.buf, did, &self.path, true),
550528
_ => {
551529
for (i, seg) in self.path.segments.iter().enumerate() {
552530
if i > 0 {
@@ -563,8 +541,7 @@ impl fmt::Show for clean::ImportSource {
563541
impl fmt::Show for clean::ViewListIdent {
564542
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
565543
match self.source {
566-
// FIXME: shouldn't be limited to just local imports
567-
Some(did) if ast_util::is_local(did) => {
544+
Some(did) => {
568545
let path = clean::Path {
569546
global: false,
570547
segments: vec!(clean::PathSegment {
@@ -573,7 +550,7 @@ impl fmt::Show for clean::ViewListIdent {
573550
types: Vec::new(),
574551
})
575552
};
576-
resolved_path(f.buf, did.node, &path, false)
553+
resolved_path(f.buf, did, &path, false)
577554
}
578555
_ => write!(f.buf, "{}", self.name),
579556
}

src/librustdoc/html/item_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ impl ItemType {
4444
match *self {
4545
Module => "mod",
4646
Struct => "struct",
47-
Enum => "enum",
47+
Enum => "type",
4848
Function => "fn",
49-
Typedef => "typedef",
49+
Typedef => "type",
5050
Static => "static",
5151
Trait => "trait",
5252
Impl => "impl",

0 commit comments

Comments
 (0)