Skip to content

rustdoc: Suggest corrections for reference label typos - #165568

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
pkondzior:fix-132788-reference-label-typo
Nov 16, 2026
Merged

rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
pkondzior:fix-132788-reference-label-typo

Conversation

@pkondzior

@pkondzior pkondzior commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Addresses point 2 from #132788

if there is a similarly-named link reference definition, it should suggest referencing that (probable typo)

Screenshot 2026-11-06 at 22 17 26

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Nov 6, 2026
@rustbot

rustbot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

r? @lolbinarycat

rustbot has assigned @lolbinarycat.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: rustdoc
  • rustdoc expanded to 8 candidates
  • Random selection from GuillaumeGomez, fmease, lolbinarycat, notriddle

@rust-bors

This comment has been minimized.

@pkondzior
pkondzior force-pushed the fix-132788-reference-label-typo branch from 9ccf48a to 3124cb3 Compare November 9, 2026 17:33
@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@lolbinarycat lolbinarycat left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good functionally, just some stylistic nits, and some code that's purpose is not clear to me.

View changes since this review

let original_label = diag_info.dox.get(range.clone())?;
let source = tcx.sess.source_map().span_to_snippet(span).ok()?;

(source.as_str() == original_label).then_some(span)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that reference link definitions are case-insensitive, unsure if that's relevant here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right. This check verifies the edit location, so it stays exact. I’ll lowercase labels for typo matching but keep the definition’s spelling in suggestions.

diag_info: &DiagnosticInfo<'_>,
replacement: &str,
) -> Option<rustc_span::Span> {
let MarkdownLinkRange::Destination(range) = &diag_info.link_range else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we name range more descriptively? is it the range of the link reference or the link definition? or something else?

@pkondzior pkondzior Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, during working on this I've stumbled upon following diagnostics help behaviour. I think this is wrong:
Screenshot 2026-11-12 at 09 45 58

Is this correct issue #84081 or should I create separate case for this, I don't want to make a duplicate upfront. Probably much better would be suggesting that in the scope there is already better candidates with similar name instead of trying to import it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd say open a new enhancement issue describing what exactly you think could be done better.

return None;
}

