Skip to content

Commit ee74f92

Browse files
committed
Replace manual impl with a derive macro as multipart suggestions are now supported by them
1 parent cb7ad9e commit ee74f92

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

compiler/rustc_infer/src/errors/mod.rs

+50-39
Original file line numberDiff line numberDiff line change
@@ -147,56 +147,67 @@ pub enum SourceKindSubdiag<'a> {
147147
},
148148
}
149149

150-
// Has to be implemented manually because multipart suggestions are not supported by the derive macro.
151-
// Would be a part of `SourceKindSubdiag` otherwise.
150+
#[derive(SessionSubdiagnostic)]
152151
pub enum SourceKindMultiSuggestion<'a> {
152+
#[multipart_suggestion_verbose(
153+
infer::source_kind_fully_qualified,
154+
applicability = "has-placeholders"
155+
)]
153156
FullyQualified {
154-
span: Span,
157+
#[suggestion_part(code = "{def_path}({adjustment}")]
158+
span_lo: Span,
159+
#[suggestion_part(code = "{successor_pos}")]
160+
span_hi: Span,
155161
def_path: String,
156162
adjustment: &'a str,
157-
successor: (&'a str, BytePos),
163+
successor_pos: &'a str,
158164
},
165+
#[multipart_suggestion_verbose(
166+
infer::source_kind_closure_return,
167+
applicability = "has-placeholders"
168+
)]
159169
ClosureReturn {
160-
ty_info: String,
161-
data: &'a FnRetTy<'a>,
162-
should_wrap_expr: Option<Span>,
170+
#[suggestion_part(code = "{start_span_code}")]
171+
start_span: Span,
172+
start_span_code: String,
173+
#[suggestion_part(code = "}}")]
174+
end_span: Option<Span>,
163175
},
164176
}
165177

166-
impl AddSubdiagnostic for SourceKindMultiSuggestion<'_> {
167-
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
168-
match self {
169-
Self::FullyQualified { span, def_path, adjustment, successor } => {
170-
let suggestion = vec![
171-
(span.shrink_to_lo(), format!("{def_path}({adjustment}")),
172-
(span.shrink_to_hi().with_hi(successor.1), successor.0.to_string()),
173-
];
174-
diag.multipart_suggestion_verbose(
175-
fluent::infer::source_kind_fully_qualified,
176-
suggestion,
177-
rustc_errors::Applicability::HasPlaceholders,
178-
);
179-
}
180-
Self::ClosureReturn { ty_info, data, should_wrap_expr } => {
181-
let (arrow, post) = match data {
182-
FnRetTy::DefaultReturn(_) => ("-> ", " "),
183-
_ => ("", ""),
184-
};
185-
let suggestion = match should_wrap_expr {
186-
Some(end_span) => vec![
187-
(data.span(), format!("{}{}{}{{ ", arrow, ty_info, post)),
188-
(end_span, " }".to_string()),
189-
],
190-
None => vec![(data.span(), format!("{}{}{}", arrow, ty_info, post))],
191-
};
192-
diag.multipart_suggestion_verbose(
193-
fluent::infer::source_kind_closure_return,
194-
suggestion,
195-
rustc_errors::Applicability::HasPlaceholders,
196-
);
197-
}
178+
impl<'a> SourceKindMultiSuggestion<'a> {
179+
pub fn new_fully_qualified(
180+
span: Span,
181+
def_path: String,
182+
adjustment: &'a str,
183+
successor: (&'a str, BytePos),
184+
) -> Self {
185+
Self::FullyQualified {
186+
span_lo: span.shrink_to_lo(),
187+
span_hi: span.shrink_to_hi().with_hi(successor.1),
188+
def_path,
189+
adjustment,
190+
successor_pos: successor.0,
198191
}
199192
}
193+
194+
pub fn new_closure_return(
195+
ty_info: String,
196+
data: &'a FnRetTy<'a>,
197+
should_wrap_expr: Option<Span>,
198+
) -> Self {
199+
let (arrow, post) = match data {
200+
FnRetTy::DefaultReturn(_) => ("-> ", " "),
201+
_ => ("", ""),
202+
};
203+
let (start_span, start_span_code, end_span) = match should_wrap_expr {
204+
Some(end_span) => {
205+
(data.span(), format!("{}{}{}{{ ", arrow, ty_info, post), Some(end_span))
206+
}
207+
None => (data.span(), format!("{}{}{}", arrow, ty_info, post), None),
208+
};
209+
Self::ClosureReturn { start_span, start_span_code, end_span }
210+
}
200211
}
201212

202213
pub enum RegionOriginNote<'a> {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -511,20 +511,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
511511
_ => "",
512512
};
513513

514-
multi_suggestions.push(SourceKindMultiSuggestion::FullyQualified {
515-
span: receiver.span,
514+
multi_suggestions.push(SourceKindMultiSuggestion::new_fully_qualified(
515+
receiver.span,
516516
def_path,
517517
adjustment,
518518
successor,
519-
});
519+
));
520520
}
521521
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
522522
let ty_info = ty_to_string(self, ty);
523-
multi_suggestions.push(SourceKindMultiSuggestion::ClosureReturn {
523+
multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(
524524
ty_info,
525525
data,
526526
should_wrap_expr,
527-
});
527+
));
528528
}
529529
}
530530
match error_code {

0 commit comments

Comments
 (0)