From ae4b6d6c65c32d41d8017b4e3765a8a1871e8519 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 19 Apr 2025 21:06:52 +0200 Subject: [PATCH 1/3] Update docs for `AssocItems::filter_by_name_unhygienic` --- compiler/rustc_middle/src/ty/assoc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index 0c44fd2758d..78b2e265b48 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -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, From e0437ec364017b11b5e63d1c09e3204029d8b497 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 19 Apr 2025 21:07:24 +0200 Subject: [PATCH 2/3] Fix error when an intra doc link is trying to resolve an empty associated item --- src/librustdoc/passes/collect_intra_doc_links.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 297597b3dea..36f5889dcf4 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -59,7 +59,12 @@ fn filter_assoc_items_by_name_and_namespace( ident: Ident, ns: Namespace, ) -> impl Iterator { - tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name).filter(move |item| { + let iter: Box> = 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) }) } From 88a5e1ef683673c7e1b16b399d745d01ad540a19 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 19 Apr 2025 21:10:40 +0200 Subject: [PATCH 3/3] Add regression test for #140026 --- .../rustdoc-ui/intra-doc/empty-associated-items.rs | 8 ++++++++ .../intra-doc/empty-associated-items.stderr | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/rustdoc-ui/intra-doc/empty-associated-items.rs create mode 100644 tests/rustdoc-ui/intra-doc/empty-associated-items.stderr diff --git a/tests/rustdoc-ui/intra-doc/empty-associated-items.rs b/tests/rustdoc-ui/intra-doc/empty-associated-items.rs new file mode 100644 index 00000000000..ea94cb349ad --- /dev/null +++ b/tests/rustdoc-ui/intra-doc/empty-associated-items.rs @@ -0,0 +1,8 @@ +// This test ensures that an empty associated item will not crash rustdoc. +// This is a regression test for . + +#[deny(rustdoc::broken_intra_doc_links)] + +/// [`String::`] +//~^ ERROR +pub struct Foo; diff --git a/tests/rustdoc-ui/intra-doc/empty-associated-items.stderr b/tests/rustdoc-ui/intra-doc/empty-associated-items.stderr new file mode 100644 index 00000000000..b0527916ab5 --- /dev/null +++ b/tests/rustdoc-ui/intra-doc/empty-associated-items.stderr @@ -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 +