From af1ad419e154b5306c7d89b21218fe35607e9f3a Mon Sep 17 00:00:00 2001 From: mitaa Date: Thu, 3 Dec 2015 23:11:19 +0100 Subject: [PATCH] Use the extern item-path for documentation links The local item-path includes the local crates path to the extern crate declaration which breaks cross-crate rustdoc links if the extern crate is not linked into the crate root or renamed via `extern foo as bar`. --- src/librustc/middle/cstore.rs | 2 ++ src/librustc_metadata/csearch.rs | 18 +++++++++++++++--- src/librustdoc/clean/inline.rs | 2 +- src/test/auxiliary/issue-30109-1.rs | 11 +++++++++++ src/test/rustdoc/issue-30109.rs | 24 ++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/test/auxiliary/issue-30109-1.rs create mode 100644 src/test/rustdoc/issue-30109.rs diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 81375bd3a5a..fba6dea4448 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -142,6 +142,7 @@ fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TypeScheme<'tcx>; fn item_path(&self, def: DefId) -> Vec; + fn extern_item_path(&self, def: DefId) -> Vec; fn item_name(&self, def: DefId) -> ast::Name; fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::GenericPredicates<'tcx>; @@ -295,6 +296,7 @@ fn repr_attrs(&self, def: DefId) -> Vec { unimplemented!() } fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TypeScheme<'tcx> { unimplemented!() } fn item_path(&self, def: DefId) -> Vec { unimplemented!() } + fn extern_item_path(&self, def: DefId) -> Vec { unimplemented!() } fn item_name(&self, def: DefId) -> ast::Name { unimplemented!() } fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::GenericPredicates<'tcx> { unimplemented!() } diff --git a/src/librustc_metadata/csearch.rs b/src/librustc_metadata/csearch.rs index 3c97692ee56..822b11112f2 100644 --- a/src/librustc_metadata/csearch.rs +++ b/src/librustc_metadata/csearch.rs @@ -21,7 +21,7 @@ use middle::ty::{self, Ty}; use middle::def_id::{DefId, DefIndex}; -use rustc::front::map as ast_map; +use rustc::front::map as hir_map; use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet}; use std::cell::RefCell; @@ -29,6 +29,7 @@ use std::path::PathBuf; use syntax::ast; use syntax::attr; +use syntax::parse::token; use rustc_back::svh::Svh; use rustc_back::target::Target; use rustc_front::hir; @@ -115,7 +116,7 @@ fn method_arg_names(&self, did: DefId) -> Vec decoder::get_method_arg_names(&cdata, did.index) } - fn item_path(&self, def: DefId) -> Vec { + fn item_path(&self, def: DefId) -> Vec { let cdata = self.get_crate_data(def.krate); let path = decoder::get_item_path(&*cdata, def.index); @@ -127,6 +128,17 @@ fn item_path(&self, def: DefId) -> Vec { }) } + fn extern_item_path(&self, def: DefId) -> Vec { + let cdata = self.get_crate_data(def.krate); + let path = decoder::get_item_path(&*cdata, def.index); + + let mut r = Vec::with_capacity(path.len() + 1); + let crate_name = hir_map::PathMod(token::intern(&cdata.name)); + r.push(crate_name); + r.push_all(&path); + r + } + fn item_name(&self, def: DefId) -> ast::Name { let cdata = self.get_crate_data(def.krate); decoder::get_item_name(&self.intr, &cdata, def.index) @@ -350,7 +362,7 @@ fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec decoder::get_reachable_ids(&*cdata) } - fn def_path(&self, def: DefId) -> ast_map::DefPath + fn def_path(&self, def: DefId) -> hir_map::DefPath { let cdata = self.get_crate_data(def.krate); let path = decoder::def_path(&*cdata, def.index); diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 40cd5e5bf27..40e71466709 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -138,7 +138,7 @@ pub fn load_attrs(cx: &DocContext, tcx: &ty::ctxt, pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) { match cx.tcx_opt() { Some(tcx) => { - let fqn = tcx.sess.cstore.item_path(did); + let fqn = tcx.sess.cstore.extern_item_path(did); let fqn = fqn.into_iter().map(|i| i.to_string()).collect(); cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind)); } diff --git a/src/test/auxiliary/issue-30109-1.rs b/src/test/auxiliary/issue-30109-1.rs new file mode 100644 index 00000000000..59f952a0b29 --- /dev/null +++ b/src/test/auxiliary/issue-30109-1.rs @@ -0,0 +1,11 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub struct Bar; diff --git a/src/test/rustdoc/issue-30109.rs b/src/test/rustdoc/issue-30109.rs new file mode 100644 index 00000000000..2d33e9323d1 --- /dev/null +++ b/src/test/rustdoc/issue-30109.rs @@ -0,0 +1,24 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// build-aux-docs +// aux-build:issue-30109-1.rs +// ignore-cross-compile + +pub mod quux { + extern crate issue_30109_1 as bar; + use self::bar::Bar; + + pub trait Foo {} + + // @has issue_30109/quux/trait.Foo.html \ + // '//a/@href' '../issue_30109_1/struct.Bar.html' + impl Foo for Bar {} +}