Skip to content

Commit 2f46586

Browse files
committed
Separate out the unboxed closure table into two tables, so that we can
generate the closure type and closure kind separately.
1 parent 92f9476 commit 2f46586

File tree

12 files changed

+81
-148
lines changed

12 files changed

+81
-148
lines changed

src/librustc/metadata/common.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ pub enum astencode_tag { // Reserves 0x40 -- 0x5f
139139
tag_table_adjustments = 0x51,
140140
tag_table_moves_map = 0x52,
141141
tag_table_capture_map = 0x53,
142-
tag_table_closures = 0x54,
143-
tag_table_upvar_capture_map = 0x55,
144-
tag_table_capture_modes = 0x56,
145-
tag_table_object_cast_map = 0x57,
142+
tag_table_closure_tys = 0x54,
143+
tag_table_closure_kinds = 0x55,
144+
tag_table_upvar_capture_map = 0x56,
145+
tag_table_capture_modes = 0x57,
146+
tag_table_object_cast_map = 0x58,
146147
}
147148

148149
static first_astencode_tag: uint = tag_ast as uint;
@@ -225,10 +226,7 @@ pub struct LinkMeta {
225226
pub crate_hash: Svh,
226227
}
227228

228-
pub const tag_closures: uint = 0x95;
229-
pub const tag_closure: uint = 0x96;
230-
pub const tag_closure_type: uint = 0x97;
231-
pub const tag_closure_kind: uint = 0x98;
229+
// GAP 0x94...0x98
232230

233231
pub const tag_struct_fields: uint = 0x99;
234232
pub const tag_struct_field: uint = 0x9a;

src/librustc/metadata/encoder.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -618,17 +618,6 @@ fn encode_visibility(rbml_w: &mut Encoder, visibility: ast::Visibility) {
618618
rbml_w.end_tag();
619619
}
620620

