Skip to content

Commit 9ca9e1d

Browse files
committed
Remove visitor use
1 parent e3272b3 commit 9ca9e1d

File tree

2 files changed

+23
-44
lines changed

2 files changed

+23
-44
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
9292

9393
type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;
9494

95-
pub struct CollectRetsVisitor<'tcx> {
96-
pub ret_exprs: Vec<&'tcx hir::Expr<'tcx>>,
95+
struct CollectRetsVisitor<'tcx> {
96+
ret_exprs: Vec<&'tcx hir::Expr<'tcx>>,
9797
}
9898

9999
impl<'tcx> Visitor<'tcx> for CollectRetsVisitor<'tcx> {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+21-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::FnCtxt;
22

3-
use crate::coercion::CollectRetsVisitor;
43
use crate::errors;
54
use crate::fluent_generated as fluent;
65
use crate::fn_ctxt::rustc_span::BytePos;
@@ -17,7 +16,6 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
1716
use rustc_hir as hir;
1817
use rustc_hir::def::Res;
1918
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
20-
use rustc_hir::intravisit::{Map, Visitor};
2119
use rustc_hir::lang_items::LangItem;
2220
use rustc_hir::{
2321
CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node,
@@ -1042,22 +1040,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10421040
return;
10431041
}
10441042

1045-
let in_closure = matches!(
1046-
self.tcx
1047-
.hir()
1048-
.parent_iter(id)
1049-
.filter(|(_, node)| {
1050-
matches!(
1051-
node,
1052-
Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
1053-
| Node::Item(_)
1054-
| Node::TraitItem(_)
1055-
| Node::ImplItem(_)
1056-
)
1057-
})
1058-
.next(),
1059-
Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. })))
1060-
);
1043+
let scope = self
1044+
.tcx
1045+
.hir()
1046+
.parent_iter(id)
1047+
.filter(|(_, node)| {
1048+
matches!(
1049+
node,
1050+
Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
1051+
| Node::Item(_)
1052+
| Node::TraitItem(_)
1053+
| Node::ImplItem(_)
1054+
)
1055+
})
1056+
.next();
1057+
let in_closure =
1058+
matches!(scope, Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. }))));
10611059

10621060
let can_return = match fn_decl.output {
10631061
hir::FnRetTy::Return(ty) => {
@@ -1079,35 +1077,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10791077
self.can_coerce(found, ty)
10801078
}
10811079
hir::FnRetTy::DefaultReturn(_) if in_closure => {
1082-
let mut rets = vec![];
1083-
if let Some(ret_coercion) = self.ret_coercion.as_ref() {
1084-
let ret_ty = ret_coercion.borrow().expected_ty();
1085-
rets.push(ret_ty);
1086-
}
1087-
let mut visitor = CollectRetsVisitor { ret_exprs: vec![] };
1088-
if let Some(item) = self.tcx.hir().find(id)
1089-
&& let Node::Expr(expr) = item
1090-
{
1091-
visitor.visit_expr(expr);
1092-
for expr in visitor.ret_exprs {
1093-
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
1094-
rets.push(ty);
1095-
}
1096-
}
1097-
if let hir::ExprKind::Block(hir::Block { expr: Some(expr), .. }, _) = expr.kind
1098-
{
1099-
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
1100-
rets.push(ty);
1101-
}
1102-
}
1103-
}
1104-
rets.into_iter().all(|ty| self.can_coerce(found, ty))
1080+
self.ret_coercion.as_ref().map_or(false, |ret| {
1081+
let ret_ty = ret.borrow().expected_ty();
1082+
self.can_coerce(found, ret_ty)
1083+
})
11051084
}
11061085
_ => false,
11071086
};
11081087
if can_return
11091088
&& let Some(owner_node) = self.tcx.hir_node(fn_id).as_owner()
1110-
&& let Some(span) = expr.span.find_ancestor_inside(owner_node.span())
1089+
&& let Some(span) = expr.span.find_ancestor_inside(*owner_node.span())
11111090
{
11121091
err.multipart_suggestion(
11131092
"you might have meant to return this value",

0 commit comments

Comments
 (0)