Handle relative paths

This commit is contained in:
Manish Goregaokar 2018-01-01 13:01:19 +05:30
parent 8166b59c74
commit 4f10f676d9
3 changed files with 61 additions and 43 deletions

View file

@ -472,6 +472,11 @@ impl Clean<Item> for doctree::Module {
"".to_string()
};
// maintain a stack of mod ids
// we could also pass this down through clean()
// but that might complicate things.
cx.mod_ids.borrow_mut().push(self.id);
let mut items: Vec<Item> = vec![];
items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
@ -488,6 +493,8 @@ impl Clean<Item> for doctree::Module {
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
items.extend(self.macros.iter().map(|x| x.clean(cx)));
cx.mod_ids.borrow_mut().pop();
// determine if we should display the inner contents or
// the outer `mod` item for the source code.
let whence = {
@ -847,21 +854,20 @@ impl Clean<Attributes> for [ast::Attribute] {
link.trim()
};
if !path_str.starts_with("::") {
// FIXME (misdreavus): can only support absolute paths because of limitations
// in Resolver. this may, with a lot of effort, figure out how to resolve paths
// within scopes, but the one use of `resolve_hir_path` i found in the HIR
// lowering code itself used an absolute path. we're brushing up against some
// structural limitations in the compiler already, but this may be a design one
// as well >_>
continue;
}
// This allocation could be avoided if resolve_str_path could take an iterator;
// but it can't because that would break object safety. This can still be
// fixed.
let components = path_str.split("::").skip(1).collect::<Vec<_>>();
let resolve = |is_val| cx.resolver.borrow_mut().resolve_str_path_error(DUMMY_SP, None, &components, is_val);
let resolve = |is_val| {
// In case we're in a module, try to resolve the relative
// path
if let Some(id) = cx.mod_ids.borrow().last() {
cx.resolver.borrow_mut()
.with_scope(*id, |resolver| {
resolver.resolve_str_path_error(DUMMY_SP, &path_str, is_val)
})
} else {
// FIXME(Manishearth) this branch doesn't seem to ever be hit, really
cx.resolver.borrow_mut()
.resolve_str_path_error(DUMMY_SP, &path_str, is_val)
}
};
if let Some(is_value) = is_value {
if let Ok(path) = resolve(is_value) {

View file

@ -23,6 +23,7 @@ use rustc_resolve as resolve;
use rustc_metadata::creader::CrateLoader;
use rustc_metadata::cstore::CStore;
use syntax::ast::NodeId;
use syntax::codemap;
use syntax::feature_gate::UnstableFeatures;
use errors;
@ -47,6 +48,8 @@ pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
pub resolver: &'a RefCell<resolve::Resolver<'rcx>>,
/// The stack of module NodeIds up till this point
pub mod_ids: RefCell<Vec<NodeId>>,
pub populated_all_crate_impls: Cell<bool>,
// Note that external items for which `doc(hidden)` applies to are shown as
// non-reachable while local items aren't. This is because we're reusing
@ -243,6 +246,7 @@ pub fn run_core(search_paths: SearchPaths,
render_type,
ty_substs: Default::default(),
lt_substs: Default::default(),
mod_ids: Default::default(),
};
debug!("crate: {:?}", tcx.hir.krate());