Rollup merge of #140052 - GuillaumeGomez:fix-140026, r=nnethercote

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

Fixes https://github.com/rust-lang/rust/issues/140026.

Assigning ```@nnethercote``` since they're the one who wrote the initial change.

I updated rustdoc code instead of compiler's because I think it makes more sense that the caller ensures on their side that the name they're looking for isn't empty.

r? ```@nnethercote```
This commit is contained in:
Chris Denton 2025-04-21 18:53:18 +00:00 committed by GitHub
commit 96ac7d8b5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 1 deletions

View file

@ -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,

View file

@ -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)
})
}

View file

@ -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;

View file

@ -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