Skip to content

Commit 54f4f39

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 eb26e30 commit 54f4f39

File tree

10 files changed

+108
-115
lines changed

10 files changed

+108
-115
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#![feature(inclusive_range_syntax)]
5252
#![cfg_attr(windows, feature(libc))]
5353
#![feature(macro_vis_matcher)]
54+
#![feature(match_default_bindings)]
5455
#![feature(never_type)]
5556
#![feature(nonzero)]
5657
#![feature(quote)]

src/librustc/middle/mem_categorization.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
663663
if let Some(tables) = self.in_progress_tables {
664664
let tables = tables.borrow();
665665
let closure_hir_id = self.tcx.hir.node_to_hir_id(node_id);
666-
match tables.closure_kinds().get(closure_hir_id) {
667-
Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) => {
668-
err.span_note(span, &format!(
666+
match (found_kind, tables.closure_kind_origins().get(closure_hir_id)) {
667+
(ty::ClosureKind::FnOnce, Some((span, name))) => {
668+
err.span_note(*span, &format!(
669669
"closure is `FnOnce` because it moves the \
670670
variable `{}` out of its environment", name));
671671
},
672-
Some(&(ty::ClosureKind::FnMut, Some((span, name)))) => {
673-
err.span_note(span, &format!(
672+
(ty::ClosureKind::FnMut, Some((span, name))) => {
673+
err.span_note(*span, &format!(
674674
"closure is `FnMut` because it mutates the \
675675
variable `{}` here", name));
676676
},

src/librustc/ty/context.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ pub struct TypeckTables<'tcx> {
359359
/// Records the type of each closure.
360360
closure_tys: ItemLocalMap<ty::PolyFnSig<'tcx>>,
361361

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

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

@@ -414,7 +414,7 @@ impl<'tcx> TypeckTables<'tcx> {
414414
generator_sigs: ItemLocalMap(),
415415
generator_interiors: ItemLocalMap(),
416416
closure_tys: ItemLocalMap(),
417-
closure_kinds: ItemLocalMap(),
417+
closure_kind_origins: ItemLocalMap(),
418418
liberated_fn_sigs: ItemLocalMap(),
419419
fru_field_types: ItemLocalMap(),
420420
cast_kinds: ItemLocalMap(),
@@ -624,19 +624,17 @@ impl<'tcx> TypeckTables<'tcx> {
624624
}
625625
}
626626

627-
pub fn closure_kinds(&self) -> LocalTableInContext<(ty::ClosureKind,
628-
Option<(Span, ast::Name)>)> {
627+
pub fn closure_kind_origins(&self) -> LocalTableInContext<(Span, ast::Name)> {
629628
LocalTableInContext {
630629
local_id_root: self.local_id_root,
631-
data: &self.closure_kinds
630+
data: &self.closure_kind_origins
632631
}
633632
}
634633

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

@@ -733,7 +731,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
733731
ref pat_adjustments,
734732
ref upvar_capture_map,
735733
ref closure_tys,
736-
ref closure_kinds,
734+
ref closure_kind_origins,
737735
ref liberated_fn_sigs,
738736
ref fru_field_types,
739737

@@ -776,7 +774,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
776774
});
777775

778776
closure_tys.hash_stable(hcx, hasher);
779-
closure_kinds.hash_stable(hcx, hasher);
777+
closure_kind_origins.hash_stable(hcx, hasher);
780778
liberated_fn_sigs.hash_stable(hcx, hasher);
781779
fru_field_types.hash_stable(hcx, hasher);
782780
cast_kinds.hash_stable(hcx, hasher);

src/librustc/ty/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,9 @@ pub enum ClosureKind {
19371937
}
19381938

19391939
impl<'a, 'tcx> ClosureKind {
1940+
// This is the initial value used when doing upvar inference.
1941+
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
1942+
19401943
pub fn trait_did(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> DefId {
19411944
match *self {
19421945
ClosureKind::Fn => tcx.require_lang_item(FnTraitLangItem),

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 2 additions & 4 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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)