diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 905e45bf9f0..05f1184fad3 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -1484,9 +1484,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { if let Some(tables) = self.in_progress_tables { if let Some(id) = self.tcx.hir.as_local_node_id(def_id) { let hir_id = self.tcx.hir.node_to_hir_id(id); - if let Some(&ty) = tables.borrow().closure_tys().get(hir_id) { - return ty; - } + let closure_ty = tables.borrow().node_id_to_type(hir_id); + let (closure_def_id, closure_substs) = match closure_ty.sty { + ty::TyClosure(closure_def_id, closure_substs) => + (closure_def_id, closure_substs), + _ => + bug!("closure with non-closure type: {:?}", closure_ty), + }; + assert_eq!(def_id, closure_def_id); + let closure_sig_ty = closure_substs.closure_sig_ty(def_id, self.tcx); + let closure_sig_ty = self.shallow_resolve(&closure_sig_ty); + return closure_sig_ty.fn_sig(self.tcx); } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 6bd1a3564b1..ccd851799fe 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -356,9 +356,6 @@ pub struct TypeckTables<'tcx> { /// Borrows pub upvar_capture_map: ty::UpvarCaptureMap<'tcx>, - /// Records the type of each closure. - closure_tys: ItemLocalMap>, - /// Records the reasons that we picked the kind of each closure; /// not all closures are present in the map. closure_kind_origins: ItemLocalMap<(Span, ast::Name)>, @@ -413,7 +410,6 @@ impl<'tcx> TypeckTables<'tcx> { upvar_capture_map: FxHashMap(), generator_sigs: ItemLocalMap(), generator_interiors: ItemLocalMap(), - closure_tys: ItemLocalMap(), closure_kind_origins: ItemLocalMap(), liberated_fn_sigs: ItemLocalMap(), fru_field_types: ItemLocalMap(), @@ -609,21 +605,6 @@ impl<'tcx> TypeckTables<'tcx> { self.upvar_capture_map[&upvar_id] } - pub fn closure_tys(&self) -> LocalTableInContext> { - LocalTableInContext { - local_id_root: self.local_id_root, - data: &self.closure_tys - } - } - - pub fn closure_tys_mut(&mut self) - -> LocalTableInContextMut> { - LocalTableInContextMut { - local_id_root: self.local_id_root, - data: &mut self.closure_tys - } - } - pub fn closure_kind_origins(&self) -> LocalTableInContext<(Span, ast::Name)> { LocalTableInContext { local_id_root: self.local_id_root, @@ -730,7 +711,6 @@ impl<'gcx> HashStable> for TypeckTables<'gcx> { ref pat_binding_modes, ref pat_adjustments, ref upvar_capture_map, - ref closure_tys, ref closure_kind_origins, ref liberated_fn_sigs, ref fru_field_types, @@ -773,7 +753,6 @@ impl<'gcx> HashStable> for TypeckTables<'gcx> { hcx.def_path_hash(closure_def_id)) }); - closure_tys.hash_stable(hcx, hasher); closure_kind_origins.hash_stable(hcx, hasher); liberated_fn_sigs.hash_stable(hcx, hasher); fru_field_types.hash_stable(hcx, hasher); diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 77aa3553967..e581eac9ccd 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -297,24 +297,19 @@ impl<'tcx> ClosureSubsts<'tcx> { } /// Returns the closure kind for this closure; may return a type - /// variable during inference. + /// variable during inference. To get the closure kind during + /// inference, use `infcx.closure_kind(def_id, substs)`. pub fn closure_kind_ty(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>) -> Ty<'tcx> { self.split(def_id, tcx).closure_kind_ty } /// Returns the type representing the closure signature for this - /// closure; may contain type variables during inference. + /// closure; may contain type variables during inference. To get + /// the closure signature during inference, use + /// `infcx.fn_sig(def_id)`. pub fn closure_sig_ty(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>) -> Ty<'tcx> { self.split(def_id, tcx).closure_sig_ty } - - /// Extracts the signature from the closure. - pub fn closure_sig(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>) -> ty::PolyFnSig<'tcx> { - match &self.split(def_id, tcx).closure_sig_ty.sty { - ty::TyFnPtr(sig) => *sig, - t => bug!("closure_sig_ty is not a fn-ptr: {:?}", t), - } - } } impl<'tcx> ClosureSubsts<'tcx> { @@ -324,6 +319,16 @@ impl<'tcx> ClosureSubsts<'tcx> { pub fn closure_kind(self, def_id: DefId, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> ty::ClosureKind { self.split(def_id, tcx).closure_kind_ty.to_opt_closure_kind().unwrap() } + + /// Extracts the signature from the closure; only usable outside + /// of an inference context, because in that context we know that + /// there are no type variables. + pub fn closure_sig(self, def_id: DefId, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> ty::PolyFnSig<'tcx> { + match self.closure_sig_ty(def_id, tcx).sty { + ty::TyFnPtr(sig) => sig, + ref t => bug!("closure_sig_ty is not a fn-ptr: {:?}", t), + } + } } impl<'a, 'gcx, 'tcx> ClosureSubsts<'tcx> { diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 6c20468c286..5fd1a0afb3e 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -146,7 +146,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { sig_fn_ptr_ty, substs.closure_sig_ty(expr_def_id, self.tcx)); - self.tables.borrow_mut().closure_tys_mut().insert(expr.hir_id, sig); if let Some(kind) = opt_kind { self.demand_eqtype(expr.span, kind.to_ty(self.tcx), diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 48af2f0eff7..cf5864e910d 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -243,15 +243,6 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> { debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root); let common_local_id_root = fcx_tables.local_id_root.unwrap(); - for (&id, closure_ty) in fcx_tables.closure_tys().iter() { - let hir_id = hir::HirId { - owner: common_local_id_root.index, - local_id: id, - }; - let closure_ty = self.resolve(closure_ty, &hir_id); - self.tables.closure_tys_mut().insert(hir_id, closure_ty); - } - for (&id, &origin) in fcx_tables.closure_kind_origins().iter() { let hir_id = hir::HirId { owner: common_local_id_root.index, diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 7a03d97c18a..90a3ab75751 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1265,7 +1265,14 @@ fn fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } NodeExpr(&hir::Expr { node: hir::ExprClosure(..), hir_id, .. }) => { - tcx.typeck_tables_of(def_id).closure_tys()[hir_id] + let tables = tcx.typeck_tables_of(def_id); + match tables.node_id_to_type(hir_id).sty { + ty::TyClosure(closure_def_id, closure_substs) => { + assert_eq!(def_id, closure_def_id); + return closure_substs.closure_sig(closure_def_id, tcx); + } + ref t => bug!("closure with non-closure type: {:?}", t), + } } x => {