Skip to content

Commit

Permalink
Remove visitor use
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 6, 2024
1 parent 6add192 commit bd40a05
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::FnCtxt;

use crate::coercion::CollectRetsVisitor;
use crate::errors;
use crate::fluent_generated as fluent;
use crate::fn_ctxt::rustc_span::BytePos;
Expand All @@ -17,7 +16,6 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::intravisit::{Map, Visitor};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{
CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node,
Expand Down Expand Up @@ -1040,22 +1038,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}

let in_closure = matches!(
self.tcx
.hir()
.parent_iter(id)
.filter(|(_, node)| {
matches!(
node,
Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
| Node::Item(_)
| Node::TraitItem(_)
| Node::ImplItem(_)
)
})
.next(),
Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. })))
);
let scope = self
.tcx
.hir()
.parent_iter(id)
.filter(|(_, node)| {
matches!(
node,
Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
| Node::Item(_)
| Node::TraitItem(_)
| Node::ImplItem(_)
)
})
.next();
let in_closure =
matches!(scope, Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. }))));

let can_return = match fn_decl.output {
hir::FnRetTy::Return(ty) => {
Expand All @@ -1077,29 +1075,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.can_coerce(found, ty)
}
hir::FnRetTy::DefaultReturn(_) if in_closure => {
let mut rets = vec![];
if let Some(ret_coercion) = self.ret_coercion.as_ref() {
let ret_ty = ret_coercion.borrow().expected_ty();
rets.push(ret_ty);
}
let mut visitor = CollectRetsVisitor { ret_exprs: vec![] };
if let Some(item) = self.tcx.hir().find(id)
&& let Node::Expr(expr) = item
{
visitor.visit_expr(expr);
for expr in visitor.ret_exprs {
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
rets.push(ty);
}
}
if let hir::ExprKind::Block(hir::Block { expr: Some(expr), .. }, _) = expr.kind
{
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
rets.push(ty);
}
}
}
rets.into_iter().all(|ty| self.can_coerce(found, ty))
self.ret_coercion.as_ref().map_or(false, |ret| {
let ret_ty = ret.borrow().expected_ty();
self.can_coerce(found, ret_ty)
})
}
_ => false,
};
Expand Down

0 comments on commit bd40a05

Please sign in to comment.