From 88bbe79aa430444f89d9cfde8e25ef17d2ae5798 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 4 Jun 2018 23:02:32 +0200 Subject: [PATCH] Support Self for intra links paths --- src/librustdoc/clean/mod.rs | 8 +++++++- src/librustdoc/core.rs | 4 +++- src/librustdoc/visit_ast.rs | 19 ++++++++++++------- src/test/rustdoc/intra-link-self.rs | 25 +++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/test/rustdoc/intra-link-self.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 1c1ba208678ed..a1fa65cf00fed 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1072,11 +1072,17 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option }; let mut path = if let Some(second) = split.next() { - second + second.to_owned() } else { return Err(()) }; + if path == "self" || path == "Self" { + if let Some(id) = *cx.current_item_id.borrow() { + path = cx.tcx.hir.name(id).as_str().to_string(); + } + } + let ty = cx.resolver.borrow_mut() .with_scope(*id, |resolver| { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 458ed105d2650..1890d11c87897 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -82,7 +82,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> { pub fake_def_ids: RefCell>, pub all_fake_def_ids: RefCell>, /// Maps (type_id, trait_id) -> auto trait impl - pub generated_synthetics: RefCell> + pub generated_synthetics: RefCell>, + pub current_item_id: RefCell>, } impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> { @@ -327,6 +328,7 @@ pub fn run_core(search_paths: SearchPaths, fake_def_ids: RefCell::new(FxHashMap()), all_fake_def_ids: RefCell::new(FxHashSet()), generated_synthetics: RefCell::new(FxHashSet()), + current_item_id: RefCell::new(None), }; debug!("crate: {:?}", tcx.hir.krate()); diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 8c2555c4b3de2..68f4b0b9ce3a1 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -112,6 +112,7 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { name: ast::Name, sd: &hir::VariantData, generics: &hir::Generics) -> Struct { debug!("Visiting struct"); + *self.cx.current_item_id.borrow_mut() = Some(item.id); let struct_type = struct_type_from_def(&*sd); Struct { id: item.id, @@ -131,6 +132,7 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { name: ast::Name, sd: &hir::VariantData, generics: &hir::Generics) -> Union { debug!("Visiting union"); + *self.cx.current_item_id.borrow_mut() = Some(item.id); let struct_type = struct_type_from_def(&*sd); Union { id: item.id, @@ -146,10 +148,11 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { } } - pub fn visit_enum_def(&mut self, it: &hir::Item, + pub fn visit_enum_def(&mut self, item: &hir::Item, name: ast::Name, def: &hir::EnumDef, params: &hir::Generics) -> Enum { debug!("Visiting enum"); + *self.cx.current_item_id.borrow_mut() = Some(item.id); Enum { name, variants: def.variants.iter().map(|v| Variant { @@ -160,13 +163,13 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { def: v.node.data.clone(), whence: v.span, }).collect(), - vis: it.vis.clone(), - stab: self.stability(it.id), - depr: self.deprecation(it.id), + vis: item.vis.clone(), + stab: self.stability(item.id), + depr: self.deprecation(item.id), generics: params.clone(), - attrs: it.attrs.clone(), - id: it.id, - whence: it.span, + attrs: item.attrs.clone(), + id: item.id, + whence: item.span, } } @@ -178,6 +181,7 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { gen: &hir::Generics, body: hir::BodyId) -> Function { debug!("Visiting fn"); + *self.cx.current_item_id.borrow_mut() = Some(item.id); Function { id: item.id, vis: item.vis.clone(), @@ -199,6 +203,7 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> { vis: hir::Visibility, id: ast::NodeId, m: &hir::Mod, name: Option) -> Module { + *self.cx.current_item_id.borrow_mut() = Some(id); let mut om = Module::new(name); om.where_outer = span; om.where_inner = m.inner; diff --git a/src/test/rustdoc/intra-link-self.rs b/src/test/rustdoc/intra-link-self.rs new file mode 100644 index 0000000000000..0c3ba6d8b11fb --- /dev/null +++ b/src/test/rustdoc/intra-link-self.rs @@ -0,0 +1,25 @@ +// Copyright 2018 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. + +#![crate_name = "foo"] + +// @has foo/index.html '//a/@href' '../foo/struct.Foo.html#method.new' +// @has foo/struct.Foo.html '//a/@href' '../foo/struct.Foo.html#method.new' + +/// Use [`new`] to create a new instance. +/// +/// [`new`]: Self::new +pub struct Foo; + +impl Foo { + pub fn new() -> Self { + unimplemented!() + } +}