let (span, from_expansion) = source_span_for_markdown_range(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think span could be more descriptive too.

Comment on lines +1099 to +1101
if from_expansion {
return None;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this actually needed? remember that include_str is a macro...

it would be nice if this could work with macros.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added support of this

// If errors are cached then they are only reported on first occurrence
// which we want in some cases but not in others.
cache_errors: bool,
reference_labels: Option<&[String]>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reference_labels: Option<&[String]>,
refdef_labels: Option<&[String]>,

nit: i think this name would be clearer, and ideally would be used instead of reference_labels, to make it clear the labels are sourced from the reference definitions, and not reference links.

Comment on lines +2212 to +2215
// Match the original link label, rather than the path produced by
// Rust-specific preprocessing.
let reference_candidate = reference_labels.and_then(|labels| {
let label = diag_info.ori_link;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Match the original link label, rather than the path produced by
// Rust-specific preprocessing.
let reference_candidate = reference_labels.and_then(|labels| {
let label = diag_info.ori_link;
let reference_candidate = reference_labels.and_then(|labels| {
// Match against the original link label, rather than the path produced by
// Rust-specific preprocessing.
let label = diag_info.ori_link;

let reference_candidate = reference_labels.and_then(|labels| {
let label = diag_info.ori_link;

if labels.is_empty() || label.chars().nth(1).is_none() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we short-circuiting for labels on length 1? [x] is a valid link definition...

if this is unintended:

Suggested change
if labels.is_empty() || label.chars().nth(1).is_none() {
if labels.is_empty() || label.is_empty() {

if this intended: please provide a comment explaining why.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought skipping suggestions for one-character labels was a good idea, but now I agree with you.

let candidates = labels
.iter()
.map(|reference_label| Symbol::intern(reference_label))
.filter(|&candidate| candidate != lookup)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this here? shouldn't there never be candidates that are the same as the label? if there are, doesn't their presence indicate a deeper bug that this would only mask?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is redundant, removing


// Both a replacement and plain help count as reference-label advice.
// Suppress the Rust-item typo and bracket-escaping fallbacks after either.
let suggested_reference_label = if let Some(candidate) = reference_candidate {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let suggested_reference_label = if let Some(candidate) = reference_candidate {
let refdef_label_was_suggested = if let Some(candidate) = reference_candidate {

nit: make it more clear this is a bool, and not a Symbol

the if could also be is_some_and, but i'm not sure that is actually more readable.

@rust-log-analyzer

This comment has been minimized.

@pkondzior
pkondzior force-pushed the fix-132788-reference-label-typo branch from a60c5b9 to 7686ab0 Compare November 12, 2026 10:21
@lolbinarycat

Copy link
Copy Markdown
Contributor

now that we support macros, please add a test for the macro case.

@pkondzior
pkondzior force-pushed the fix-132788-reference-label-typo branch from cd19a2c to 09402ca Compare November 13, 2026 08:50
@pkondzior

Copy link
Copy Markdown
Contributor Author

@lolbinarycat I've added more test coverage, LGTY ?

@lolbinarycat

Copy link
Copy Markdown
Contributor

Thanks!

@bors r+

@rust-bors

rust-bors Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 09402ca has been approved by lolbinarycat

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 13, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 13, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
rust-bors Bot pushed a commit that referenced this pull request Nov 13, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164997 (Fix as str suggestion in subpattern)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 13, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
rust-bors Bot pushed a commit that referenced this pull request Nov 13, 2026
Rollup of 17 pull requests

Successful merges:

 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164808 (asm const ptr: move name generation to asm-specific logic)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165475 (Add a `!type` annotation to a vtable for every supertrait that is a prefix of the vtable)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)
 - #165936 (Enhance documentation for try_clone File implementation)
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 14, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
rust-bors Bot pushed a commit that referenced this pull request Nov 14, 2026
Rollup of 16 pull requests

Successful merges:

 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165475 (Add a `!type` annotation to a vtable for every supertrait that is a prefix of the vtable)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)
 - #165936 (Enhance documentation for try_clone File implementation)
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 14, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 14, 2026
…el-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from rust-lang#132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - #162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164808 (asm const ptr: move name generation to asm-specific logic)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #163696 (Stabilize `int_from_ascii`)
 - #163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - #165893 (Prune un/under used methods of `ast::Attribute`)
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - #162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164808 (asm const ptr: move name generation to asm-specific logic)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #163696 (Stabilize `int_from_ascii`)
 - #163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - #165893 (Prune un/under used methods of `ast::Attribute`)
@rust-bors
rust-bors Bot merged commit b0294b8 into rust-lang:main Nov 16, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 16, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
Rollup merge of #165568 - pkondzior:fix-132788-reference-label-typo, r=lolbinarycat

rustdoc: Suggest corrections for reference label typos

Addresses point 2 from #132788

> if there is a similarly-named [link reference definition](https://spec.commonmark.org/0.31.2/#link-reference-definitions), it should suggest referencing that (probable typo)

<img width="761" height="304" alt="Screenshot 2026-11-06 at 22 17 26" src="https://github.com/user-attachments/assets/997fa9de-91da-4764-b048-ce07547d3ee6" />
sushink70 pushed a commit to sushink70/miri that referenced this pull request Nov 17, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - rust-lang/rust#165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - rust-lang/rust#160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - rust-lang/rust#161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - rust-lang/rust#162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - rust-lang/rust#164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - rust-lang/rust#164808 (asm const ptr: move name generation to asm-specific logic)
 - rust-lang/rust#164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - rust-lang/rust#165568 (rustdoc: Suggest corrections for reference label typos)
 - rust-lang/rust#165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - rust-lang/rust#165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - rust-lang/rust#165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - rust-lang/rust#163696 (Stabilize `int_from_ascii`)
 - rust-lang/rust#163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - rust-lang/rust#164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - rust-lang/rust#164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - rust-lang/rust#164997 (Fix as str suggestion in subpattern)
 - rust-lang/rust#165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - rust-lang/rust#165868 (Small changes to dyn compatibility)
 - rust-lang/rust#165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - rust-lang/rust#165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - rust-lang/rust#165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - rust-lang/rust#165914 (Use `trim_prefix` in bootstrap)
 - rust-lang/rust#165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - rust-lang/rust#165893 (Prune un/under used methods of `ast::Attribute`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants