Skip to content

Commit 08d1d6c

Browse files
committed
convert the closure_kinds map to just store the origin information
The closure kinds themselves are now completely found in the `ClosureSubsts`.
1 parent 75d61f7 commit 08d1d6c

File tree

10 files changed

+108
-115
lines changed

10 files changed

+108
-115
lines changed

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#![feature(inclusive_range_syntax)]
5050
#![cfg_attr(windows, feature(libc))]
5151
#![feature(macro_vis_matcher)]
52+
#![feature(match_default_bindings)]
5253
#![feature(never_type)]
5354
#![feature(nonzero)]
5455
#![feature(quote)]

src/librustc/middle/mem_categorization.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -753,16 +753,16 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
753753
ty::TyClosure(closure_def_id, closure_substs) => {
754754
match self.infcx {
755755
// During upvar inference we may not know the
756-
// closure kind, just use `Fn`.
756+
// closure kind, just use the LATTICE_BOTTOM value.
757757
Some(infcx) =>
758758
infcx.closure_kind(closure_def_id, closure_substs)
759-
.unwrap_or(ty::ClosureKind::Fn),
759+
.unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
760760

761761
None =>
762762
self.tcx.global_tcx()
763763
.lift(&closure_substs)
764764
.expect("no inference cx, but inference variables in closure ty")
765-
.closure_kind(closure_def_id, self.tcx.global_tcx())
765+
.closure_kind(closure_def_id, self.tcx.global_tcx()),
766766
}
767767
}
768768
ref t => span_bug!(span, "unexpected type for fn in mem_categorization: {:?}", t),

src/librustc/traits/error_reporting.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -681,14 +681,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
681681
if let Some(tables) = self.in_progress_tables {
682682
let tables = tables.borrow();
683683
let closure_hir_id = self.tcx.hir.node_to_hir_id(node_id);
684-
match tables.closure_kinds().get(closure_hir_id) {
685-
Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) => {
686-
err.span_note(span, &format!(
684+
match (found_kind, tables.closure_kind_origins().get(closure_hir_id)) {
685+
(ty::ClosureKind::FnOnce, Some((span, name))) => {
686+
err.span_note(*span, &format!(
687687
"closure is `FnOnce` because it moves the \
688688
variable `{}` out of its environment", name));
689689
},
690-
Some(&(ty::ClosureKind::FnMut, Some((span, name)))) => {
691-
err.span_note(span, &format!(
690+
(ty::ClosureKind::FnMut, Some((span, name))) => {
691+
err.span_note(*span, &format!(
692692
"closure is `FnMut` because it mutates the \
693693
variable `{}` here", name));
694694
},

src/librustc/ty/context.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ pub struct TypeckTables<'tcx> {
360360
/// Records the type of each closure.
361361
closure_tys: ItemLocalMap<ty::PolyFnSig<'tcx>>,
362362

363-
/// Records the kind of each closure and the span and name of the variable
364-
/// that caused the closure to be this kind.
365-
closure_kinds: ItemLocalMap<(ty::ClosureKind, Option<(Span, ast::Name)>)>,
363+
/// Records the reasons that we picked the kind of each closure;
364+
/// not all closures are present in the map.
365+
closure_kind_origins: ItemLocalMap<(Span, ast::Name)>,
366366

367367
generator_sigs: ItemLocalMap<Option<ty::GenSig<'tcx>>>,
368368

@@ -415,7 +415,7 @@ impl<'tcx> TypeckTables<'tcx> {
415415
generator_sigs: ItemLocalMap(),
416416
generator_interiors: ItemLocalMap(),
417417
closure_tys: ItemLocalMap(),
418-
closure_kinds: ItemLocalMap(),
418+
closure_kind_origins: ItemLocalMap(),
419419
liberated_fn_sigs: ItemLocalMap(),
420420
fru_field_types: ItemLocalMap(),
421421
cast_kinds: ItemLocalMap(),
@@ -625,19 +625,17 @@ impl<'tcx> TypeckTables<'tcx> {
625625
}
626626
}
627627

628-
pub fn closure_kinds(&self) -> LocalTableInContext<(ty::ClosureKind,
629-
Option<(Span, ast::Name)>)> {
628+
pub fn closure_kind_origins(&self) -> LocalTableInContext<(Span, ast::Name)> {
630629
LocalTableInContext {
631630
local_id_root: self.local_id_root,
632-
data: &self.closure_kinds
631+
data: &self.closure_kind_origins
633632
}
634633
}
635634

636-
pub fn closure_kinds_mut(&mut self)
637-
-> LocalTableInContextMut<(ty::ClosureKind, Option<(Span, ast::Name)>)> {
635+
pub fn closure_kind_origins_mut(&mut self) -> LocalTableInContextMut<(Span, ast::Name)> {
638636
LocalTableInContextMut {
639637
local_id_root: self.local_id_root,
640-
data: &mut self.closure_kinds
638+
data: &mut self.closure_kind_origins
641639
}
642640
}
643641

@@ -734,7 +732,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
734732
ref pat_adjustments,
735733
ref upvar_capture_map,
736734
ref closure_tys,
737-
ref closure_kinds,
735+
ref closure_kind_origins,
738736
ref liberated_fn_sigs,
739737
ref fru_field_types,
740738

@@ -777,7 +775,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
777775
});
778776

779777
closure_tys.hash_stable(hcx, hasher);
780-
closure_kinds.hash_stable(hcx, hasher);
778+
closure_kind_origins.hash_stable(hcx, hasher);
781779
liberated_fn_sigs.hash_stable(hcx, hasher);
782780
fru_field_types.hash_stable(hcx, hasher);
783781
cast_kinds.hash_stable(hcx, hasher);

src/librustc/ty/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,9 @@ pub enum ClosureKind {
19021902
}
19031903

19041904
impl<'a, 'tcx> ClosureKind {
1905+
// This is the initial value used when doing upvar inference.
1906+
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
1907+
19051908
pub fn trait_did(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> DefId {
19061909
match *self {
19071910
ClosureKind::Fn => tcx.require_lang_item(FnTraitLangItem),

src/librustc_borrowck/borrowck/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
655655
ty::TypeVariants::TyClosure(id, _) => {
656656
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
657657
let hir_id = self.tcx.hir.node_to_hir_id(node_id);
658-
if let Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) =
659-
self.tables.closure_kinds().get(hir_id)
660-
{
661-
err.span_note(span, &format!(
658+
if let Some((span, name)) = self.tables.closure_kind_origins().get(hir_id) {
659+
err.span_note(*span, &format!(
662660
"closure cannot be invoked more than once because \
663661
it moves the variable `{}` out of its environment",
664662
name

src/librustc_borrowck/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#![allow(non_camel_case_types)]
1717

18+
#![feature(match_default_bindings)]
1819
#![feature(quote)]
1920

2021
#[macro_use] extern crate log;

src/librustc_typeck/check/closure.rs

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
140140

141141
self.tables.borrow_mut().closure_tys_mut().insert(expr.hir_id, sig);
142142
if let Some(kind) = opt_kind {
143-
self.tables.borrow_mut().closure_kinds_mut().insert(expr.hir_id, (kind, None));
144143
self.demand_eqtype(expr.span,
145144
kind.to_ty(self.tcx),
146145
substs.closure_kind_ty(expr_def_id, self.tcx));

0 commit comments

Comments
 (0)