Skip to content

Commit 3529341

Browse files
authored
Rollup merge of #135562 - chenyukang:yukang-fix-128561, r=compiler-errors
Add ignore value suggestion in closure body Fixes #128561 r? `@estebank`
2 parents 1d67944 + 774ab46 commit 3529341

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,25 +1854,24 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18541854
fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err);
18551855

18561856
let due_to_block = matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));
1857-
1858-
let parent_id = fcx.tcx.parent_hir_id(block_or_return_id);
1859-
let parent = fcx.tcx.hir_node(parent_id);
1857+
let parent = fcx.tcx.parent_hir_node(block_or_return_id);
18601858
if let Some(expr) = expression
18611859
&& let hir::Node::Expr(&hir::Expr {
18621860
kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
18631861
..
18641862
}) = parent
1865-
&& !matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..))
18661863
{
1867-
fcx.suggest_missing_semicolon(&mut err, expr, expected, true);
1864+
let needs_block =
1865+
!matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
1866+
fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
18681867
}
18691868
// Verify that this is a tail expression of a function, otherwise the
18701869
// label pointing out the cause for the type coercion will be wrong
18711870
// as prior return coercions would not be relevant (#57664).
18721871
if let Some(expr) = expression
18731872
&& due_to_block
18741873
{
1875-
fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
1874+
fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
18761875
let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
18771876
&mut err,
18781877
expr,

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
911911
self,
912912
&cause,
913913
|mut err| {
914-
self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
914+
self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
915915
self.suggest_mismatched_types_on_tail(
916916
&mut err, expr, ty, e_ty, target_id,
917917
);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
764764
expression: &'tcx hir::Expr<'tcx>,
765765
expected: Ty<'tcx>,
766766
needs_block: bool,
767+
parent_is_closure: bool,
767768
) {
768769
if expected.is_unit() {
769770
// `BlockTailExpression` only relevant if the tail expr would be
@@ -799,6 +800,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799800
);
800801
}
801802
}
803+
ExprKind::Path(..) | ExprKind::Lit(_)
804+
if parent_is_closure
805+
&& !expression.span.in_external_macro(self.tcx.sess.source_map()) =>
806+
{
807+
err.span_suggestion_verbose(
808+
expression.span.shrink_to_lo(),
809+
"consider ignoring the value",
810+
"_ = ",
811+
Applicability::MachineApplicable,
812+
);
813+
}
802814
_ => (),
803815
}
804816
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
b"abc".iter().for_each(|x| x); //~ ERROR: mismatched types
3+
4+
b"abc".iter().for_each(|x| dbg!(x)); //~ ERROR: mismatched types
5+
6+
b"abc".iter().for_each(|x| {
7+
println!("{}", x);
8+
x //~ ERROR: mismatched types
9+
})
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/closure-ty-mismatch-issue-128561.rs:2:32
3+
|
4+
LL | b"abc".iter().for_each(|x| x);
5+
| ^ expected `()`, found `&u8`
6+
|
7+
help: consider ignoring the value
8+
|
9+
LL | b"abc".iter().for_each(|x| _ = x);
10+
| +++
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/closure-ty-mismatch-issue-128561.rs:4:32
14+
|
15+
LL | b"abc".iter().for_each(|x| dbg!(x));
16+
| ^^^^^^^ expected `()`, found `&u8`
17+
|
18+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/closure-ty-mismatch-issue-128561.rs:8:9
22+
|
23+
LL | x
24+
| ^ expected `()`, found `&u8`
25+
26+
error: aborting due to 3 previous errors
27+
28+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)