Migrate SuggestTuplePattern

This commit is contained in:
IQuant 2023-03-03 18:56:45 +03:00
parent 23b8567a78
commit d18adb7d56
3 changed files with 54 additions and 24 deletions

View File

@ -364,4 +364,7 @@ infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` us
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`
infer_sbfrit_change_return_type = you could change the return type to be a boxed trait object
infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions
infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions
infer_stp_wrap_one = try wrapping the pattern in `{$variant}`
infer_stp_wrap_many = try wrapping the pattern in a variant of `{$path}`

View File

@ -1335,3 +1335,39 @@ pub enum SuggestBoxingForReturnImplTrait {
ends: Vec<Span>,
},
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(infer_stp_wrap_one, applicability = "maybe-incorrect")]
pub struct SuggestTuplePatternOne {
pub variant: String,
#[suggestion_part(code = "{variant}(")]
pub span_low: Span,
#[suggestion_part(code = ")")]
pub span_high: Span,
}
pub struct SuggestTuplePatternMany {
pub path: String,
pub cause_span: Span,
pub compatible_variants: Vec<String>,
}
impl AddToDiagnostic for SuggestTuplePatternMany {
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, f: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
diag.set_arg("path", self.path);
let message = f(diag, crate::fluent_generated::infer_stp_wrap_many.into());
diag.multipart_suggestions(
message,
self.compatible_variants.into_iter().map(|variant| {
vec![
(self.cause_span.shrink_to_lo(), format!("{}(", variant)),
(self.cause_span.shrink_to_hi(), ")".to_string()),
]
}),
rustc_errors::Applicability::MaybeIncorrect,
);
}
}

View File

@ -1,7 +1,7 @@
use hir::def::CtorKind;
use hir::intravisit::{walk_expr, walk_stmt, Visitor};
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{Applicability, Diagnostic};
use rustc_errors::Diagnostic;
use rustc_hir as hir;
use rustc_middle::traits::{
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
@ -16,7 +16,7 @@ use crate::errors::{
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
SuggestAsRefWhereAppropriate, SuggestBoxingForReturnImplTrait,
SuggestRemoveSemiOrReturnBinding,
SuggestRemoveSemiOrReturnBinding, SuggestTuplePatternMany, SuggestTuplePatternOne,
};
use super::TypeErrCtxt;
@ -134,30 +134,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
match &compatible_variants[..] {
[] => {}
[variant] => {
diag.multipart_suggestion_verbose(
&format!("try wrapping the pattern in `{}`", variant),
vec![
(cause.span.shrink_to_lo(), format!("{}(", variant)),
(cause.span.shrink_to_hi(), ")".to_string()),
],
Applicability::MaybeIncorrect,
);
let sugg = SuggestTuplePatternOne {
variant: variant.to_owned(),
span_low: cause.span.shrink_to_lo(),
span_high: cause.span.shrink_to_hi(),
};
diag.subdiagnostic(sugg);
}
_ => {
// More than one matching variant.
diag.multipart_suggestions(
&format!(
"try wrapping the pattern in a variant of `{}`",
self.tcx.def_path_str(expected_adt.did())
),
compatible_variants.into_iter().map(|variant| {
vec![
(cause.span.shrink_to_lo(), format!("{}(", variant)),
(cause.span.shrink_to_hi(), ")".to_string()),
]
}),
Applicability::MaybeIncorrect,
);
let sugg = SuggestTuplePatternMany {
path: self.tcx.def_path_str(expected_adt.did()),
cause_span: cause.span,
compatible_variants,
};
diag.subdiagnostic(sugg);
}
}
}