Skip to content

Commit d919a5a

Browse files
authored
Rollup merge of #84203 - jyn514:anchor-span, r=euclio
rustdoc: Give a more accurate span for anchor failures r? `@euclio`
2 parents b5ce9c4 + 59546ef commit d919a5a

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1957,20 +1957,28 @@ fn resolution_failure(
19571957

19581958
/// Report an anchor failure.
19591959
fn anchor_failure(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>, failure: AnchorFailure) {
1960-
let msg = match failure {
1960+
let (msg, anchor_idx) = match failure {
19611961
AnchorFailure::MultipleAnchors => {
1962-
format!("`{}` contains multiple anchors", diag_info.ori_link)
1962+
(format!("`{}` contains multiple anchors", diag_info.ori_link), 1)
19631963
}
1964-
AnchorFailure::RustdocAnchorConflict(res) => format!(
1965-
"`{}` contains an anchor, but links to {kind}s are already anchored",
1966-
diag_info.ori_link,
1967-
kind = res.descr(),
1964+
AnchorFailure::RustdocAnchorConflict(res) => (
1965+
format!(
1966+
"`{}` contains an anchor, but links to {kind}s are already anchored",
1967+
diag_info.ori_link,
1968+
kind = res.descr(),
1969+
),
1970+
0,
19681971
),
19691972
};
19701973

19711974
report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &diag_info, |diag, sp| {
1972-
if let Some(sp) = sp {
1973-
diag.span_label(sp, "contains invalid anchor");
1975+
if let Some(mut sp) = sp {
1976+
if let Some((fragment_offset, _)) =
1977+
diag_info.ori_link.char_indices().filter(|(_, x)| *x == '#').nth(anchor_idx)
1978+
{
1979+
sp = sp.with_lo(sp.lo() + rustc_span::BytePos(fragment_offset as _));
1980+
}
1981+
diag.span_label(sp, "invalid anchor");
19741982
}
19751983
if let AnchorFailure::RustdocAnchorConflict(Res::Primitive(_)) = failure {
19761984
diag.note("this restriction may be lifted in a future release");

src/test/rustdoc-ui/intra-doc/anchors.stderr

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: `prim@usize#x` contains an anchor, but links to builtin types are already
22
--> $DIR/anchors.rs:47:6
33
|
44
LL | /// [prim@usize#x]
5-
| ^^^^^^^^^^^^ contains invalid anchor
5+
| ^^^^^^^^^^--
6+
| |
7+
| invalid anchor
68
|
79
note: the lint level is defined here
810
--> $DIR/anchors.rs:1:9
@@ -16,25 +18,33 @@ error: `Foo::f#hola` contains an anchor, but links to fields are already anchore
1618
--> $DIR/anchors.rs:25:15
1719
|
1820
LL | /// Or maybe [Foo::f#hola].
19-
| ^^^^^^^^^^^ contains invalid anchor
21+
| ^^^^^^-----
22+
| |
23+
| invalid anchor
2024

2125
error: `hello#people#!` contains multiple anchors
2226
--> $DIR/anchors.rs:31:28
2327
|
2428
LL | /// Another anchor error: [hello#people#!].
25-
| ^^^^^^^^^^^^^^ contains invalid anchor
29+
| ^^^^^^^^^^^^--
30+
| |
31+
| invalid anchor
2632

2733
error: `Enum::A#whatever` contains an anchor, but links to variants are already anchored
2834
--> $DIR/anchors.rs:37:28
2935
|
3036
LL | /// Damn enum's variants: [Enum::A#whatever].
31-
| ^^^^^^^^^^^^^^^^ contains invalid anchor
37+
| ^^^^^^^---------
38+
| |
39+
| invalid anchor
3240

3341
error: `u32#hello` contains an anchor, but links to builtin types are already anchored
3442
--> $DIR/anchors.rs:43:6
3543
|
3644
LL | /// [u32#hello]
37-
| ^^^^^^^^^ contains invalid anchor
45+
| ^^^------
46+
| |
47+
| invalid anchor
3848
|
3949
= note: this restriction may be lifted in a future release
4050
= note: see https://github.com/rust-lang/rust/issues/83083 for more information

src/test/rustdoc-ui/intra-doc/double-anchor.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ warning: `with#anchor#error` contains multiple anchors
22
--> $DIR/double-anchor.rs:5:18
33
|
44
LL | /// docs [label][with#anchor#error]
5-
| ^^^^^^^^^^^^^^^^^ contains invalid anchor
5+
| ^^^^^^^^^^^------
6+
| |
7+
| invalid anchor
68
|
79
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
810

0 commit comments

Comments
 (0)