Skip to content

Commit 6bd534d

Browse files
committed
return the correct type for closures in type_of
1 parent 18fa434 commit 6bd534d

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

compiler/rustc_typeck/src/collect.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
294294
if let hir::ExprKind::Closure(..) = expr.kind {
295295
let def_id = self.tcx.hir().local_def_id(expr.hir_id);
296296
self.tcx.ensure().generics_of(def_id);
297-
self.tcx.ensure().type_of(def_id);
297+
// We do not call `type_of` for closures here as that
298+
// depends on typecheck and would therefore hide
299+
// any further errors in case one typeck fails.
298300
}
299301
intravisit::walk_expr(self, expr);
300302
}

compiler/rustc_typeck/src/collect/type_of.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
463463

464464
Node::Field(field) => icx.to_ty(field.ty),
465465

466-
Node::Expr(&Expr { kind: ExprKind::Closure(.., gen), .. }) => {
467-
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
468-
if let Some(movability) = gen {
469-
tcx.mk_generator(def_id.to_def_id(), substs, movability)
470-
} else {
471-
tcx.mk_closure(def_id.to_def_id(), substs)
472-
}
473-
}
466+
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => tcx.typeck(def_id).node_type(hir_id),
474467

475468
Node::AnonConst(_) if let Some(param) = tcx.opt_const_param_of(def_id) => {
476469
// We defer to `type_of` of the corresponding parameter

compiler/rustc_typeck/src/expr_use_visitor.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
715715

716716
debug!("walk_captures({:?})", closure_expr);
717717

718-
let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id).to_def_id();
719-
let upvars = self.tcx().upvars_mentioned(self.body_owner);
718+
let tcx = self.tcx();
719+
let closure_def_id = tcx.hir().local_def_id(closure_expr.hir_id).to_def_id();
720+
let upvars = tcx.upvars_mentioned(self.body_owner);
720721

721722
// For purposes of this function, generator and closures are equivalent.
722723
let body_owner_is_closure = matches!(
723-
self.tcx().type_of(self.body_owner.to_def_id()).kind(),
724-
ty::Closure(..) | ty::Generator(..)
724+
tcx.hir().body_owner_kind(tcx.hir().local_def_id_to_hir_id(self.body_owner)),
725+
hir::BodyOwnerKind::Closure,
725726
);
726727

727728
// If we have a nested closure, we want to include the fake reads present in the nested closure.

0 commit comments

Comments
 (0)