Skip to content

Commit 3101a32

Browse files
committed
Improve typo suggestions
1 parent e2116ac commit 3101a32

File tree

116 files changed

+318
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+318
-259
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,25 @@ impl Diagnostic {
526526
self
527527
}
528528

529+
/// Prints out a message for a suggestion without showing the suggested code if
530+
/// the message is shown inline.
531+
pub fn span_suggestion_hide_inline(
532+
&mut self,
533+
sp: Span,
534+
msg: &str,
535+
suggestion: String,
536+
applicability: Applicability,
537+
) -> &mut Self {
538+
self.span_suggestion_with_style(
539+
sp,
540+
msg,
541+
suggestion,
542+
applicability,
543+
SuggestionStyle::HideCodeInline,
544+
);
545+
self
546+
}
547+
529548
/// Prints out a message for a suggestion without showing the suggested code.
530549
///
531550
/// This is intended to be used for suggestions that are obvious in what the changes need to

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,21 @@ impl<'a> DiagnosticBuilder<'a> {
360360
self
361361
}
362362

363+
/// See [`Diagnostic::span_suggestion_hide_inline()`].
364+
pub fn span_suggestion_hide_inline(
365+
&mut self,
366+
sp: Span,
367+
msg: &str,
368+
suggestion: String,
369+
applicability: Applicability,
370+
) -> &mut Self {
371+
if !self.0.allow_suggestions {
372+
return self;
373+
}
374+
self.0.diagnostic.span_suggestion_hide_inline(sp, msg, suggestion, applicability);
375+
self
376+
}
377+
363378
/// See [`Diagnostic::span_suggestion_hidden()`].
364379
pub fn span_suggestion_hidden(
365380
&mut self,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr;
44
use rustc_ast::{self as ast, Path};
55
use rustc_ast_pretty::pprust;
66
use rustc_data_structures::fx::FxHashSet;
7-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
7+
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, SuggestionStyle};
88
use rustc_feature::BUILTIN_ATTRIBUTES;
99
use rustc_hir::def::Namespace::{self, *};
1010
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
@@ -211,10 +211,14 @@ impl<'a> Resolver<'a> {
211211
);
212212
err.span_label(span, format!("not a member of trait `{}`", trait_));
213213
if let Some(candidate) = candidate {
214-
err.span_suggestion(
214+
let sugg = candidate.to_ident_string();
215+
err.span_suggestion_hide_inline(
215216
method.span,
216-
"there is an associated function with a similar name",
217-
candidate.to_ident_string(),
217+
&format!(
218+
"did you mean `{}`? (a similarly named associated function)",
219+
&sugg
220+
),
221+
sugg,
218222
Applicability::MaybeIncorrect,
219223
);
220224
}
@@ -231,10 +235,11 @@ impl<'a> Resolver<'a> {
231235
);
232236
err.span_label(span, format!("not a member of trait `{}`", trait_));
233237
if let Some(candidate) = candidate {
234-
err.span_suggestion(
238+
let sugg = candidate.to_ident_string();
239+
err.span_suggestion_hide_inline(
235240
type_.span,
236-
"there is an associated type with a similar name",
237-
candidate.to_ident_string(),
241+
&format!("did you mean `{}`? (a similarly named associated type)", &sugg),
242+
sugg,
238243
Applicability::MaybeIncorrect,
239244
);
240245
}
@@ -251,10 +256,14 @@ impl<'a> Resolver<'a> {
251256
);
252257
err.span_label(span, format!("not a member of trait `{}`", trait_));
253258
if let Some(candidate) = candidate {
254-
err.span_suggestion(
259+
let sugg = candidate.to_ident_string();
260+
err.span_suggestion_hide_inline(
255261
const_.span,
256-
"there is an associated constant with a similar name",
257-
candidate.to_ident_string(),
262+
&format!(
263+
"did you mean `{}`? (a similarly named associated constant)",
264+
&sugg
265+
),
266+
sugg,
258267
Applicability::MaybeIncorrect,
259268
);
260269
}
@@ -339,9 +348,9 @@ impl<'a> Resolver<'a> {
339348
// A reachable label with a similar name exists.
340349
Some((ident, true)) => {
341350
err.span_label(ident.span, "a label with a similar name is reachable");
342-
err.span_suggestion(
351+
err.span_suggestion_hide_inline(
343352
span,
344-
"try using similarly named label",
353+
&format!("did you mean `{}`? (a similarly named label)", ident),
345354
ident.name.to_string(),
346355
Applicability::MaybeIncorrect,
347356
);
@@ -583,9 +592,9 @@ impl<'a> Resolver<'a> {
583592
// A reachable label with a similar name exists.
584593
Some((ident, true)) => {
585594
err.span_label(ident.span, "a label with a similar name is reachable");
586-
err.span_suggestion(
595+
err.span_suggestion_hide_inline(
587596
span,
588-
"try using similarly named label",
597+
&format!("did you mean `{}`? (a similarly named label)", ident),
589598
ident.name.to_string(),
590599
Applicability::MaybeIncorrect,
591600
);
@@ -1157,21 +1166,26 @@ impl<'a> Resolver<'a> {
11571166
),
11581167
);
11591168
}
1160-
let msg = match suggestion.target {
1161-
SuggestionTarget::SimilarlyNamed => format!(
1162-
"{} {} with a similar name exists",
1163-
suggestion.res.article(),
1164-
suggestion.res.descr()
1169+
let (msg, style) = match suggestion.target {
1170+
SuggestionTarget::SimilarlyNamed => (
1171+
format!(
1172+
"did you mean `{}`? (a similarly named {})",
1173+
suggestion.candidate,
1174+
suggestion.res.descr()
1175+
),
1176+
SuggestionStyle::HideCodeInline,
1177+
),
1178+
SuggestionTarget::SingleItem => (
1179+
format!("maybe you meant this {}", suggestion.res.descr()),
1180+
SuggestionStyle::ShowCode,
11651181
),
1166-
SuggestionTarget::SingleItem => {
1167-
format!("maybe you meant this {}", suggestion.res.descr())
1168-
}
11691182
};
1170-
err.span_suggestion(
1183+
err.span_suggestion_with_style(
11711184
span,
11721185
&msg,
11731186
suggestion.candidate.to_string(),
11741187
Applicability::MaybeIncorrect,
1188+
style,
11751189
);
11761190
true
11771191
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
571571
..
572572
})) = source
573573
{
574-
err.span_suggestion(
574+
err.span_suggestion_hide_inline(
575575
span,
576-
"use the similarly named label",
576+
&format!(
577+
"did you mean `{}`? (a similarly named label)",
578+
label_ident
579+
),
577580
label_ident.name.to_string(),
578581
Applicability::MaybeIncorrect,
579582
);

compiler/rustc_typeck/src/astconv/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
184184
find_best_match_for_name(&all_candidate_names, assoc_name.name, None),
185185
assoc_name.span != DUMMY_SP,
186186
) {
187-
err.span_suggestion(
187+
err.span_suggestion_hide_inline(
188188
assoc_name.span,
189-
"there is an associated type with a similar name",
189+
&format!("did you mean `{}`? (a similarly named associated type)", suggested_name),
190190
suggested_name.to_string(),
191191
Applicability::MaybeIncorrect,
192192
);

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,9 +1811,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
18111811
assoc_ident.name,
18121812
None,
18131813
) {
1814-
err.span_suggestion(
1814+
err.span_suggestion_hide_inline(
18151815
assoc_ident.span,
1816-
"there is a variant with a similar name",
1816+
&format!(
1817+
"did you mean `{}`? (a similarly named variant)",
1818+
suggested_name
1819+
),
18171820
suggested_name.to_string(),
18181821
Applicability::MaybeIncorrect,
18191822
);

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,9 +1729,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17291729
if let Some(field_name) =
17301730
Self::suggest_field_name(variant, field.ident.name, skip_fields.collect())
17311731
{
1732-
err.span_suggestion(
1732+
err.span_suggestion_hide_inline(
17331733
field.ident.span,
1734-
"a field with a similar name exists",
1734+
&format!("did you mean `{}`? (a similarly named field)", field_name),
17351735
field_name.to_string(),
17361736
Applicability::MaybeIncorrect,
17371737
);
@@ -2125,9 +2125,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21252125
if let Some(suggested_field_name) =
21262126
Self::suggest_field_name(def.non_enum_variant(), field.name, vec![])
21272127
{
2128-
err.span_suggestion(
2128+
err.span_suggestion_hide_inline(
21292129
field.span,
2130-
"a field with a similar name exists",
2130+
&format!("did you mean `{}`? (a similarly named field)", suggested_field_name),
21312131
suggested_field_name.to_string(),
21322132
Applicability::MaybeIncorrect,
21332133
);

compiler/rustc_typeck/src/check/method/suggest.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
981981
item_name.name,
982982
None,
983983
) {
984-
err.span_suggestion(
984+
err.span_suggestion_hide_inline(
985985
span,
986-
"there is a variant with a similar name",
986+
&format!("did you mean `{}`? (a similarly named variant)", suggestion),
987987
suggestion.to_string(),
988988
Applicability::MaybeIncorrect,
989989
);
@@ -1014,11 +1014,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10141014
// that had unsatisfied trait bounds
10151015
if unsatisfied_predicates.is_empty() {
10161016
let def_kind = lev_candidate.kind.as_def_kind();
1017-
err.span_suggestion(
1017+
err.span_suggestion_hide_inline(
10181018
span,
10191019
&format!(
1020-
"there is {} {} with a similar name",
1021-
def_kind.article(),
1020+
"did you mean `{}`? (a similarly named {})",
1021+
lev_candidate.ident,
10221022
def_kind.descr(lev_candidate.def_id),
10231023
),
10241024
lev_candidate.ident.to_string(),

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14891489
unmentioned_fields.iter().map(|(_, field)| field.name).collect::<Vec<_>>();
14901490
let suggested_name = find_best_match_for_name(&input, ident.name, None);
14911491
if let Some(suggested_name) = suggested_name {
1492-
err.span_suggestion(
1492+
err.span_suggestion_hide_inline(
14931493
ident.span,
1494-
"a field with a similar name exists",
1494+
&format!("did you mean `{}`? (a similarly named field)", suggested_name),
14951495
suggested_name.to_string(),
14961496
Applicability::MaybeIncorrect,
14971497
);

src/test/ui/associated-item/associated-item-enum.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | Enum::mispellable();
88
| ^^^^^^^^^^^
99
| |
1010
| variant or associated item not found in `Enum`
11-
| help: there is an associated function with a similar name: `misspellable`
11+
| help: did you mean `misspellable`? (a similarly named associated function)
1212

1313
error[E0599]: no variant or associated item named `mispellable_trait` found for enum `Enum` in the current scope
1414
--> $DIR/associated-item-enum.rs:18:11
@@ -29,7 +29,7 @@ LL | Enum::MISPELLABLE;
2929
| ^^^^^^^^^^^
3030
| |
3131
| variant or associated item not found in `Enum`
32-
| help: there is an associated constant with a similar name: `MISSPELLABLE`
32+
| help: did you mean `MISSPELLABLE`? (a similarly named associated constant)
3333

3434
error: aborting due to 3 previous errors
3535

0 commit comments

Comments
 (0)