Skip to content
New issue

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

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

Already on GitHub? # to your account

Self does not work in intra-doc links for primitives #75809

Closed
jyn514 opened this issue Aug 22, 2020 · 5 comments
Closed

Self does not work in intra-doc links for primitives #75809

jyn514 opened this issue Aug 22, 2020 · 5 comments
Assignees
Labels
A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name C-bug Category: This is a bug. P-low Low priority T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Aug 22, 2020

I tried this code in #75807:

#![feature(lang_items)]
#![feature(no_core)]
#![no_core]

#[lang = "usize"]
/// [Self::f]
/// [usize::f]
impl usize {
    /// Some docs
    pub fn f() {}
}

I expected to see this happen: Self::f and usize::f resolve to the same thing.

Instead, this happened: Self::f gives a warning that it can't be resolved.

Meta

rustdoc --version:

rustdoc 1.47.0-nightly (de521cbb3 2020-08-21)

(but the problem is also present on master at time of writing)

@jyn514 jyn514 added P-low Low priority C-bug Category: This is a bug. A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name labels Aug 22, 2020
@jyn514
Copy link
Member Author

jyn514 commented Aug 22, 2020

Marking as P-low since this only affects std and crates with #![lang_items], and there's an easy workaround.

@jyn514 jyn514 added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Aug 22, 2020
@jyn514
Copy link
Member Author

jyn514 commented Aug 22, 2020

I think the issue is actually that Self doesn't work if the item is from a different crate. But it's hard to test until #74489 is fixed (since the only time this is the case is with lang items or implementing a local trait for a foreign type).

// find item's parent to resolve `Self` in item's docs below
let parent_name = self.cx.as_local_hir_id(item.def_id).and_then(|item_hir| {

@jyn514
Copy link
Member Author

jyn514 commented Aug 22, 2020

Ugh, this Self replacement is happening in three different places as usual.

// replace `Self` with suitable item's parent name
if path_str.starts_with("Self::") {
uses the logic I linked above,
if f == "self" || f == "Self" {
and
if f == "self" || f == "Self" {
use
let current_item = match item.inner {
ModuleItem(..) => {
if item.attrs.inner_docs {
if item.def_id.is_top_level_module() { item.name.clone() } else { None }
} else {
match parent_node.or(self.mod_ids.last().copied()) {
Some(parent) if !parent.is_top_level_module() => {
// FIXME: can we pull the parent module's name from elsewhere?
Some(self.cx.tcx.item_name(parent).to_string())
}
_ => None,
}
}
}
ImplItem(Impl { ref for_, .. }) => {
for_.def_id().map(|did| self.cx.tcx.item_name(did).to_string())
}
// we don't display docs on `extern crate` items anyway, so don't process them.
ExternCrateItem(..) => {
debug!("ignoring extern crate item {:?}", item.def_id);
return self.fold_item_recur(item);
}
ImportItem(Import::Simple(ref name, ..)) => Some(name.clone()),
MacroItem(..) => None,
_ => item.name.clone(),
};

@jyn514
Copy link
Member Author

jyn514 commented Aug 22, 2020

There's also a secondary issue: self should never mean a type. At the module level it means the current module; at the function level it means a value, but treating it the same as Self is wrong.

@jyn514
Copy link
Member Author

jyn514 commented Dec 2, 2020

Fixed in #76467.

@jyn514 jyn514 closed this as completed Dec 2, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name C-bug Category: This is a bug. P-low Low priority T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant