Skip to content

Commit 96ac7d8

Browse files
authored
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 #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```
2 parents b3a0104 + 88a5e1e commit 96ac7d8

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

Diff for: compiler/rustc_middle/src/ty/assoc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ impl AssocItems {
246246
}
247247

248248
/// Returns an iterator over all associated items with the given name, ignoring hygiene.
249+
///
250+
/// Panics if `name.is_empty()` returns `true`.
249251
pub fn filter_by_name_unhygienic(
250252
&self,
251253
name: Symbol,

Diff for: src/librustdoc/passes/collect_intra_doc_links.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ fn filter_assoc_items_by_name_and_namespace(
5959
ident: Ident,
6060
ns: Namespace,
6161
) -> impl Iterator<Item = &ty::AssocItem> {
62-
tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name).filter(move |item| {
62+
let iter: Box<dyn Iterator<Item = &ty::AssocItem>> = if !ident.name.is_empty() {
63+
Box::new(tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name))
64+
} else {
65+
Box::new([].iter())
66+
};
67+
iter.filter(move |item| {
6368
item.namespace() == ns && tcx.hygienic_eq(ident, item.ident(tcx), assoc_items_of)
6469
})
6570
}

Diff for: tests/rustdoc-ui/intra-doc/empty-associated-items.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test ensures that an empty associated item will not crash rustdoc.
2+
// This is a regression test for <https://github.com/rust-lang/rust/issues/140026>.
3+
4+
#[deny(rustdoc::broken_intra_doc_links)]
5+
6+
/// [`String::`]
7+
//~^ ERROR
8+
pub struct Foo;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unresolved link to `String::`
2+
--> $DIR/empty-associated-items.rs:6:7
3+
|
4+
LL | /// [`String::`]
5+
| ^^^^^^^^ the struct `String` has no field or associated item named ``
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/empty-associated-items.rs:4:8
9+
|
10+
LL | #[deny(rustdoc::broken_intra_doc_links)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)