621-
fn encode_closure_kind(rbml_w: &mut Encoder, kind: ty::ClosureKind) {
622-
rbml_w.start_tag(tag_closure_kind);
623-
let ch = match kind {
624-
ty::FnClosureKind => 'f',
625-
ty::FnMutClosureKind => 'm',
626-
ty::FnOnceClosureKind => 'o',
627-
};
628-
rbml_w.wr_str(&ch.to_string()[]);
629-
rbml_w.end_tag();
630-
}
631-
632621
fn encode_explicit_self(rbml_w: &mut Encoder,
633622
explicit_self: &ty::ExplicitSelfCategory) {
634623
rbml_w.start_tag(tag_item_trait_method_explicit_self);
@@ -1843,24 +1832,6 @@ fn encode_macro_defs(rbml_w: &mut Encoder,
18431832
rbml_w.end_tag();
18441833
}
18451834

1846-
fn encode_closures<'a>(ecx: &'a EncodeContext, rbml_w: &'a mut Encoder) {
1847-
rbml_w.start_tag(tag_closures);
1848-
for (closure_id, closure) in ecx.tcx.closures.borrow().iter() {
1849-
if closure_id.krate != ast::LOCAL_CRATE {
1850-
continue
1851-
}
1852-
1853-
rbml_w.start_tag(tag_closure);
1854-
encode_def_id(rbml_w, *closure_id);
1855-
rbml_w.start_tag(tag_closure_type);
1856-
write_closure_type(ecx, rbml_w, &closure.closure_type);
1857-
rbml_w.end_tag();
1858-
encode_closure_kind(rbml_w, closure.kind);
1859-
rbml_w.end_tag();
1860-
}
1861-
rbml_w.end_tag();
1862-
}
1863-
18641835
fn encode_struct_field_attrs(rbml_w: &mut Encoder, krate: &ast::Crate) {
18651836
struct StructFieldVisitor<'a, 'b:'a> {
18661837
rbml_w: &'a mut Encoder<'b>,
@@ -2069,7 +2040,6 @@ fn encode_metadata_inner(wr: &mut SeekableMemWriter,
20692040
native_lib_bytes: u64,
20702041
plugin_registrar_fn_bytes: u64,
20712042
macro_defs_bytes: u64,
2072-
closure_bytes: u64,
20732043
impl_bytes: u64,
20742044
misc_bytes: u64,
20752045
item_bytes: u64,
@@ -2084,7 +2054,6 @@ fn encode_metadata_inner(wr: &mut SeekableMemWriter,
20842054
native_lib_bytes: 0,
20852055
plugin_registrar_fn_bytes: 0,
20862056
macro_defs_bytes: 0,
2087-
closure_bytes: 0,
20882057
impl_bytes: 0,
20892058
misc_bytes: 0,
20902059
item_bytes: 0,
@@ -2154,11 +2123,6 @@ fn encode_metadata_inner(wr: &mut SeekableMemWriter,
21542123
encode_macro_defs(&mut rbml_w, krate);
21552124
stats.macro_defs_bytes = rbml_w.writer.tell().unwrap() - i;
21562125

2157-
// Encode the types of all closures in this crate.
2158-
i = rbml_w.writer.tell().unwrap();
2159-
encode_closures(&ecx, &mut rbml_w);
2160-
stats.closure_bytes = rbml_w.writer.tell().unwrap() - i;
2161-
21622126
// Encode the def IDs of impls, for coherence checking.
21632127
i = rbml_w.writer.tell().unwrap();
21642128
encode_impls(&ecx, krate, &mut rbml_w);
@@ -2199,7 +2163,6 @@ fn encode_metadata_inner(wr: &mut SeekableMemWriter,
21992163
println!(" native bytes: {}", stats.native_lib_bytes);
22002164
println!("plugin registrar bytes: {}", stats.plugin_registrar_fn_bytes);
22012165
println!(" macro def bytes: {}", stats.macro_defs_bytes);
2202-
println!(" closure bytes: {}", stats.closure_bytes);
22032166
println!(" impl bytes: {}", stats.impl_bytes);
22042167
println!(" misc bytes: {}", stats.misc_bytes);
22052168
println!(" item bytes: {}", stats.item_bytes);

src/librustc/middle/astencode.rs

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -647,30 +647,7 @@ impl<'tcx> tr for MethodOrigin<'tcx> {
647647
}
648648

649649
pub fn encode_closure_kind(ebml_w: &mut Encoder, kind: ty::ClosureKind) {
650-
use serialize::Encoder;
651-
652-
ebml_w.emit_enum("ClosureKind", |ebml_w| {
653-
match kind {
654-
ty::FnClosureKind => {
655-
ebml_w.emit_enum_variant("FnClosureKind", 0, 3, |_| {
656-
Ok(())
657-
})
658-
}
659-
ty::FnMutClosureKind => {
660-
ebml_w.emit_enum_variant("FnMutClosureKind", 1, 3, |_| {
661-
Ok(())
662-
})
663-
}
664-
ty::FnOnceClosureKind => {
665-
ebml_w.emit_enum_variant("FnOnceClosureKind",
666-
2,
667-
3,
668-
|_| {
669-
Ok(())
670-
})
671-
}
672-
}
673-
}).unwrap()
650+
kind.encode(ebml_w).unwrap();
674651
}
675652

676653
pub trait vtable_decoder_helpers<'tcx> {
@@ -1310,12 +1287,20 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
13101287
})
13111288
}
13121289

1313-
for closure in tcx.closures.borrow().get(&ast_util::local_def(id)).iter() {
1314-
rbml_w.tag(c::tag_table_closures, |rbml_w| {
1290+
for &closure_type in tcx.closure_tys.borrow().get(&ast_util::local_def(id)).iter() {
1291+
rbml_w.tag(c::tag_table_closure_tys, |rbml_w| {
1292+
rbml_w.id(id);
1293+
rbml_w.tag(c::tag_table_val, |rbml_w| {
1294+
rbml_w.emit_closure_type(ecx, closure_type);
1295+
})
1296+
})
1297+
}
1298+
1299+
for &&closure_kind in tcx.closure_kinds.borrow().get(&ast_util::local_def(id)).iter() {
1300+
rbml_w.tag(c::tag_table_closure_kinds, |rbml_w| {
13151301
rbml_w.id(id);
13161302
rbml_w.tag(c::tag_table_val, |rbml_w| {
1317-
rbml_w.emit_closure_type(ecx, &closure.closure_type);
1318-
encode_closure_kind(rbml_w, closure.kind)
1303+
encode_closure_kind(rbml_w, closure_kind)
13191304
})
13201305
})
13211306
}
@@ -1354,8 +1339,10 @@ trait rbml_decoder_decoder_helpers<'tcx> {
13541339
-> subst::Substs<'tcx>;
13551340
fn read_auto_adjustment<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
13561341
-> ty::AutoAdjustment<'tcx>;
1357-
fn read_closure<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1358-
-> ty::Closure<'tcx>;
1342+
fn read_closure_kind<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1343+
-> ty::ClosureKind;
1344+
fn read_closure_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1345+
-> ty::ClosureTy<'tcx>;
13591346
fn read_auto_deref_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
13601347
-> ty::AutoDerefRef<'tcx>;
13611348
fn read_autoref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
@@ -1782,35 +1769,23 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
17821769
}).unwrap()
17831770
}
17841771

1785-
fn read_closure<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
1786-
-> ty::Closure<'tcx> {
1787-
let closure_type = self.read_opaque(|this, doc| {
1772+
fn read_closure_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>)
1773+
-> ty::ClosureKind
1774+
{
1775+
Decodable::decode(self).ok().unwrap()
1776+
}
1777+
1778+
fn read_closure_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
1779+
-> ty::ClosureTy<'tcx>
1780+
{
1781+
self.read_opaque(|this, doc| {
17881782
Ok(tydecode::parse_ty_closure_data(
17891783
doc.data,
17901784
dcx.cdata.cnum,
17911785
doc.start,
17921786
dcx.tcx,
17931787
|s, a| this.convert_def_id(dcx, s, a)))
1794-
}).unwrap();
1795-
let variants = &[
1796-
"FnClosureKind",
1797-
"FnMutClosureKind",
1798-
"FnOnceClosureKind"
1799-
];
1800-
let kind = self.read_enum("ClosureKind", |this| {
1801-
this.read_enum_variant(variants, |_, i| {
1802-
Ok(match i {
1803-
0 => ty::FnClosureKind,
1804-
1 => ty::FnMutClosureKind,
1805-
2 => ty::FnOnceClosureKind,
1806-
_ => panic!("bad enum variant for ty::ClosureKind"),
1807-
})
1808-
})
1809-
}).unwrap();
1810-
ty::Closure {
1811-
closure_type: closure_type,
1812-
kind: kind,
1813-
}
1788+
}).unwrap()
18141789
}
18151790

18161791
/// Converts a def-id that appears in a type. The correct
@@ -1937,11 +1912,17 @@ fn decode_side_tables(dcx: &DecodeContext,
19371912
let adj: ty::AutoAdjustment = val_dsr.read_auto_adjustment(dcx);
19381913
dcx.tcx.adjustments.borrow_mut().insert(id, adj);
19391914
}
1940-
c::tag_table_closures => {
1941-
let closure =
1942-
val_dsr.read_closure(dcx);
1943-
dcx.tcx.closures.borrow_mut().insert(ast_util::local_def(id),
1944-
closure);
1915+
c::tag_table_closure_tys => {
1916+
let closure_ty =
1917+
val_dsr.read_closure_ty(dcx);
1918+
dcx.tcx.closure_tys.borrow_mut().insert(ast_util::local_def(id),
1919+
closure_ty);
1920+
}
1921+
c::tag_table_closure_kinds => {
1922+
let closure_kind =
1923+
val_dsr.read_closure_kind(dcx);
1924+
dcx.tcx.closure_kinds.borrow_mut().insert(ast_util::local_def(id),
1925+
closure_kind);
19451926
}
19461927
_ => {
19471928
dcx.tcx.sess.bug(

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,10 @@ impl OverloadedCallType {
260260
fn from_closure(tcx: &ty::ctxt, closure_did: ast::DefId)
261261
-> OverloadedCallType {
262262
let trait_did =
263-
tcx.closures
263+
tcx.closure_kinds
264264
.borrow()
265265
.get(&closure_did)
266-
.expect("OverloadedCallType::from_closure: didn't \
267-
find closure id")
268-
.kind
266+
.expect("OverloadedCallType::from_closure: didn't find closure id")
269267
.trait_did(tcx);
270268
OverloadedCallType::from_trait_id(tcx, trait_did)
271269
}

src/librustc/middle/ty.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,11 @@ pub struct ctxt<'tcx> {
790790

791791
/// Records the type of each closure. The def ID is the ID of the
792792
/// expression defining the closure.
793-
pub closures: RefCell<DefIdMap<Closure<'tcx>>>,
793+
pub closure_kinds: RefCell<DefIdMap<ClosureKind>>,
794+
795+
/// Records the type of each closure. The def ID is the ID of the
796+
/// expression defining the closure.
797+
pub closure_tys: RefCell<DefIdMap<ClosureTy<'tcx>>>,
794798

795799
pub node_lint_levels: RefCell<FnvHashMap<(ast::NodeId, lint::LintId),
796800
lint::LevelSource>>,
@@ -2251,16 +2255,7 @@ pub struct ItemSubsts<'tcx> {
22512255
pub substs: Substs<'tcx>,
22522256
}
22532257

2254-
/// Records information about each closure.
2255-
#[derive(Clone)]
2256-
pub struct Closure<'tcx> {
2257-
/// The type of the closure.
2258-
pub closure_type: ClosureTy<'tcx>,
2259-
/// The kind of closure this is.
2260-
pub kind: ClosureKind,
2261-
}
2262-
2263-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
2258+
#[derive(Clone, Copy, PartialEq, Eq, Debug, RustcEncodable, RustcDecodable)]
22642259
pub enum ClosureKind {
22652260
FnClosureKind,
22662261
FnMutClosureKind,
@@ -2399,7 +2394,8 @@ pub fn mk_ctxt<'tcx>(s: Session,
23992394
extern_const_variants: RefCell::new(DefIdMap()),
24002395
method_map: RefCell::new(FnvHashMap()),
24012396
dependency_formats: RefCell::new(FnvHashMap()),
2402-
closures: RefCell::new(DefIdMap()),
2397+
closure_kinds: RefCell::new(DefIdMap()),
2398+
closure_tys: RefCell::new(DefIdMap()),
24032399
node_lint_levels: RefCell::new(FnvHashMap()),
24042400
transmute_restrictions: RefCell::new(Vec::new()),
24052401
stability: RefCell::new(stability),
@@ -2446,15 +2442,15 @@ impl<'tcx> ctxt<'tcx> {
24462442
}
24472443

24482444
pub fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind {
2449-
self.closures.borrow()[def_id].kind
2445+
self.closure_kinds.borrow()[def_id]
24502446
}
24512447

24522448
pub fn closure_type(&self,
24532449
def_id: ast::DefId,
24542450
substs: &subst::Substs<'tcx>)
24552451
-> ty::ClosureTy<'tcx>
24562452
{
2457-
self.closures.borrow()[def_id].closure_type.subst(self, substs)
2453+
self.closure_tys.borrow()[def_id].subst(self, substs)
24582454
}
24592455
}
24602456

src/librustc/util/ppaux.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
405405
}
406406
ty_str => "str".to_string(),
407407
ty_closure(ref did, _, substs) => {
408-
let closures = cx.closures.borrow();
409-
closures.get(did).map(|cl| {
410-
closure_to_string(cx, &cl.closure_type.subst(cx, substs))
408+
let closure_tys = cx.closure_tys.borrow();
409+
closure_tys.get(did).map(|closure_type| {
410+
closure_to_string(cx, &closure_type.subst(cx, substs))
411411
}).unwrap_or_else(|| {
412412
if did.krate == ast::LOCAL_CRATE {
413413
let span = cx.map.span(did.node);

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub fn self_type_for_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
273273
}
274274

275275
pub fn kind_for_closure(ccx: &CrateContext, closure_id: ast::DefId) -> ty::ClosureKind {
276-
ccx.tcx().closures.borrow()[closure_id].kind
276+
ccx.tcx().closure_kinds.borrow()[closure_id]
277277
}
278278

279279
pub fn decl_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,

src/librustc_trans/trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn get_or_create_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tc
125125
closure_id: ast::DefId,
126126
substs: &Substs<'tcx>)
127127
-> Option<Datum<'tcx, Rvalue>> {
128-
if !ccx.tcx().closures.borrow().contains_key(&closure_id) {
128+
if !ccx.tcx().closure_kinds.borrow().contains_key(&closure_id) {
129129
// Not a closure.
130130
return None
131131
}

src/librustc_typeck/check/closure.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,8 @@ fn check_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
129129
fn_ty.sig.repr(fcx.tcx()),
130130
kind);
131131

132-
let closure = ty::Closure {
133-
closure_type: fn_ty,
134-
kind: kind,
135-
};
136-
137-
fcx.inh.closures.borrow_mut().insert(expr_def_id, closure);
132+
fcx.inh.closure_tys.borrow_mut().insert(expr_def_id, fn_ty);
133+
fcx.inh.closure_kinds.borrow_mut().insert(expr_def_id, kind);
138134
}
139135

140136
fn deduce_expectations_from_expected_type<'a,'tcx>(

src/librustc_typeck/check/method/probe.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
598598
_ => continue,
599599
};
600600

601-
let closures = self.fcx.inh.closures.borrow();
602-
let closure_data = match closures.get(&closure_def_id) {
603-
Some(data) => data,
601+
let closure_kinds = self.fcx.inh.closure_kinds.borrow();
602+
let closure_kind = match closure_kinds.get(&closure_def_id) {
603+
Some(&k) => k,
604604
None => {
605605
self.tcx().sess.span_bug(
606606
self.span,
@@ -610,7 +610,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
610610
};
611611

612612
// this closure doesn't implement the right kind of `Fn` trait
613-
if closure_data.kind != kind {
613+
if closure_kind != kind {
614614
continue;
615615
}
616616

0 commit comments

Comments
 (0)