Skip to content

Commit

Permalink
Replace manual impl with a derive macro as multipart suggestions are …
Browse files Browse the repository at this point in the history
…now supported by them
  • Loading branch information
IntQuant committed Sep 6, 2022
1 parent cb7ad9e commit ee74f92
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
89 changes: 50 additions & 39 deletions compiler/rustc_infer/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,56 +147,67 @@ pub enum SourceKindSubdiag<'a> {
},
}

// Has to be implemented manually because multipart suggestions are not supported by the derive macro.
// Would be a part of `SourceKindSubdiag` otherwise.
#[derive(SessionSubdiagnostic)]
pub enum SourceKindMultiSuggestion<'a> {
#[multipart_suggestion_verbose(
infer::source_kind_fully_qualified,
applicability = "has-placeholders"
)]
FullyQualified {
span: Span,
#[suggestion_part(code = "{def_path}({adjustment}")]
span_lo: Span,
#[suggestion_part(code = "{successor_pos}")]
span_hi: Span,
def_path: String,
adjustment: &'a str,
successor: (&'a str, BytePos),
successor_pos: &'a str,
},
#[multipart_suggestion_verbose(
infer::source_kind_closure_return,
applicability = "has-placeholders"
)]
ClosureReturn {
ty_info: String,
data: &'a FnRetTy<'a>,
should_wrap_expr: Option<Span>,
#[suggestion_part(code = "{start_span_code}")]
start_span: Span,
start_span_code: String,
#[suggestion_part(code = "}}")]
end_span: Option<Span>,
},
}

impl AddSubdiagnostic for SourceKindMultiSuggestion<'_> {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
match self {
Self::FullyQualified { span, def_path, adjustment, successor } => {
let suggestion = vec![
(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 } => {
let (arrow, post) = match data {
FnRetTy::DefaultReturn(_) => ("-> ", " "),
_ => ("", ""),
};
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,
);
}
impl<'a> SourceKindMultiSuggestion<'a> {
pub fn new_fully_qualified(
span: Span,
def_path: String,
adjustment: &'a str,
successor: (&'a str, BytePos),
) -> Self {
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,
data: &'a FnRetTy<'a>,
should_wrap_expr: Option<Span>,
) -> Self {
let (arrow, post) = match data {
FnRetTy::DefaultReturn(_) => ("-> ", " "),
_ => ("", ""),
};
let (start_span, start_span_code, end_span) = match should_wrap_expr {
Some(end_span) => {
(data.span(), format!("{}{}{}{{ ", arrow, ty_info, post), Some(end_span))
}
None => (data.span(), format!("{}{}{}", arrow, ty_info, post), None),
};
Self::ClosureReturn { start_span, start_span_code, end_span }
}
}

pub enum RegionOriginNote<'a> {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,20 +511,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
_ => "",
};

multi_suggestions.push(SourceKindMultiSuggestion::FullyQualified {
span: receiver.span,
multi_suggestions.push(SourceKindMultiSuggestion::new_fully_qualified(
receiver.span,
def_path,
adjustment,
successor,
});
));
}
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
let ty_info = ty_to_string(self, ty);
multi_suggestions.push(SourceKindMultiSuggestion::ClosureReturn {
multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(
ty_info,
data,
should_wrap_expr,
});
));
}
}
match error_code {
Expand Down

0 comments on commit ee74f92

Please sign in to comment.