Skip to content

rustdoc: Give a more accurate span for anchor failures #84203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,20 +1957,28 @@ fn resolution_failure(

/// Report an anchor failure.
fn anchor_failure(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>, failure: AnchorFailure) {
let msg = match failure {
let (msg, anchor_idx) = match failure {
AnchorFailure::MultipleAnchors => {
format!("`{}` contains multiple anchors", diag_info.ori_link)
(format!("`{}` contains multiple anchors", diag_info.ori_link), 1)
}
AnchorFailure::RustdocAnchorConflict(res) => format!(
"`{}` contains an anchor, but links to {kind}s are already anchored",
diag_info.ori_link,
kind = res.descr(),
AnchorFailure::RustdocAnchorConflict(res) => (
format!(
"`{}` contains an anchor, but links to {kind}s are already anchored",
diag_info.ori_link,
kind = res.descr(),
),
0,
),
};

report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &diag_info, |diag, sp| {
if let Some(sp) = sp {
diag.span_label(sp, "contains invalid anchor");
if let Some(mut sp) = sp {
if let Some((fragment_offset, _)) =
diag_info.ori_link.char_indices().filter(|(_, x)| *x == '#').nth(anchor_idx)
{
sp = sp.with_lo(sp.lo() + rustc_span::BytePos(fragment_offset as _));
}
diag.span_label(sp, "invalid anchor");
}
if let AnchorFailure::RustdocAnchorConflict(Res::Primitive(_)) = failure {
diag.note("this restriction may be lifted in a future release");
Expand Down
20 changes: 15 additions & 5 deletions src/test/rustdoc-ui/intra-doc/anchors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: `prim@usize#x` contains an anchor, but links to builtin types are already
--> $DIR/anchors.rs:47:6
|
LL | /// [prim@usize#x]
| ^^^^^^^^^^^^ contains invalid anchor
| ^^^^^^^^^^--
| |
| invalid anchor
|
note: the lint level is defined here
--> $DIR/anchors.rs:1:9
Expand All @@ -16,25 +18,33 @@ error: `Foo::f#hola` contains an anchor, but links to fields are already anchore
--> $DIR/anchors.rs:25:15
|
LL | /// Or maybe [Foo::f#hola].
| ^^^^^^^^^^^ contains invalid anchor
| ^^^^^^-----
| |
| invalid anchor

error: `hello#people#!` contains multiple anchors
--> $DIR/anchors.rs:31:28
|
LL | /// Another anchor error: [hello#people#!].
| ^^^^^^^^^^^^^^ contains invalid anchor
| ^^^^^^^^^^^^--
| |
| invalid anchor

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

error: `u32#hello` contains an anchor, but links to builtin types are already anchored
--> $DIR/anchors.rs:43:6
|
LL | /// [u32#hello]
| ^^^^^^^^^ contains invalid anchor
| ^^^------
| |
| invalid anchor
|
= note: this restriction may be lifted in a future release
= note: see https://github.com/rust-lang/rust/issues/83083 for more information
Expand Down
4 changes: 3 additions & 1 deletion src/test/rustdoc-ui/intra-doc/double-anchor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ warning: `with#anchor#error` contains multiple anchors
--> $DIR/double-anchor.rs:5:18
|
LL | /// docs [label][with#anchor#error]
| ^^^^^^^^^^^^^^^^^ contains invalid anchor
| ^^^^^^^^^^^------
| |
| invalid anchor
|
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default

Expand Down