1
Fork 0

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