Skip to content

Commit 0ed4849

Browse files
authored
Rollup merge of #77469 - camelid:rustdoc-better-failed-res-error, r=jyn514
Improve rustdoc error for failed intra-doc link resolution The previous error was confusing since it made it sound like you can't link to items that are defined outside the current module. Also suggested importing the item. r? @jyn514
2 parents 9db26b7 + aa9b718 commit 0ed4849

8 files changed

+45
-40
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1576,22 +1576,27 @@ fn resolution_failure(
15761576
};
15771577
// See if this was a module: `[path]` or `[std::io::nope]`
15781578
if let Some(module) = last_found_module {
1579-
let module_name = collector.cx.tcx.item_name(module);
1580-
let note = format!(
1581-
"the module `{}` contains no item named `{}`",
1582-
module_name, unresolved
1583-
);
1579+
let note = if partial_res.is_some() {
1580+
// Part of the link resolved; e.g. `std::io::nonexistent`
1581+
let module_name = collector.cx.tcx.item_name(module);
1582+
format!("no item named `{}` in module `{}`", unresolved, module_name)
1583+
} else {
1584+
// None of the link resolved; e.g. `Notimported`
1585+
format!("no item named `{}` in scope", unresolved)
1586+
};
15841587
if let Some(span) = sp {
15851588
diag.span_label(span, &note);
15861589
} else {
15871590
diag.note(&note);
15881591
}
1592+
15891593
// If the link has `::` in it, assume it was meant to be an intra-doc link.
15901594
// Otherwise, the `[]` might be unrelated.
15911595
// FIXME: don't show this for autolinks (`<>`), `()` style links, or reference links
15921596
if !path_str.contains("::") {
15931597
diag.help(r#"to escape `[` and `]` characters, add '\' before them like `\[` or `\]`"#);
15941598
}
1599+
15951600
continue;
15961601
}
15971602

src/test/rustdoc-ui/deny-intra-link-resolution-failure.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unresolved link to `v2`
22
--> $DIR/deny-intra-link-resolution-failure.rs:3:6
33
|
44
LL | /// [v2]
5-
| ^^ the module `deny_intra_link_resolution_failure` contains no item named `v2`
5+
| ^^ no item named `v2` in scope
66
|
77
note: the lint level is defined here
88
--> $DIR/deny-intra-link-resolution-failure.rs:1:9

src/test/rustdoc-ui/intra-link-errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
77
/// [path::to::nonexistent::module]
88
//~^ ERROR unresolved link
9-
//~| NOTE `intra_link_errors` contains no item named `path`
9+
//~| NOTE no item named `path` in scope
1010

1111
/// [path::to::nonexistent::macro!]
1212
//~^ ERROR unresolved link
13-
//~| NOTE `intra_link_errors` contains no item named `path`
13+
//~| NOTE no item named `path` in scope
1414

1515
/// [type@path::to::nonexistent::type]
1616
//~^ ERROR unresolved link
17-
//~| NOTE `intra_link_errors` contains no item named `path`
17+
//~| NOTE no item named `path` in scope
1818

1919
/// [std::io::not::here]
2020
//~^ ERROR unresolved link
21-
//~| NOTE `io` contains no item named `not`
21+
//~| NOTE no item named `not` in module `io`
2222

2323
/// [type@std::io::not::here]
2424
//~^ ERROR unresolved link
25-
//~| NOTE `io` contains no item named `not`
25+
//~| NOTE no item named `not` in module `io`
2626

2727
/// [std::io::Error::x]
2828
//~^ ERROR unresolved link

src/test/rustdoc-ui/intra-link-errors.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unresolved link to `path::to::nonexistent::module`
22
--> $DIR/intra-link-errors.rs:7:6
33
|
44
LL | /// [path::to::nonexistent::module]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `path` in scope
66
|
77
note: the lint level is defined here
88
--> $DIR/intra-link-errors.rs:1:9
@@ -14,25 +14,25 @@ error: unresolved link to `path::to::nonexistent::macro`
1414
--> $DIR/intra-link-errors.rs:11:6
1515
|
1616
LL | /// [path::to::nonexistent::macro!]
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `path` in scope
1818

1919
error: unresolved link to `path::to::nonexistent::type`
2020
--> $DIR/intra-link-errors.rs:15:6
2121
|
2222
LL | /// [type@path::to::nonexistent::type]
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the module `intra_link_errors` contains no item named `path`
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item named `path` in scope
2424

2525
error: unresolved link to `std::io::not::here`
2626
--> $DIR/intra-link-errors.rs:19:6
2727
|
2828
LL | /// [std::io::not::here]
29-
| ^^^^^^^^^^^^^^^^^^ the module `io` contains no item named `not`
29+
| ^^^^^^^^^^^^^^^^^^ no item named `not` in module `io`
3030

3131
error: unresolved link to `std::io::not::here`
3232
--> $DIR/intra-link-errors.rs:23:6
3333
|
3434
LL | /// [type@std::io::not::here]
35-
| ^^^^^^^^^^^^^^^^^^^^^^^ the module `io` contains no item named `not`
35+
| ^^^^^^^^^^^^^^^^^^^^^^^ no item named `not` in module `io`
3636

3737
error: unresolved link to `std::io::Error::x`
3838
--> $DIR/intra-link-errors.rs:27:6

src/test/rustdoc-ui/intra-link-span-ice-55723.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unresolved link to `i`
22
--> $DIR/intra-link-span-ice-55723.rs:9:10
33
|
44
LL | /// (arr[i])
5-
| ^ the module `intra_link_span_ice_55723` contains no item named `i`
5+
| ^ no item named `i` in scope
66
|
77
note: the lint level is defined here
88
--> $DIR/intra-link-span-ice-55723.rs:1:9

src/test/rustdoc-ui/intra-links-warning-crlf.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unresolved link to `error`
22
--> $DIR/intra-links-warning-crlf.rs:7:6
33
|
44
LL | /// [error]
5-
| ^^^^^ the module `intra_links_warning_crlf` contains no item named `error`
5+
| ^^^^^ no item named `error` in scope
66
|
77
= note: `#[warn(broken_intra_doc_links)]` on by default
88
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
@@ -11,23 +11,23 @@ warning: unresolved link to `error1`
1111
--> $DIR/intra-links-warning-crlf.rs:12:11
1212
|
1313
LL | /// docs [error1]
14-
| ^^^^^^ the module `intra_links_warning_crlf` contains no item named `error1`
14+
| ^^^^^^ no item named `error1` in scope
1515
|
1616
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
1717

1818
warning: unresolved link to `error2`
1919
--> $DIR/intra-links-warning-crlf.rs:15:11
2020
|
2121
LL | /// docs [error2]
22-
| ^^^^^^ the module `intra_links_warning_crlf` contains no item named `error2`
22+
| ^^^^^^ no item named `error2` in scope
2323
|
2424
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
2525

2626
warning: unresolved link to `error`
2727
--> $DIR/intra-links-warning-crlf.rs:23:20
2828
|
2929
LL | * It also has an [error].
30-
| ^^^^^ the module `intra_links_warning_crlf` contains no item named `error`
30+
| ^^^^^ no item named `error` in scope
3131
|
3232
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
3333

src/test/rustdoc-ui/intra-links-warning.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,53 @@ warning: unresolved link to `Bar::foo`
1010
--> $DIR/intra-links-warning.rs:3:35
1111
|
1212
LL | //! Test with [Foo::baz], [Bar::foo], ...
13-
| ^^^^^^^^ the module `intra_links_warning` contains no item named `Bar`
13+
| ^^^^^^^^ no item named `Bar` in scope
1414

1515
warning: unresolved link to `Uniooon::X`
1616
--> $DIR/intra-links-warning.rs:6:13
1717
|
1818
LL | //! , [Uniooon::X] and [Qux::Z].
19-
| ^^^^^^^^^^ the module `intra_links_warning` contains no item named `Uniooon`
19+
| ^^^^^^^^^^ no item named `Uniooon` in scope
2020

2121
warning: unresolved link to `Qux::Z`
2222
--> $DIR/intra-links-warning.rs:6:30
2323
|
2424
LL | //! , [Uniooon::X] and [Qux::Z].
25-
| ^^^^^^ the module `intra_links_warning` contains no item named `Qux`
25+
| ^^^^^^ no item named `Qux` in scope
2626

2727
warning: unresolved link to `Uniooon::X`
2828
--> $DIR/intra-links-warning.rs:10:14
2929
|
3030
LL | //! , [Uniooon::X] and [Qux::Z].
31-
| ^^^^^^^^^^ the module `intra_links_warning` contains no item named `Uniooon`
31+
| ^^^^^^^^^^ no item named `Uniooon` in scope
3232

3333
warning: unresolved link to `Qux::Z`
3434
--> $DIR/intra-links-warning.rs:10:31
3535
|
3636
LL | //! , [Uniooon::X] and [Qux::Z].
37-
| ^^^^^^ the module `intra_links_warning` contains no item named `Qux`
37+
| ^^^^^^ no item named `Qux` in scope
3838

3939
warning: unresolved link to `Qux:Y`
4040
--> $DIR/intra-links-warning.rs:14:13
4141
|
4242
LL | /// [Qux:Y]
43-
| ^^^^^ the module `intra_links_warning` contains no item named `Qux:Y`
43+
| ^^^^^ no item named `Qux:Y` in scope
4444
|
4545
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
4646

4747
warning: unresolved link to `error`
4848
--> $DIR/intra-links-warning.rs:58:30
4949
|
5050
LL | * time to introduce a link [error]*/
51-
| ^^^^^ the module `intra_links_warning` contains no item named `error`
51+
| ^^^^^ no item named `error` in scope
5252
|
5353
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
5454

5555
warning: unresolved link to `error`
5656
--> $DIR/intra-links-warning.rs:64:30
5757
|
5858
LL | * time to introduce a link [error]
59-
| ^^^^^ the module `intra_links_warning` contains no item named `error`
59+
| ^^^^^ no item named `error` in scope
6060
|
6161
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
6262

@@ -70,7 +70,7 @@ LL | #[doc = "single line [error]"]
7070

7171
single line [error]
7272
^^^^^
73-
= note: the module `intra_links_warning` contains no item named `error`
73+
= note: no item named `error` in scope
7474
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
7575

7676
warning: unresolved link to `error`
@@ -83,7 +83,7 @@ LL | #[doc = "single line with \"escaping\" [error]"]
8383

8484
single line with "escaping" [error]
8585
^^^^^
86-
= note: the module `intra_links_warning` contains no item named `error`
86+
= note: no item named `error` in scope
8787
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
8888

8989
warning: unresolved link to `error`
@@ -98,46 +98,46 @@ LL | | /// [error]
9898

9999
[error]
100100
^^^^^
101-
= note: the module `intra_links_warning` contains no item named `error`
101+
= note: no item named `error` in scope
102102
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
103103

104104
warning: unresolved link to `error1`
105105
--> $DIR/intra-links-warning.rs:80:11
106106
|
107107
LL | /// docs [error1]
108-
| ^^^^^^ the module `intra_links_warning` contains no item named `error1`
108+
| ^^^^^^ no item named `error1` in scope
109109
|
110110
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
111111

112112
warning: unresolved link to `error2`
113113
--> $DIR/intra-links-warning.rs:82:11
114114
|
115115
LL | /// docs [error2]
116-
| ^^^^^^ the module `intra_links_warning` contains no item named `error2`
116+
| ^^^^^^ no item named `error2` in scope
117117
|
118118
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
119119

120120
warning: unresolved link to `BarA`
121121
--> $DIR/intra-links-warning.rs:21:10
122122
|
123123
LL | /// bar [BarA] bar
124-
| ^^^^ the module `intra_links_warning` contains no item named `BarA`
124+
| ^^^^ no item named `BarA` in scope
125125
|
126126
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
127127

128128
warning: unresolved link to `BarB`
129129
--> $DIR/intra-links-warning.rs:27:9
130130
|
131131
LL | * bar [BarB] bar
132-
| ^^^^ the module `intra_links_warning` contains no item named `BarB`
132+
| ^^^^ no item named `BarB` in scope
133133
|
134134
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
135135

136136
warning: unresolved link to `BarC`
137137
--> $DIR/intra-links-warning.rs:34:6
138138
|
139139
LL | bar [BarC] bar
140-
| ^^^^ the module `intra_links_warning` contains no item named `BarC`
140+
| ^^^^ no item named `BarC` in scope
141141
|
142142
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
143143

@@ -151,7 +151,7 @@ LL | #[doc = "Foo\nbar [BarD] bar\nbaz"]
151151

152152
bar [BarD] bar
153153
^^^^
154-
= note: the module `intra_links_warning` contains no item named `BarD`
154+
= note: no item named `BarD` in scope
155155
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
156156

157157
warning: unresolved link to `BarF`
@@ -167,7 +167,7 @@ LL | f!("Foo\nbar [BarF] bar\nbaz");
167167

168168
bar [BarF] bar
169169
^^^^
170-
= note: the module `intra_links_warning` contains no item named `BarF`
170+
= note: no item named `BarF` in scope
171171
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
172172
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
173173

src/test/rustdoc-ui/lint-group.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ error: unresolved link to `error`
3232
--> $DIR/lint-group.rs:9:29
3333
|
3434
LL | /// what up, let's make an [error]
35-
| ^^^^^ the module `lint_group` contains no item named `error`
35+
| ^^^^^ no item named `error` in scope
3636
|
3737
note: the lint level is defined here
3838
--> $DIR/lint-group.rs:7:9

0 commit comments

Comments
 (0)