Skip to content

Commit 57250ef

Browse files
committed
Use span_label instead of note
This puts the error message closer to the link, making it easier to see what went wrong.
1 parent c213c68 commit 57250ef

File tree

3 files changed

+65
-78
lines changed

3 files changed

+65
-78
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+47-37
Original file line numberDiff line numberDiff line change
@@ -1516,16 +1516,15 @@ fn resolution_failure(
15161516
collector.cx.tcx.item_name(res.def_id()).to_string()
15171517
)
15181518
};
1519-
let assoc_item_not_allowed = |res: Res, diag: &mut DiagnosticBuilder<'_>| {
1519+
let assoc_item_not_allowed = |res: Res| {
15201520
let def_id = res.def_id();
15211521
let name = collector.cx.tcx.item_name(def_id);
1522-
let note = format!(
1522+
format!(
15231523
"`{}` is {} {}, not a module or type, and cannot have associated items",
15241524
name,
15251525
res.article(),
15261526
res.descr()
1527-
);
1528-
diag.note(&note);
1527+
)
15291528
};
15301529
// ignore duplicates
15311530
let mut variants_seen = SmallVec::<[_; 3]>::new();
@@ -1559,29 +1558,29 @@ fn resolution_failure(
15591558
continue;
15601559
}
15611560
variants_seen.push(variant);
1562-
match failure {
1561+
let note = match failure {
15631562
ResolutionFailure::NotInScope { name, .. } => {
15641563
if in_scope {
15651564
continue;
15661565
}
1567-
diag.note(&format!("no item named `{}` is in scope", name));
1566+
// NOTE: uses an explicit `continue` so the `note:` will come before the `help:`
1567+
let note = format!("no item named `{}` is in scope", name);
1568+
if let Some(span) = sp {
1569+
diag.span_label(span, &note);
1570+
} else {
1571+
diag.note(&note);
1572+
}
15681573
// If the link has `::` in the path, assume it's meant to be an intra-doc link
15691574
if !path_str.contains("::") {
15701575
// Otherwise, the `[]` might be unrelated.
15711576
// FIXME(https://github.com/raphlinus/pulldown-cmark/issues/373):
15721577
// don't show this for autolinks (`<>`), `()` style links, or reference links
15731578
diag.help(r#"to escape `[` and `]` characters, add '\' before them like `\[` or `\]`"#);
15741579
}
1580+
continue;
15751581
}
15761582
ResolutionFailure::Dummy => continue,
15771583
ResolutionFailure::WrongNamespace(res, expected_ns) => {
1578-
let note = format!(
1579-
"this link resolves to {}, which is not in the {} namespace",
1580-
item(res),
1581-
expected_ns.descr()
1582-
);
1583-
diag.note(&note);
1584-
15851584
if let Res::Def(kind, _) = res {
15861585
let disambiguator = Disambiguator::Kind(kind);
15871586
suggest_disambiguator(
@@ -1593,24 +1592,26 @@ fn resolution_failure(
15931592
&link_range,
15941593
)
15951594
}
1595+
1596+
format!(
1597+
"this link resolves to {}, which is not in the {} namespace",
1598+
item(res),
1599+
expected_ns.descr()
1600+
)
15961601
}
15971602
ResolutionFailure::NoParentItem => {
15981603
diag.level = rustc_errors::Level::Bug;
1599-
diag.note("all intra doc links should have a parent item");
1600-
}
1601-
ResolutionFailure::NoPrimitiveImpl(res, _) => {
1602-
let note = format!(
1603-
"this link partially resolves to {}, which does not have any associated items",
1604-
item(res),
1605-
);
1606-
diag.note(&note);
1604+
"all intra doc links should have a parent item".to_owned()
16071605
}
1606+
ResolutionFailure::NoPrimitiveImpl(res, _) => format!(
1607+
"this link partially resolves to {}, which does not have any associated items",
1608+
item(res),
1609+
),
16081610
ResolutionFailure::NoPrimitiveAssocItem { prim_name, assoc_item, .. } => {
1609-
let note = format!(
1611+
format!(
16101612
"the builtin type `{}` does not have an associated item named `{}`",
16111613
prim_name, assoc_item
1612-
);
1613-
diag.note(&note);
1614+
)
16141615
}
16151616
ResolutionFailure::NoAssocItem(res, assoc_item) => {
16161617
use DefKind::*;
@@ -1645,32 +1646,41 @@ fn resolution_failure(
16451646
| Use
16461647
| LifetimeParam
16471648
| Ctor(_, _)
1648-
| AnonConst => return assoc_item_not_allowed(res, diag),
1649+
| AnonConst => {
1650+
let note = assoc_item_not_allowed(res);
1651+
if let Some(span) = sp {
1652+
diag.span_label(span, &note);
1653+
} else {
1654+
diag.note(&note);
1655+
}
1656+
return;
1657+
}
16491658
Trait | TyAlias | ForeignTy | OpaqueTy | TraitAlias | TyParam
16501659
| Static => "associated item",
16511660
Impl | GlobalAsm => unreachable!("not a path"),
16521661
}
16531662
};
1654-
let note = format!(
1663+
format!(
16551664
"the {} `{}` has no {} named `{}`",
16561665
res.descr(),
16571666
name,
16581667
path_description,
16591668
assoc_item
1660-
);
1661-
diag.note(&note);
1669+
)
16621670
}
16631671
ResolutionFailure::CannotHaveAssociatedItems(res, _) => {
1664-
assoc_item_not_allowed(res, diag)
1665-
}
1666-
ResolutionFailure::NotAVariant(res, variant) => {
1667-
let note = format!(
1668-
"this link partially resolves to {}, but there is no variant named {}",
1669-
item(res),
1670-
variant
1671-
);
1672-
diag.note(&note);
1672+
assoc_item_not_allowed(res)
16731673
}
1674+
ResolutionFailure::NotAVariant(res, variant) => format!(
1675+
"this link partially resolves to {}, but there is no variant named {}",
1676+
item(res),
1677+
variant
1678+
),
1679+
};
1680+
if let Some(span) = sp {
1681+
diag.span_label(span, &note);
1682+
} else {
1683+
diag.note(&note);
16741684
}
16751685
}
16761686
},

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,33 @@ warning: unresolved link to `error`
22
--> $DIR/intra-links-warning-crlf.rs:7:6
33
|
44
LL | /// [error]
5-
| ^^^^^
5+
| ^^^^^ no item named `error` is in scope
66
|
77
= note: `#[warn(broken_intra_doc_links)]` on by default
8-
= note: no item named `error` is in scope
98
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
109

