Skip to content

Commit d18adb7

Browse files
committed
Migrate SuggestTuplePattern
1 parent 23b8567 commit d18adb7

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

compiler/rustc_infer/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,7 @@ infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` us
364364
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`
365365
366366
infer_sbfrit_change_return_type = you could change the return type to be a boxed trait object
367-
infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions
367+
infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions
368+
369+
infer_stp_wrap_one = try wrapping the pattern in `{$variant}`
370+
infer_stp_wrap_many = try wrapping the pattern in a variant of `{$path}`

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,3 +1335,39 @@ pub enum SuggestBoxingForReturnImplTrait {
13351335
ends: Vec<Span>,
13361336
},
13371337
}
1338+
1339+
#[derive(Subdiagnostic)]
1340+
#[multipart_suggestion(infer_stp_wrap_one, applicability = "maybe-incorrect")]
1341+
pub struct SuggestTuplePatternOne {
1342+
pub variant: String,
1343+
#[suggestion_part(code = "{variant}(")]
1344+
pub span_low: Span,
1345+
#[suggestion_part(code = ")")]
1346+
pub span_high: Span,
1347+
}
1348+
1349+
pub struct SuggestTuplePatternMany {
1350+
pub path: String,
1351+
pub cause_span: Span,
1352+
pub compatible_variants: Vec<String>,
1353+
}
1354+
1355+
impl AddToDiagnostic for SuggestTuplePatternMany {
1356+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
1357+
where
1358+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
1359+
{
1360+
diag.set_arg("path", self.path);
1361+
let message = f(diag, crate::fluent_generated::infer_stp_wrap_many.into());
1362+
diag.multipart_suggestions(
1363+
message,
1364+
self.compatible_variants.into_iter().map(|variant| {
1365+
vec![
1366+
(self.cause_span.shrink_to_lo(), format!("{}(", variant)),
1367+
(self.cause_span.shrink_to_hi(), ")".to_string()),
1368+
]
1369+
}),
1370+
rustc_errors::Applicability::MaybeIncorrect,
1371+
);
1372+
}
1373+
}

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use hir::def::CtorKind;
22
use hir::intravisit::{walk_expr, walk_stmt, Visitor};
33
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_errors::{Applicability, Diagnostic};
4+
use rustc_errors::Diagnostic;
55
use rustc_hir as hir;
66
use rustc_middle::traits::{
77
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
@@ -16,7 +16,7 @@ use crate::errors::{
1616
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
1717
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
1818
SuggestAsRefWhereAppropriate, SuggestBoxingForReturnImplTrait,
19-
SuggestRemoveSemiOrReturnBinding,
19+
SuggestRemoveSemiOrReturnBinding, SuggestTuplePatternMany, SuggestTuplePatternOne,
2020
};
2121

2222
use super::TypeErrCtxt;
@@ -134,30 +134,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
134134
match &compatible_variants[..] {
135135
[] => {}
136136
[variant] => {
137-
diag.multipart_suggestion_verbose(
138-
&format!("try wrapping the pattern in `{}`", variant),
139-
vec![
140-
(cause.span.shrink_to_lo(), format!("{}(", variant)),
141-
(cause.span.shrink_to_hi(), ")".to_string()),
142-
],
143-
Applicability::MaybeIncorrect,
144-
);
137+
let sugg = SuggestTuplePatternOne {
138+
variant: variant.to_owned(),
139+
span_low: cause.span.shrink_to_lo(),
140+
span_high: cause.span.shrink_to_hi(),
141+
};
142+
diag.subdiagnostic(sugg);
145143
}
146144
_ => {
147145
// More than one matching variant.
148-
diag.multipart_suggestions(
149-
&format!(
150-
"try wrapping the pattern in a variant of `{}`",
151-
self.tcx.def_path_str(expected_adt.did())
152-
),
153-
compatible_variants.into_iter().map(|variant| {
154-
vec![
155-
(cause.span.shrink_to_lo(), format!("{}(", variant)),
156-
(cause.span.shrink_to_hi(), ")".to_string()),
157-
]
158-
}),
159-
Applicability::MaybeIncorrect,
160-
);
146+
let sugg = SuggestTuplePatternMany {
147+
path: self.tcx.def_path_str(expected_adt.did()),
148+
cause_span: cause.span,
149+
compatible_variants,
150+
};
151+
diag.subdiagnostic(sugg);
161152
}
162153
}
163154
}

0 commit comments

Comments
 (0)