Remove visitor use

This commit is contained in:
Esteban Küber 2024-02-06 05:04:58 +00:00
parent 37d2ea2fa0
commit d07195fe6b
2 changed files with 23 additions and 44 deletions

View File

@ -92,8 +92,8 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>; type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;
pub struct CollectRetsVisitor<'tcx> { struct CollectRetsVisitor<'tcx> {
pub ret_exprs: Vec<&'tcx hir::Expr<'tcx>>, ret_exprs: Vec<&'tcx hir::Expr<'tcx>>,
} }
impl<'tcx> Visitor<'tcx> for CollectRetsVisitor<'tcx> { impl<'tcx> Visitor<'tcx> for CollectRetsVisitor<'tcx> {

View File

@ -1,6 +1,5 @@
use super::FnCtxt; use super::FnCtxt;
use crate::coercion::CollectRetsVisitor;
use crate::errors; use crate::errors;
use crate::fluent_generated as fluent; use crate::fluent_generated as fluent;
use crate::fn_ctxt::rustc_span::BytePos; 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 as hir;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::def::{CtorKind, CtorOf, DefKind}; use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::intravisit::{Map, Visitor};
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::{ use rustc_hir::{
CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node,
@ -1041,22 +1039,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return; return;
} }
let in_closure = matches!( let scope = self
self.tcx .tcx
.hir() .hir()
.parent_iter(id) .parent_iter(id)
.filter(|(_, node)| { .filter(|(_, node)| {
matches!( matches!(
node, node,
Node::Expr(Expr { kind: ExprKind::Closure(..), .. }) Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
| Node::Item(_) | Node::Item(_)
| Node::TraitItem(_) | Node::TraitItem(_)
| Node::ImplItem(_) | Node::ImplItem(_)
) )
}) })
.next(), .next();
Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. }))) let in_closure =
); matches!(scope, Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. }))));
let can_return = match fn_decl.output { let can_return = match fn_decl.output {
hir::FnRetTy::Return(ty) => { hir::FnRetTy::Return(ty) => {
@ -1078,35 +1076,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.can_coerce(found, ty) self.can_coerce(found, ty)
} }
hir::FnRetTy::DefaultReturn(_) if in_closure => { hir::FnRetTy::DefaultReturn(_) if in_closure => {
let mut rets = vec![]; self.ret_coercion.as_ref().map_or(false, |ret| {
if let Some(ret_coercion) = self.ret_coercion.as_ref() { let ret_ty = ret.borrow().expected_ty();
let ret_ty = ret_coercion.borrow().expected_ty(); self.can_coerce(found, ret_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))
} }
_ => false, _ => false,
}; };
if can_return if can_return
&& let Some(owner_node) = self.tcx.hir_node(fn_id).as_owner() && let Some(owner_node) = self.tcx.hir_node(fn_id).as_owner()
&& let Some(span) = expr.span.find_ancestor_inside(owner_node.span()) && let Some(span) = expr.span.find_ancestor_inside(*owner_node.span())
{ {
err.multipart_suggestion( err.multipart_suggestion(
"you might have meant to return this value", "you might have meant to return this value",