Skip to content

Commit 2dff9a4

Browse files
committed
stop using the closure_kinds query / table for anything
Closure Kind is now extracted from the closure substs exclusively.
1 parent 716f75b commit 2dff9a4

File tree

14 files changed

+107
-109
lines changed

14 files changed

+107
-109
lines changed

src/librustc/infer/mod.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,26 +1463,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14631463
!traits::type_known_to_meet_bound(self, param_env, ty, copy_def_id, span)
14641464
}
14651465

1466+
/// Obtains the latest type of the given closure; this may be a
1467+
/// closure in the current function, in which case its
1468+
/// `ClosureKind` may not yet be known.
14661469
pub fn closure_kind(&self,
1467-
def_id: DefId)
1470+
closure_def_id: DefId,
1471+
closure_substs: ty::ClosureSubsts<'tcx>)
14681472
-> Option<ty::ClosureKind>
14691473
{
1470-
if let Some(tables) = self.in_progress_tables {
1471-
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
1472-
let hir_id = self.tcx.hir.node_to_hir_id(id);
1473-
return tables.borrow()
1474-
.closure_kinds()
1475-
.get(hir_id)
1476-
.cloned()
1477-
.map(|(kind, _)| kind);
1478-
}
1479-
}
1480-
1481-
// During typeck, ALL closures are local. But afterwards,
1482-
// during trans, we see closure ids from other traits.
1483-
// That may require loading the closure data out of the
1484-
// cstore.
1485-
Some(self.tcx.closure_kind(def_id))
1474+
let closure_kind_ty = closure_substs.closure_kind_ty(closure_def_id, self.tcx);
1475+
let closure_kind_ty = self.shallow_resolve(&closure_kind_ty);
1476+
closure_kind_ty.to_opt_closure_kind()
14861477
}
14871478

14881479
/// Obtain the signature of a function or closure.

src/librustc/middle/mem_categorization.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,19 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
750750