1110
warning: unresolved link to `error1`
1211
--> $DIR/intra-links-warning-crlf.rs:12:11
1312
|
1413
LL | /// docs [error1]
15-
| ^^^^^^
14+
| ^^^^^^ no item named `error1` is in scope
1615
|
17-
= note: no item named `error1` is in scope
1816
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
1917

2018
warning: unresolved link to `error2`
2119
--> $DIR/intra-links-warning-crlf.rs:15:11
2220
|
2321
LL | /// docs [error2]
24-
| ^^^^^^
22+
| ^^^^^^ no item named `error2` is in scope
2523
|
26-
= note: no item named `error2` is in scope
2724
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
2825

2926
warning: unresolved link to `error`
3027
--> $DIR/intra-links-warning-crlf.rs:23:20
3128
|
3229
LL | * It also has an [error].
33-
| ^^^^^
30+
| ^^^^^ no item named `error` is in scope
3431
|
35-
= note: no item named `error` is in scope
3632
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
3733

3834
warning: 4 warnings emitted

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

+14-33
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,62 @@ warning: unresolved link to `Foo::baz`
22
--> $DIR/intra-links-warning.rs:3:23
33
|
44
LL | //! Test with [Foo::baz], [Bar::foo], ...
5-
| ^^^^^^^^
5+
| ^^^^^^^^ the struct `Foo` has no field or associated item named `baz`
66
|
77
= note: `#[warn(broken_intra_doc_links)]` on by default
8-
= note: the struct `Foo` has no field or associated item named `baz`
98

109
warning: unresolved link to `Bar::foo`
1110
--> $DIR/intra-links-warning.rs:3:35
1211
|
1312
LL | //! Test with [Foo::baz], [Bar::foo], ...
14-
| ^^^^^^^^
15-
|
16-
= note: no item named `Bar` is in scope
13+
| ^^^^^^^^ no item named `Bar` is in scope
1714

