Repository navigation
rustdoc: Suggest corrections for reference label typos - #165568
Conversation
|
rustbot has assigned @lolbinarycat. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
9ccf48a to
3124cb3
Compare
|
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. |
| 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) |
There was a problem hiding this comment.
note that reference link definitions are case-insensitive, unsure if that's relevant here.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
can we name range more descriptively? is it the range of the link reference or the link definition? or something else?
There was a problem hiding this comment.
makes sense, during working on this I've stumbled upon following diagnostics help behaviour. I think this is wrong:

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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
i think span could be more descriptive too.
| if from_expansion { | ||
| return None; | ||
| } |
There was a problem hiding this comment.
is this actually needed? remember that include_str is a macro...
it would be nice if this could work with macros.
There was a problem hiding this comment.
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]>, |
There was a problem hiding this comment.
| 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.
| // 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; |
There was a problem hiding this comment.
| // 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() { |
There was a problem hiding this comment.
why are we short-circuiting for labels on length 1? [x] is a valid link definition...
if this is unintended:
| 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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
| 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.
This comment has been minimized.
This comment has been minimized.
a60c5b9 to
7686ab0
Compare
|
now that we support macros, please add a test for the macro case. |
cd19a2c to
09402ca
Compare
|
@lolbinarycat I've added more test coverage, LGTY ? |
|
Thanks! @bors r+ |
…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" />
…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" />
…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)
…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" />
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)
…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" />
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)
…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" />
…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" />
…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`)
…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`)
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" />
…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`)
View all comments
Addresses point 2 from #132788