751751
let kind = match self.node_ty(fn_hir_id)?.sty {
752752
ty::TyGenerator(..) => ty::ClosureKind::FnOnce,
753-
ty::TyClosure(..) => {
754-
match self.tables.closure_kinds().get(fn_hir_id) {
755-
Some(&(kind, _)) => kind,
756-
None => span_bug!(span, "missing closure kind"),
753+
ty::TyClosure(closure_def_id, closure_substs) => {
754+
match self.infcx {
755+
// During upvar inference we may not know the
756+
// closure kind, just use `Fn`.
757+
Some(infcx) =>
758+
infcx.closure_kind(closure_def_id, closure_substs)
759+
.unwrap_or(ty::ClosureKind::Fn),
760+
761+
None =>
762+
self.tcx.global_tcx()
763+
.lift(&closure_substs)
764+
.expect("no inference cx, but inference variables in closure ty")
765+
.closure_kind(closure_def_id, self.tcx.global_tcx())
757766
}
758767
}
759768
ref t => span_bug!(span, "unexpected type for fn in mem_categorization: {:?}", t),

src/librustc/traits/error_reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
643643
violations)
644644
}
645645

646-
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
647-
let found_kind = self.closure_kind(closure_def_id).unwrap();
646+
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
647+
let found_kind = self.closure_kind(closure_def_id, closure_substs).unwrap();
648648
let closure_span = self.tcx.hir.span_if_local(closure_def_id).unwrap();
649649
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
650650
let mut err = struct_span_err!(

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ fn process_predicate<'a, 'gcx, 'tcx>(
439439
}
440440

441441
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
442-
match closure_substs.opt_closure_kind(closure_def_id, selcx.tcx()) {
442+
match selcx.infcx().closure_kind(closure_def_id, closure_substs) {
443443
Some(closure_kind) => {
444444
if closure_kind.extends(kind) {
445445
Ok(Some(vec![]))

src/librustc/traits/select.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
719719
}
720720

721721
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
722-
match closure_substs.opt_closure_kind(closure_def_id, self.tcx()) {
722+
match self.infcx.closure_kind(closure_def_id, closure_substs) {
723723
Some(closure_kind) => {
724724
if closure_kind.extends(kind) {
725725
EvaluatedToOk
@@ -1593,10 +1593,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15931593
// touch bound regions, they just capture the in-scope
15941594
// type/region parameters
15951595
match obligation.self_ty().skip_binder().sty {
1596-
ty::TyClosure(closure_def_id, _) => {
1596+
ty::TyClosure(closure_def_id, closure_substs) => {
15971597
debug!("assemble_unboxed_candidates: kind={:?} obligation={:?}",
15981598
kind, obligation);
1599-
match self.infcx.closure_kind(closure_def_id) {
1599+
match self.infcx.closure_kind(closure_def_id, closure_substs) {
16001600
Some(closure_kind) => {
16011601
debug!("assemble_unboxed_candidates: closure_kind = {:?}", closure_kind);
16021602
if closure_kind.extends(kind) {

src/librustc/ty/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn resolve_closure<'a, 'tcx>(
189189
requested_kind: ty::ClosureKind)
190190
-> Instance<'tcx>
191191
{
192-
let actual_kind = tcx.closure_kind(def_id);
192+
let actual_kind = substs.closure_kind(def_id, tcx);
193193

194194
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
195195
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),

src/librustc/ty/sty.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,26 +290,19 @@ impl<'tcx> ClosureSubsts<'tcx> {
290290
upvar_kinds.iter().map(|t| t.as_type().expect("upvar should be type"))
291291
}
292292

293-
/// Returns the closure kind for this closure; may return `None`
294-
/// if inference has not yet completed.
295-
pub fn opt_closure_kind(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>)
296-
-> Option<ty::ClosureKind> {
297-
let closure_kind_ty = self.closure_kind_ty(def_id, tcx);
298-
closure_kind_ty.to_opt_closure_kind()
299-
}
300-
301-
/// Returns the closure kind for this closure; may return `None`
302-
/// if inference has not yet completed.
293+
/// Returns the closure kind for this closure; may return a type
294+
/// variable during inference.
303295
pub fn closure_kind_ty(self, def_id: DefId, tcx: TyCtxt<'_, '_, '_>) -> Ty<'tcx> {
304296
self.split(def_id, tcx).closure_kind_ty
305297
}
306298
}
307299

308300
impl<'tcx> ClosureSubsts<'tcx> {
309301
/// Returns the closure kind for this closure; only usable outside
310-
/// of an inference context.
302+
/// of an inference context, because in that context we know that
303+
/// there are no type variables.
311304
pub fn closure_kind(self, def_id: DefId, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> ty::ClosureKind {
312-
self.opt_closure_kind(def_id, tcx).unwrap()
305+
self.split(def_id, tcx).closure_kind_ty.to_opt_closure_kind().unwrap()
313306
}
314307
}
315308

@@ -1514,6 +1507,8 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
15141507

15151508
TyInfer(_) => None,
15161509

1510+
TyError => Some(ty::ClosureKind::Fn),
1511+
15171512
_ => bug!("cannot convert type `{:?}` to a closure kind", self),
15181513
}
15191514
}

src/librustc_mir/build/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,18 @@ pub fn closure_self_ty<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
248248
let closure_expr_hir_id = tcx.hir.node_to_hir_id(closure_expr_id);
249249
let closure_ty = tcx.body_tables(body_id).node_id_to_type(closure_expr_hir_id);
250250

251-
let closure_def_id = tcx.hir.local_def_id(closure_expr_id);
251+
let (closure_def_id, closure_substs) = match closure_ty.sty {
252+
ty::TyClosure(closure_def_id, closure_substs) => (closure_def_id, closure_substs),
253+
_ => bug!("closure expr does not have closure type: {:?}", closure_ty)
254+
};
255+
252256
let region = ty::ReFree(ty::FreeRegion {
253257
scope: closure_def_id,
254258
bound_region: ty::BoundRegion::BrEnv,
255259
});
256260
let region = tcx.mk_region(region);
257261

258-
match tcx.closure_kind(closure_def_id) {
262+
match closure_substs.closure_kind_ty(closure_def_id, tcx).to_opt_closure_kind().unwrap() {
259263
ty::ClosureKind::Fn =>
260264
tcx.mk_ref(region,
261265
ty::TypeAndMut { ty: closure_ty,

src/librustc_trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
511511
let sig = tcx.fn_sig(def_id).subst(tcx, substs.substs);
512512

513513
let env_region = ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrEnv);
514-
let env_ty = match tcx.closure_kind(def_id) {
514+
let env_ty = match substs.closure_kind(def_id, tcx) {
515515
ty::ClosureKind::Fn => tcx.mk_imm_ref(tcx.mk_region(env_region), ty),
516516
ty::ClosureKind::FnMut => tcx.mk_mut_ref(tcx.mk_region(env_region), ty),
517517
ty::ClosureKind::FnOnce => ty,

src/librustc_trans_utils/monomorphize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn resolve_closure<'a, 'tcx> (
8686
requested_kind: ty::ClosureKind)
8787
-> Instance<'tcx>
8888
{
89-
let actual_kind = tcx.closure_kind(def_id);
89+
let actual_kind = substs.closure_kind(def_id, tcx);
9090

9191
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
9292
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),

0 commit comments

Comments
 (0)