Skip to content

Commit cdc5c13

Browse files
authored
Rollup merge of rust-lang#91055 - lcnr:type_of-closures, r=nikomatsakis
return the correct type for closures in `type_of` A bit unhappy about the way `typeck::check_crate` works rn. Would have preferred to not change `CollectItemTypesVisitor` in this way. r? ``@nikomatsakis``
2 parents cfa4ac6 + 6bd534d commit cdc5c13

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
@@ -295,7 +295,9 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
295295
if let hir::ExprKind::Closure(..) = expr.kind {
296296
let def_id = self.tcx.hir().local_def_id(expr.hir_id);
297297
self.tcx.ensure().generics_of(def_id);
298-
self.tcx.ensure().type_of(def_id);
298+
// We do not call `type_of` for closures here as that
299+
// depends on typecheck and would therefore hide
300+
// any further errors in case one typeck fails.
299301
}
300302
intravisit::walk_expr(self, expr);
301303
}

compiler/rustc_typeck/src/collect/type_of.rs

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

471471
Node::Field(field) => icx.to_ty(field.ty),
472472

473-
Node::Expr(&Expr { kind: ExprKind::Closure(.., gen), .. }) => {
474-
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
475-
if let Some(movability) = gen {
476-
tcx.mk_generator(def_id.to_def_id(), substs, movability)
477-
} else {
478-
tcx.mk_closure(def_id.to_def_id(), substs)
479-
}
480-
}
473+
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => tcx.typeck(def_id).node_type(hir_id),
481474

482475
Node::AnonConst(_) if let Some(param) = tcx.opt_const_param_of(def_id) => {
483476
// 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)