Skip to content

Commit f7a34f9

Browse files
Rollup merge of #113567 - chenyukang:yukang-fix-113354-while-let, r=cjgillot
While let suggestion will work for closure body Fixes #113354
2 parents 3518041 + 9aed969 commit f7a34f9

File tree

4 files changed

+61
-38
lines changed

4 files changed

+61
-38
lines changed

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

+39-38
Original file line numberDiff line numberDiff line change
@@ -464,52 +464,53 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
464464
span: Span,
465465
) -> Option<TypeErrorAdditionalDiags> {
466466
let hir = self.tcx.hir();
467-
if let Some(node) = self.tcx.hir().find_by_def_id(cause.body_id) &&
468-
let hir::Node::Item(hir::Item {
469-
kind: hir::ItemKind::Fn(_sig, _, body_id), ..
470-
}) = node {
471-
let body = hir.body(*body_id);
472-
473-
/// Find the if expression with given span
474-
struct IfVisitor {
475-
pub result: bool,
476-
pub found_if: bool,
477-
pub err_span: Span,
478-
}
467+
if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(cause.body_id) {
468+
let body = hir.body(body_id);
469+
470+
/// Find the if expression with given span
471+
struct IfVisitor {
472+
pub result: bool,
473+
pub found_if: bool,
474+
pub err_span: Span,
475+
}
479476

480-
impl<'v> Visitor<'v> for IfVisitor {
481-
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
482-
if self.result { return; }
483-
match ex.kind {
484-
hir::ExprKind::If(cond, _, _) => {
485-
self.found_if = true;
486-
walk_expr(self, cond);
487-
self.found_if = false;
477+
impl<'v> Visitor<'v> for IfVisitor {
478+
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
479+
if self.result {
480+
return;
481+
}
482+
match ex.kind {
483+
hir::ExprKind::If(cond, _, _) => {
484+
self.found_if = true;
485+
walk_expr(self, cond);
486+
self.found_if = false;
487+
}
488+
_ => walk_expr(self, ex),
488489
}
489-
_ => walk_expr(self, ex),
490490
}
491-
}
492491

493-
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
494-
if let hir::StmtKind::Local(hir::Local {
495-
span, pat: hir::Pat{..}, ty: None, init: Some(_), ..
496-
}) = &ex.kind
497-
&& self.found_if
498-
&& span.eq(&self.err_span) {
499-
self.result = true;
492+
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
493+
if let hir::StmtKind::Local(hir::Local {
494+
span, pat: hir::Pat{..}, ty: None, init: Some(_), ..
495+
}) = &ex.kind
496+
&& self.found_if
497+
&& span.eq(&self.err_span) {
498+
self.result = true;
499+
}
500+
walk_stmt(self, ex);
500501
}
501-
walk_stmt(self, ex);
502-
}
503502

504-
fn visit_body(&mut self, body: &'v hir::Body<'v>) {
505-
hir::intravisit::walk_body(self, body);
503+
fn visit_body(&mut self, body: &'v hir::Body<'v>) {
504+
hir::intravisit::walk_body(self, body);
505+
}
506506
}
507-
}
508507

509-
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
510-
visitor.visit_body(&body);
511-
if visitor.result {
512-
return Some(TypeErrorAdditionalDiags::AddLetForLetChains{span: span.shrink_to_lo()});
508+
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
509+
visitor.visit_body(&body);
510+
if visitor.result {
511+
return Some(TypeErrorAdditionalDiags::AddLetForLetChains {
512+
span: span.shrink_to_lo(),
513+
});
513514
}
514515
}
515516
None

tests/ui/inference/issue-113354.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//run-rustfix
2+
fn main() {
3+
let _ = || { while let Some(_) = Some(1) { } }; //~ ERROR mismatched types
4+
}

tests/ui/inference/issue-113354.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//run-rustfix
2+
fn main() {
3+
let _ = || { while Some(_) = Some(1) { } }; //~ ERROR mismatched types
4+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-113354.rs:3:24
3+
|
4+
LL | let _ = || { while Some(_) = Some(1) { } };
5+
| ^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
6+
|
7+
help: consider adding `let`
8+
|
9+
LL | let _ = || { while let Some(_) = Some(1) { } };
10+
| +++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)