Skip to main content

nearest_label

Function nearest_label 

Source
fn nearest_label(
    unknown: &str,
    labels: &BTreeMap<String, LabelTarget>,
) -> Option<String>
Expand description

The single nearest resolvable label to unknown, when one is a reasonable near-miss rather than an unrelated string; the candidate for a “did you mean @intro?” fix on an unknown reference.

“Reasonable” is deliberately conservative:

  • references shorter than three bytes get no suggestion (a one-edit guess on a one- or two-byte name is noise, not help);
  • the edit distance must be within unknown.len() / 3: rustc’s “did you mean” heuristic. With the length floor that bound is always at least 1, admitting intrdointro (distance 1, bound 2) while rejecting wholly unrelated names.

Candidates are the label-index keys that is_reference_label accepts. The index is the resolvable, first-occurrence-wins set, so any surviving candidate both resolves and is spellable as @candidate. Ties break on (distance, label); the BTreeMap already yields labels in sorted order, so the choice is identical on every run and every fixpoint pass.