From d1f8f7f4e9d274b55234830f12686c2c663dd831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Feb 2024 05:04:58 +0000 Subject: [PATCH] Remove visitor use --- compiler/rustc_hir_typeck/src/coercion.rs | 4 +- .../src/fn_ctxt/suggestions.rs | 61 ++++++------------- 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 3772ad1a4677..e57eb6fff027 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -92,8 +92,8 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> { type CoerceResult<'tcx> = InferResult<'tcx, (Vec>, Ty<'tcx>)>; -pub struct CollectRetsVisitor<'tcx> { - pub ret_exprs: Vec<&'tcx hir::Expr<'tcx>>, +struct CollectRetsVisitor<'tcx> { + ret_exprs: Vec<&'tcx hir::Expr<'tcx>>, } impl<'tcx> Visitor<'tcx> for CollectRetsVisitor<'tcx> { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index a71e3bc3a03e..2a0966ea5c6f 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -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; @@ -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, @@ -1042,22 +1040,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) => { @@ -1079,29 +1077,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, };