Skip to content

Commit 6413e9b

Browse files
committed
Remove visitor use
1 parent 6add192 commit 6413e9b

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
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

+22-24
Original file line numberDiff line numberDiff line change
@@ -1040,22 +1040,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10401040
return;
10411041
}
10421042

1043-
let in_closure = matches!(
1044-
self.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-
Some((_, Node::Expr(Expr { kind: ExprKind::Closure(..), .. })))
1058-
);
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(..), .. }))));
10591059

10601060
let can_return = match fn_decl.output {
10611061
hir::FnRetTy::Return(ty) => {
@@ -1083,20 +1083,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10831083
rets.push(ret_ty);
10841084
}
10851085
let mut visitor = CollectRetsVisitor { ret_exprs: vec![] };
1086-
if let Some(item) = self.tcx.hir().find(id)
1087-
&& let Node::Expr(expr) = item
1086+
if let Some((_, Node::Expr(expr))) = scope
1087+
&& let ExprKind::Closure(closure) = expr.kind
1088+
&& let Some(node) = self.tcx.hir().find(closure.body.hir_id)
1089+
&& let Node::Expr(expr) = node
10881090
{
10891091
visitor.visit_expr(expr);
10901092
for expr in visitor.ret_exprs {
1091-
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
1092-
rets.push(ty);
1093-
}
1093+
rets.push(self.check_expr(expr));
10941094
}
10951095
if let hir::ExprKind::Block(hir::Block { expr: Some(expr), .. }, _) = expr.kind
10961096
{
1097-
if let Some(ty) = self.typeck_results.borrow().node_type_opt(expr.hir_id) {
1098-
rets.push(ty);
1099-
}
1097+
rets.push(self.check_expr(expr));
11001098
}
11011099
}
11021100
rets.into_iter().all(|ty| self.can_coerce(found, ty))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition:2021
2+
use std::future::Future;
3+
use std::pin::Pin;
4+
pub struct S;
5+
pub fn baz() -> Pin<Box<dyn Future<Output = Result<S, ()>> + 'static>> {
6+
Box::pin(async move {
7+
if true {
8+
Some(S) //~ ERROR mismatched types
9+
// Do *not* suggest returning this value, because it doesn't unify with the others.
10+
}
11+
if false {
12+
return Err(());
13+
}
14+
Err(()) //~ WARN unreachable
15+
// The warning above is *incorrect* and a consequence of using `check_expr` incorrectly
16+
// during error recovery.
17+
})
18+
}
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/missing-return-in-async-block-mismatched-types.rs:8:13
3+
|
4+
LL | / if true {
5+
LL | | Some(S)
6+
| | ^^^^^^^ expected `()`, found `Option<S>`
7+
LL | | // Do *not* suggest returning this value, because it doesn't unify with the others.
8+
LL | | }
9+
| |_________- expected this to be `()`
10+
|
11+
= note: expected unit type `()`
12+
found enum `Option<S>`
13+
note: return type inferred to be `()` here
14+
--> $DIR/missing-return-in-async-block-mismatched-types.rs:12:20
15+
|
16+
LL | return Err(());
17+
| ^^^^^^^
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)