Skip to content

rustdoc: never link to unnamable items #143849

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ pub(crate) enum HrefError {
Private,
// Not in external cache, href link should be in same page
NotInExternalCache,
/// Refers to an unnamable item, such as one defined within a function or const block.
UnnamableItem,
}

// Panics if `syms` is empty.
Expand Down Expand Up @@ -509,7 +511,13 @@ fn url_parts(
builder.extend(module_fqp.iter().copied());
Ok(builder)
}
ExternalLocation::Local => Ok(href_relative_parts(module_fqp, relative_to)),
ExternalLocation::Local => {
if module_fqp.iter().any(|sym| sym.as_str() == "_") {
Err(HrefError::UnnamableItem)
} else {
Ok(href_relative_parts(module_fqp, relative_to))
}
}
ExternalLocation::Unknown => Err(HrefError::DocumentationNotBuilt),
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/rustdoc/reexport/auxiliary/wrap-unnamable-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub trait Assoc {
type Ty;
}

pub struct Foo(<Foo as crate::Assoc>::Ty);

const _: () = {
impl crate::Assoc for Foo {
type Ty = Bar;
}
pub struct Bar;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have this as a dependency for this test? Can't it be done within the current crate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what happens if the const is named X?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug only happens during cross-crate re-exports. Otherwise a separate failsafe kicks in.

I was under the impression that the _ was a delibraerately injected placeholder, but maybe it is just the constant name, if so we'll need a more principled approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah, you're right, my assumptions were wrong. Just looking at the path isn't enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check what kind of item (block more precisely) the parent is. If it's not a module, then it's likely wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any idea what datastructure would retain that info in the cross-crate case? I'm somewhat struggling to understand how we even reconstruct path info and such from an rlib.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path doesn't matter here, you need to check what is its parent.

12 changes: 12 additions & 0 deletions tests/rustdoc/reexport/wrapped-unnamble-type-143222.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ compile-flags: -Z normalize-docs --document-private-items -Zunstable-options --show-type-layout
//@ aux-build:wrap-unnamable-type.rs
//@ build-aux-docs

#![crate_name = "foo"]

extern crate wrap_unnamable_type as helper;
//extern crate helper;
//@ has 'foo/struct.Foo.html'
//@ !hasraw - '_/struct.Bar.html'
#[doc(inline)]
pub use helper::Foo;
Loading