1
Fork 0

Replace manual impl with a derive macro as multipart suggestions are now supported by them

This commit is contained in:
Nikita Tomashevich 2022-09-06 22:18:56 +03:00
parent cb7ad9e548
commit ee74f925f5
No known key found for this signature in database
GPG key ID: B29791D4D878E345
2 changed files with 55 additions and 44 deletions

View file

@ -147,55 +147,66 @@ pub enum SourceKindSubdiag<'a> {
}, },
} }
// Has to be implemented manually because multipart suggestions are not supported by the derive macro. #[derive(SessionSubdiagnostic)]
// Would be a part of `SourceKindSubdiag` otherwise.
pub enum SourceKindMultiSuggestion<'a> { pub enum SourceKindMultiSuggestion<'a> {
#[multipart_suggestion_verbose(
infer::source_kind_fully_qualified,
applicability = "has-placeholders"
)]
FullyQualified { FullyQualified {
#[suggestion_part(code = "{def_path}({adjustment}")]
span_lo: Span,
#[suggestion_part(code = "{successor_pos}")]
span_hi: Span,
def_path: String,
adjustment: &'a str,
successor_pos: &'a str,
},
#[multipart_suggestion_verbose(
infer::source_kind_closure_return,
applicability = "has-placeholders"
)]
ClosureReturn {
#[suggestion_part(code = "{start_span_code}")]
start_span: Span,
start_span_code: String,
#[suggestion_part(code = "}}")]
end_span: Option<Span>,
},
}
impl<'a> SourceKindMultiSuggestion<'a> {
pub fn new_fully_qualified(
span: Span, span: Span,
def_path: String, def_path: String,
adjustment: &'a str, adjustment: &'a str,
successor: (&'a str, BytePos), successor: (&'a str, BytePos),
}, ) -> Self {
ClosureReturn { Self::FullyQualified {
span_lo: span.shrink_to_lo(),
span_hi: span.shrink_to_hi().with_hi(successor.1),
def_path,
adjustment,
successor_pos: successor.0,
}
}
pub fn new_closure_return(
ty_info: String, ty_info: String,
data: &'a FnRetTy<'a>, data: &'a FnRetTy<'a>,
should_wrap_expr: Option<Span>, should_wrap_expr: Option<Span>,
}, ) -> Self {
} let (arrow, post) = match data {
FnRetTy::DefaultReturn(_) => ("-> ", " "),
impl AddSubdiagnostic for SourceKindMultiSuggestion<'_> { _ => ("", ""),
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) { };
match self { let (start_span, start_span_code, end_span) = match should_wrap_expr {
Self::FullyQualified { span, def_path, adjustment, successor } => { Some(end_span) => {
let suggestion = vec![ (data.span(), format!("{}{}{}{{ ", arrow, ty_info, post), Some(end_span))
(span.shrink_to_lo(), format!("{def_path}({adjustment}")),
(span.shrink_to_hi().with_hi(successor.1), successor.0.to_string()),
];
diag.multipart_suggestion_verbose(
fluent::infer::source_kind_fully_qualified,
suggestion,
rustc_errors::Applicability::HasPlaceholders,
);
} }
Self::ClosureReturn { ty_info, data, should_wrap_expr } => { None => (data.span(), format!("{}{}{}", arrow, ty_info, post), None),
let (arrow, post) = match data { };
FnRetTy::DefaultReturn(_) => ("-> ", " "), Self::ClosureReturn { start_span, start_span_code, end_span }
_ => ("", ""),
};
let suggestion = match should_wrap_expr {
Some(end_span) => vec![
(data.span(), format!("{}{}{}{{ ", arrow, ty_info, post)),
(end_span, " }".to_string()),
],
None => vec![(data.span(), format!("{}{}{}", arrow, ty_info, post))],
};
diag.multipart_suggestion_verbose(
fluent::infer::source_kind_closure_return,
suggestion,
rustc_errors::Applicability::HasPlaceholders,
);
}
}
} }
} }

View file

@ -511,20 +511,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
_ => "", _ => "",
}; };
multi_suggestions.push(SourceKindMultiSuggestion::FullyQualified { multi_suggestions.push(SourceKindMultiSuggestion::new_fully_qualified(
span: receiver.span, receiver.span,
def_path, def_path,
adjustment, adjustment,
successor, successor,
}); ));
} }
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => { InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
let ty_info = ty_to_string(self, ty); let ty_info = ty_to_string(self, ty);
multi_suggestions.push(SourceKindMultiSuggestion::ClosureReturn { multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(
ty_info, ty_info,
data, data,
should_wrap_expr, should_wrap_expr,
}); ));
} }
} }
match error_code { match error_code {