Skip to content

Fix error when an intra doc link is trying to resolve an empty associated item #140052

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

Merged
merged 3 commits into from
Apr 21, 2025
Merged
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
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/ty/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ impl AssocItems {
}

/// Returns an iterator over all associated items with the given name, ignoring hygiene.
///
/// Panics if `name.is_empty()` returns `true`.
pub fn filter_by_name_unhygienic(
&self,
name: Symbol,
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ fn filter_assoc_items_by_name_and_namespace(
ident: Ident,
ns: Namespace,
) -> impl Iterator<Item = &ty::AssocItem> {
tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name).filter(move |item| {
let iter: Box<dyn Iterator<Item = &ty::AssocItem>> = if !ident.name.is_empty() {
Box::new(tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name))
} else {
Box::new([].iter())
};
iter.filter(move |item| {
item.namespace() == ns && tcx.hygienic_eq(ident, item.ident(tcx), assoc_items_of)
})
}
Expand Down
8 changes: 8 additions & 0 deletions tests/rustdoc-ui/intra-doc/empty-associated-items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This test ensures that an empty associated item will not crash rustdoc.
// This is a regression test for <https://github.com/rust-lang/rust/issues/140026>.

#[deny(rustdoc::broken_intra_doc_links)]

/// [`String::`]
//~^ ERROR
pub struct Foo;
14 changes: 14 additions & 0 deletions tests/rustdoc-ui/intra-doc/empty-associated-items.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unresolved link to `String::`
--> $DIR/empty-associated-items.rs:6:7
|
LL | /// [`String::`]
| ^^^^^^^^ the struct `String` has no field or associated item named ``
|
note: the lint level is defined here
--> $DIR/empty-associated-items.rs:4:8
|
LL | #[deny(rustdoc::broken_intra_doc_links)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Loading