Skip to content

Support Self for intra links paths #51357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
/// Maps (type_id, trait_id) -> auto trait impl
pub generated_synthetics: RefCell<FxHashSet<(DefId, DefId)>>
pub generated_synthetics: RefCell<FxHashSet<(DefId, DefId)>>,
pub current_item_id: RefCell<Option<NodeId>>,
}

impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> {
Expand Down Expand Up @@ -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());

Expand Down
19 changes: 12 additions & 7 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand All @@ -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(),
Expand All @@ -199,6 +203,7 @@ impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
vis: hir::Visibility, id: ast::NodeId,
m: &hir::Mod,
name: Option<ast::Name>) -> 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;
Expand Down
25 changes: 25 additions & 0 deletions src/test/rustdoc/intra-link-self.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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!()
}
}