Skip to content

Commit edbb0d5

Browse files
committed
Rollup merge of #33196 - mitaa:rdoc-crate-links, r=alexcrichton
rustdoc: Linkify extern crates fixes #33178 r? @alexcrichton
2 parents b50a2ff + 0b5b782 commit edbb0d5

File tree

8 files changed

+79
-13
lines changed

8 files changed

+79
-13
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_trans::back::link;
3838
use rustc::middle::cstore::{self, CrateStore};
3939
use rustc::middle::privacy::AccessLevels;
4040
use rustc::hir::def::Def;
41-
use rustc::hir::def_id::{DefId, DefIndex};
41+
use rustc::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
4242
use rustc::ty::subst::{self, ParamSpace, VecPerParamSpace};
4343
use rustc::ty;
4444
use rustc::middle::stability;
@@ -2388,7 +2388,7 @@ impl Clean<Item> for doctree::ExternCrate {
23882388
name: None,
23892389
attrs: self.attrs.clean(cx),
23902390
source: self.whence.clean(cx),
2391-
def_id: cx.map.local_def_id(0),
2391+
def_id: DefId { krate: self.cnum, index: CRATE_DEF_INDEX },
23922392
visibility: self.vis.clean(cx),
23932393
stability: None,
23942394
deprecation: None,

src/librustdoc/doctree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pub struct Macro {
232232

233233
pub struct ExternCrate {
234234
pub name: Name,
235+
pub cnum: ast::CrateNum,
235236
pub path: Option<String>,
236237
pub vis: hir::Visibility,
237238
pub attrs: hir::HirVec<ast::Attribute>,

src/librustdoc/html/format.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
5757
pub struct CommaSep<'a, T: 'a>(pub &'a [T]);
5858
pub struct AbiSpace(pub Abi);
5959

60+
pub struct HRef<'a> {
61+
pub did: DefId,
62+
pub text: &'a str,
63+
}
64+
6065
impl<'a> VisSpace<'a> {
6166
pub fn get(self) -> &'a Option<clean::Visibility> {
6267
let VisSpace(v) = self; v
@@ -363,15 +368,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
363368
}
364369
}
365370
}
366-
367-
match href(did) {
368-
Some((url, shortty, fqp)) => {
369-
write!(w, "<a class='{}' href='{}' title='{}'>{}</a>",
370-
shortty, url, fqp.join("::"), last.name)?;
371-
}
372-
_ => write!(w, "{}", last.name)?,
373-
}
374-
write!(w, "{}", last.params)?;
371+
write!(w, "{}{}", HRef::new(did, &last.name), last.params)?;
375372
Ok(())
376373
}
377374

@@ -437,6 +434,24 @@ fn tybounds(w: &mut fmt::Formatter,
437434
}
438435
}
439436

437+
impl<'a> HRef<'a> {
438+
pub fn new(did: DefId, text: &'a str) -> HRef<'a> {
439+
HRef { did: did, text: text }
440+
}
441+
}
442+
443+
impl<'a> fmt::Display for HRef<'a> {
444+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
445+
match href(self.did) {
446+
Some((url, shortty, fqp)) => {
447+
write!(f, "<a class='{}' href='{}' title='{}'>{}</a>",
448+
shortty, url, fqp.join("::"), self.text)
449+
}
450+
_ => write!(f, "{}", self.text),
451+
}
452+
}
453+
}
454+
440455
impl fmt::Display for clean::Type {
441456
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
442457
match *self {

src/librustdoc/html/render.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,16 +1739,19 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17391739

17401740
match myitem.inner {
17411741
clean::ExternCrateItem(ref name, ref src) => {
1742+
use html::format::HRef;
1743+
17421744
match *src {
17431745
Some(ref src) => {
17441746
write!(w, "<tr><td><code>{}extern crate {} as {};",
17451747
VisSpace(&myitem.visibility),
1746-
src,
1748+
HRef::new(myitem.def_id, src),
17471749
name)?
17481750
}
17491751
None => {
17501752
write!(w, "<tr><td><code>{}extern crate {};",
1751-
VisSpace(&myitem.visibility), name)?
1753+
VisSpace(&myitem.visibility),
1754+
HRef::new(myitem.def_id, name))?
17521755
}
17531756
}
17541757
write!(w, "</code></td></tr>")?;

src/librustdoc/visit_ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
316316
let name = renamed.unwrap_or(item.name);
317317
match item.node {
318318
hir::ItemExternCrate(ref p) => {
319+
let cstore = &self.cx.sess().cstore;
319320
om.extern_crates.push(ExternCrate {
321+
cnum: cstore.extern_mod_stmt_cnum(item.id)
322+
.unwrap_or(ast::CrateNum::max_value()),
320323
name: name,
321324
path: p.map(|x|x.to_string()),
322325
vis: item.vis.clone(),

src/librustdoc/visit_lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> {
4444

4545
pub fn visit_lib(&mut self, cnum: ast::CrateNum) {
4646
let did = DefId { krate: cnum, index: CRATE_DEF_INDEX };
47+
self.update(did, Some(AccessLevel::Public));
4748
self.visit_mod(did);
4849
}
4950

src/test/rustdoc/issue-33178-1.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 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+
// aux-build:empty.rs
12+
// aux-build:variant-struct.rs
13+
// ignore-cross-compile
14+
15+
// @has issue_33178_1/index.html
16+
// @!has - //a/@title empty
17+
pub extern crate empty;
18+
19+
// @!has - //a/@title variant_struct
20+
pub extern crate variant_struct as foo;

src/test/rustdoc/issue-33178.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 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+
// aux-build:empty.rs
12+
// aux-build:variant-struct.rs
13+
// build-aux-docs
14+
// ignore-cross-compile
15+
16+
// @has issue_33178/index.html
17+
// @has - //a/@title empty
18+
// @has - //a/@href ../empty/index.html
19+
pub extern crate empty;
20+
21+
// @has - //a/@title variant_struct
22+
// @has - //a/@href ../variant_struct/index.html
23+
pub extern crate variant_struct as foo;

0 commit comments

Comments
 (0)