1815
warning: unresolved link to `Uniooon::X`
1916
--> $DIR/intra-links-warning.rs:6:13
2017
|
2118
LL | //! , [Uniooon::X] and [Qux::Z].
22-
| ^^^^^^^^^^
23-
|
24-
= note: no item named `Uniooon` is in scope
19+
| ^^^^^^^^^^ no item named `Uniooon` is in scope
2520

2621
warning: unresolved link to `Qux::Z`
2722
--> $DIR/intra-links-warning.rs:6:30
2823
|
2924
LL | //! , [Uniooon::X] and [Qux::Z].
30-
| ^^^^^^
31-
|
32-
= note: no item named `Qux` is in scope
25+
| ^^^^^^ no item named `Qux` is in scope
3326

3427
warning: unresolved link to `Uniooon::X`
3528
--> $DIR/intra-links-warning.rs:10:14
3629
|
3730
LL | //! , [Uniooon::X] and [Qux::Z].
38-
| ^^^^^^^^^^
39-
|
40-
= note: no item named `Uniooon` is in scope
31+
| ^^^^^^^^^^ no item named `Uniooon` is in scope
4132

4233
warning: unresolved link to `Qux::Z`
4334
--> $DIR/intra-links-warning.rs:10:31
4435
|
4536
LL | //! , [Uniooon::X] and [Qux::Z].
46-
| ^^^^^^
47-
|
48-
= note: no item named `Qux` is in scope
37+
| ^^^^^^ no item named `Qux` is in scope
4938

5039
warning: unresolved link to `Qux:Y`
5140
--> $DIR/intra-links-warning.rs:14:13
5241
|
5342
LL | /// [Qux:Y]
54-
| ^^^^^
43+
| ^^^^^ no item named `Qux:Y` is in scope
5544
|
56-
= note: no item named `Qux:Y` is in scope
5745
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
5846

5947
warning: unresolved link to `error`
6048
--> $DIR/intra-links-warning.rs:58:30
6149
|
6250
LL | * time to introduce a link [error]*/
63-
| ^^^^^
51+
| ^^^^^ no item named `error` is in scope
6452
|
65-
= note: no item named `error` is in scope
6653
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
6754

6855
warning: unresolved link to `error`
6956
--> $DIR/intra-links-warning.rs:64:30
7057
|
7158
LL | * time to introduce a link [error]
72-
| ^^^^^
59+
| ^^^^^ no item named `error` is in scope
7360
|
74-
= note: no item named `error` is in scope
7561
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
7662

7763
warning: unresolved link to `error`
@@ -119,45 +105,40 @@ warning: unresolved link to `error1`
119105
--> $DIR/intra-links-warning.rs:80:11
120106
|
121107
LL | /// docs [error1]
122-
| ^^^^^^
108+
| ^^^^^^ no item named `error1` is in scope
123109
|
124-
= note: no item named `error1` is in scope
125110
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
126111

127112
warning: unresolved link to `error2`
128113
--> $DIR/intra-links-warning.rs:82:11
129114
|
130115
LL | /// docs [error2]
131-
| ^^^^^^
116+
| ^^^^^^ no item named `error2` is in scope
132117
|
133-
= note: no item named `error2` is in scope
134118
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
135119

136120
warning: unresolved link to `BarA`
137121
--> $DIR/intra-links-warning.rs:21:10
138122
|
139123
LL | /// bar [BarA] bar
140-
| ^^^^
124+
| ^^^^ no item named `BarA` is in scope
141125
|
142-
= note: no item named `BarA` is in scope
143126
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
144127

145128
warning: unresolved link to `BarB`
146129
--> $DIR/intra-links-warning.rs:27:9
147130
|
148131
LL | * bar [BarB] bar
149-
| ^^^^
132+
| ^^^^ no item named `BarB` is in scope
150133
|
151-
= note: no item named `BarB` is in scope
152134
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
153135

154136
warning: unresolved link to `BarC`
155137
--> $DIR/intra-links-warning.rs:34:6
156138
|
157139
LL | bar [BarC] bar
158-
| ^^^^
140+
| ^^^^ no item named `BarC` is in scope
159141
|
160-
= note: no item named `BarC` is in scope
161142
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
162143

163144
warning: unresolved link to `BarD`

0 commit comments

Comments
 (0)