Skip to content

Commit 0a5d1e8

Browse files
authored
Rollup merge of #151255 - fix-rustdoc-ice-reexported-deprecated-note, r=lolbinarycat
rustdoc: Fix ICE when deprecated note is not resolved on the correct `DefId` Supersedes #151237. Follow-up of #151120. The `span` overlapping approach wasn't good enough so instead we now check if the reexport itself has the `deprecated` attribute, and if so, we resolve the path to the reexport `DefId`, otherwise we resolve it on the reexported item's `DefId`. cc @Zalathar r? @lolbinarycat
2 parents 88df8fd + 1faaa76 commit 0a5d1e8

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use rustc_ast::util::comments::may_have_doc_links;
1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1212
use rustc_data_structures::intern::Interned;
1313
use rustc_errors::{Applicability, Diag, DiagMessage};
14+
use rustc_hir::attrs::AttributeKind;
1415
use rustc_hir::def::Namespace::*;
1516
use rustc_hir::def::{DefKind, MacroKinds, Namespace, PerNS};
1617
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE};
17-
use rustc_hir::{Mutability, Safety};
18+
use rustc_hir::{Attribute, Mutability, Safety};
1819
use rustc_middle::ty::{Ty, TyCtxt};
1920
use rustc_middle::{bug, span_bug, ty};
2021
use rustc_resolve::rustdoc::pulldown_cmark::LinkType;
@@ -1108,10 +1109,8 @@ impl LinkCollector<'_, '_> {
11081109

11091110
// Also resolve links in the note text of `#[deprecated]`.
11101111
for attr in &item.attrs.other_attrs {
1111-
let rustc_hir::Attribute::Parsed(rustc_hir::attrs::AttributeKind::Deprecation {
1112-
span,
1113-
deprecation,
1114-
}) = attr
1112+
let Attribute::Parsed(AttributeKind::Deprecation { span: depr_span, deprecation }) =
1113+
attr
11151114
else {
11161115
continue;
11171116
};
@@ -1128,8 +1127,14 @@ impl LinkCollector<'_, '_> {
11281127
// inlined item.
11291128
// <https://github.com/rust-lang/rust/pull/151120>
11301129
let item_id = if let Some(inline_stmt_id) = item.inline_stmt_id
1131-
&& item.span(tcx).is_none_or(|item_span| !item_span.inner().contains(*span))
1132-
{
1130+
&& tcx.get_all_attrs(inline_stmt_id).iter().any(|attr| {
1131+
matches!(
1132+
attr,
1133+
Attribute::Parsed(AttributeKind::Deprecation {
1134+
span: attr_span, ..
1135+
}) if attr_span == depr_span,
1136+
)
1137+
}) {
11331138
inline_stmt_id.to_def_id()
11341139
} else {
11351140
item.item_id.expect_def_id()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test ensures that the intra-doc link from reexported deprecated attribute note
2+
// are resolved where they are declared.
3+
4+
#![deny(rustdoc::broken_intra_doc_links)]
5+
6+
#[doc(inline)]
7+
pub use bar::sql_function_proc as sql_function;
8+
9+
pub fn define_sql_function() {}
10+
11+
pub mod bar {
12+
#[deprecated(note = "Use [`define_sql_function`] instead")]
13+
//~^ ERROR: unresolved link
14+
//~| ERROR: unresolved link
15+
pub fn sql_function_proc() {}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: unresolved link to `define_sql_function`
2+
--> $DIR/deprecated-note-from-reexported.rs:12:25
3+
|
4+
LL | #[deprecated(note = "Use [`define_sql_function`] instead")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the link appears in this line:
8+
9+
Use [`define_sql_function`] instead
10+
^^^^^^^^^^^^^^^^^^^^^
11+
= note: no item named `define_sql_function` in scope
12+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
13+
note: the lint level is defined here
14+
--> $DIR/deprecated-note-from-reexported.rs:4:9
15+
|
16+
LL | #![deny(rustdoc::broken_intra_doc_links)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: unresolved link to `define_sql_function`
20+
--> $DIR/deprecated-note-from-reexported.rs:12:25
21+
|
22+
LL | #[deprecated(note = "Use [`define_sql_function`] instead")]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: the link appears in this line:
26+
27+
Use [`define_sql_function`] instead
28+
^^^^^^^^^^^^^^^^^^^^^
29+
= note: no item named `define_sql_function` in scope
30+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
31+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
32+
33+
error: aborting due to 2 previous errors
34+

0 commit comments

Comments
 (0)