Skip to content

Commit bf0193d

Browse files
Suggest adding a semicolon to a closure without block
This transforms `|| expr` into `|| { expr; }`.
1 parent b2eba05 commit bf0193d

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15001500
coercion_error.clone(),
15011501
fcx,
15021502
parent_id,
1503-
expression.map(|expr| (expr, blk_id)),
1503+
expression,
1504+
Some(blk_id),
15041505
);
15051506
if !fcx.tcx.features().unsized_locals {
15061507
unsized_return = self.is_return_ty_unsized(fcx, blk_id);
@@ -1514,6 +1515,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15141515
coercion_error.clone(),
15151516
fcx,
15161517
id,
1518+
expression,
15171519
None,
15181520
);
15191521
if !fcx.tcx.features().unsized_locals {
@@ -1564,21 +1566,30 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15641566
ty_err: TypeError<'tcx>,
15651567
fcx: &FnCtxt<'a, 'tcx>,
15661568
id: hir::HirId,
1567-
expression: Option<(&'tcx hir::Expr<'tcx>, hir::HirId)>,
1569+
expression: Option<&'tcx hir::Expr<'tcx>>,
1570+
blk_id: Option<hir::HirId>,
15681571
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
15691572
let mut err = fcx.report_mismatched_types(cause, expected, found, ty_err);
15701573

15711574
let mut pointing_at_return_type = false;
15721575
let mut fn_output = None;
15731576

1577+
let parent_id = fcx.tcx.hir().get_parent_node(id);
1578+
let parent = fcx.tcx.hir().get(parent_id);
1579+
if let Some(expr) = expression
1580+
&& let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, _, body_id, ..), .. }) = parent
1581+
&& !matches!(fcx.tcx.hir().get(body_id.hir_id), hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Block(..), .. }))
1582+
&& expr.can_have_side_effects()
1583+
&& !in_external_macro(fcx.tcx.sess, expr.span)
1584+
{
1585+
fcx.suggest_missing_semicolon(&mut err, expr, expected, true);
1586+
}
15741587
// Verify that this is a tail expression of a function, otherwise the
15751588
// label pointing out the cause for the type coercion will be wrong
15761589
// as prior return coercions would not be relevant (#57664).
1577-
let parent_id = fcx.tcx.hir().get_parent_node(id);
1578-
let fn_decl = if let Some((expr, blk_id)) = expression {
1590+
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
15791591
pointing_at_return_type =
15801592
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
1581-
let parent = fcx.tcx.hir().get(parent_id);
15821593
if let (Some(cond_expr), true, false) = (
15831594
fcx.tcx.hir().get_if_cause(expr.hir_id),
15841595
expected.is_unit(),
@@ -1607,7 +1618,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16071618
};
16081619

16091620
if let Some((fn_decl, can_suggest)) = fn_decl {
1610-
if expression.is_none() {
1621+
if blk_id.is_none() {
16111622
pointing_at_return_type |= fcx.suggest_missing_return_type(
16121623
&mut err,
16131624
&fn_decl,
@@ -1625,8 +1636,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16251636
let parent_id = fcx.tcx.hir().get_parent_item(id);
16261637
let parent_item = fcx.tcx.hir().get_by_def_id(parent_id);
16271638

1628-
if let (Some((expr, _)), Some((fn_decl, _, _))) =
1629-
(expression, fcx.get_node_fn_decl(parent_item))
1639+
if let (Some(expr), Some(_), Some((fn_decl, _, _))) =
1640+
(expression, blk_id, fcx.get_node_fn_decl(parent_item))
16301641
{
16311642
fcx.suggest_missing_break_or_return_expr(
16321643
&mut err,

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5050
// adding a semicolon, because there's nowhere to put it.
5151
// See issue #81943.
5252
if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, expr.span) {
53-
self.suggest_missing_semicolon(err, expr, expected);
53+
self.suggest_missing_semicolon(err, expr, expected, false);
5454
}
5555
let mut pointing_at_return_type = false;
5656
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
@@ -473,11 +473,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
473473
/// This routine checks if the return expression in a block would make sense on its own as a
474474
/// statement and the return type has been left as default or has been specified as `()`. If so,
475475
/// it suggests adding a semicolon.
476-
fn suggest_missing_semicolon(
476+
///
477+
/// If the expression is the expression of a closure without block (`|| expr`), a
478+
/// block is needed to be added too (`|| { expr; }`). This is denoted by `needs_block`.
479+
pub fn suggest_missing_semicolon(
477480
&self,
478481
err: &mut Diagnostic,
479482
expression: &'tcx hir::Expr<'tcx>,
480483
expected: Ty<'tcx>,
484+
needs_block: bool,
481485
) {
482486
if expected.is_unit() {
483487
// `BlockTailExpression` only relevant if the tail expr would be
@@ -491,12 +495,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
491495
| ExprKind::Block(..)
492496
if expression.can_have_side_effects() =>
493497
{
494-
err.span_suggestion(
495-
expression.span.shrink_to_hi(),
496-
"consider using a semicolon here",
497-
";".to_string(),
498-
Applicability::MachineApplicable,
499-
);
498+
if needs_block {
499+
err.multipart_suggestion(
500+
"consider using a semicolon here",
501+
vec![
502+
(expression.span.shrink_to_lo(), "{ ".to_owned()),
503+
(expression.span.shrink_to_hi(), "; }".to_owned()),
504+
],
505+
Applicability::MachineApplicable,
506+
);
507+
} else {
508+
err.span_suggestion(
509+
expression.span.shrink_to_hi(),
510+
"consider using a semicolon here",
511+
";".to_string(),
512+
Applicability::MachineApplicable,
513+
);
514+
}
500515
}
501516
_ => (),
502517
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn foo(_f: impl Fn()) {}
2+
3+
fn bar() -> i32 {
4+
1
5+
}
6+
7+
fn main() {
8+
foo(|| bar())
9+
//~^ ERROR mismatched types [E0308]
10+
//~| HELP consider using a semicolon here
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/add_semicolon_non_block_closure.rs:8:12
3+
|
4+
LL | fn main() {
5+
| - expected `()` because of default return type
6+
LL | foo(|| bar())
7+
| ^^^^^ expected `()`, found `i32`
8+
|
9+
help: consider using a semicolon here
10+
|
11+
LL | foo(|| { bar(); })
12+
| + +++
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)