Skip to content

Commit 9cbc90c

Browse files
committed
Auto merge of #115861 - clubby789:migrate-remove-semi-diag, r=compiler-errors
Make 'remove semi to coerce match' diagnostic translatable
2 parents ae9465f + b663464 commit 9cbc90c

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

compiler/rustc_hir_typeck/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ hir_typeck_option_result_asref = use `{$def_path}::as_ref` to convert `{$expecte
8181
hir_typeck_option_result_cloned = use `{$def_path}::cloned` to clone the value inside the `{$def_path}`
8282
hir_typeck_option_result_copied = use `{$def_path}::copied` to copy the value inside the `{$def_path}`
8383
84+
hir_typeck_remove_semi_for_coerce = you might have meant to return the `match` expression
85+
hir_typeck_remove_semi_for_coerce_expr = this could be implicitly returned but it is a statement, not a tail expression
86+
hir_typeck_remove_semi_for_coerce_ret = the `match` arms can conform to this return type
87+
hir_typeck_remove_semi_for_coerce_semi = the `match` is a statement because of this semicolon, consider removing it
88+
hir_typeck_remove_semi_for_coerce_suggestion = remove this semicolon
89+
8490
hir_typeck_return_stmt_outside_of_fn_body =
8591
{$statement_kind} statement outside of function body
8692
.encl_body_label = the {$statement_kind} is part of this body...

compiler/rustc_hir_typeck/src/_match.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::coercion::{AsCoercionSite, CoerceMany};
22
use crate::{Diverges, Expectation, FnCtxt, Needs};
3-
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
3+
use rustc_errors::Diagnostic;
44
use rustc_hir::{self as hir, ExprKind};
55
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
66
use rustc_infer::traits::Obligation;
@@ -225,24 +225,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
225225
return;
226226
}
227227

228-
let semi_span = expr.span.shrink_to_hi().with_hi(semi_span.hi());
229-
let mut ret_span: MultiSpan = semi_span.into();
230-
ret_span.push_span_label(
231-
expr.span,
232-
"this could be implicitly returned but it is a statement, not a tail expression",
233-
);
234-
ret_span.push_span_label(ret, "the `match` arms can conform to this return type");
235-
ret_span.push_span_label(
236-
semi_span,
237-
"the `match` is a statement because of this semicolon, consider removing it",
238-
);
239-
diag.span_note(ret_span, "you might have meant to return the `match` expression");
240-
diag.tool_only_span_suggestion(
241-
semi_span,
242-
"remove this semicolon",
243-
"",
244-
Applicability::MaybeIncorrect,
245-
);
228+
let semi = expr.span.shrink_to_hi().with_hi(semi_span.hi());
229+
let sugg = crate::errors::RemoveSemiForCoerce { expr: expr.span, ret, semi };
230+
diag.subdiagnostic(sugg);
246231
}
247232

248233
/// When the previously checked expression (the scrutinee) diverges,

compiler/rustc_hir_typeck/src/errors.rs

+26
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ pub enum OptionResultRefMismatch {
292292
// },
293293
}
294294

295+
pub struct RemoveSemiForCoerce {
296+
pub expr: Span,
297+
pub ret: Span,
298+
pub semi: Span,
299+
}
300+
301+
impl AddToDiagnostic for RemoveSemiForCoerce {
302+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
303+
where
304+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
305+
{
306+
let mut multispan: MultiSpan = self.semi.into();
307+
multispan.push_span_label(self.expr, fluent::hir_typeck_remove_semi_for_coerce_expr);
308+
multispan.push_span_label(self.ret, fluent::hir_typeck_remove_semi_for_coerce_ret);
309+
multispan.push_span_label(self.semi, fluent::hir_typeck_remove_semi_for_coerce_semi);
310+
diag.span_note(multispan, fluent::hir_typeck_remove_semi_for_coerce);
311+
312+
diag.tool_only_span_suggestion(
313+
self.semi,
314+
fluent::hir_typeck_remove_semi_for_coerce_suggestion,
315+
"",
316+
Applicability::MaybeIncorrect,
317+
);
318+
}
319+
}
320+
295321
#[derive(Diagnostic)]
296322
#[diag(hir_typeck_const_select_must_be_const)]
297323
#[help]

0 commit comments

Comments
 (0)