Skip to content

Commit

Permalink
librustc: Encode upvar_borrow_map in metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
luqmana committed Aug 9, 2014
1 parent 5dca9fb commit 71df8e6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ pub enum astencode_tag { // Reserves 0x40 -- 0x5f
tag_table_moves_map = 0x52,
tag_table_capture_map = 0x53,
tag_table_unboxed_closure_type = 0x54,
tag_table_upvar_borrow_map = 0x55,
}
static first_astencode_tag: uint = tag_ast as uint;
static last_astencode_tag: uint = tag_table_unboxed_closure_type as uint;
static last_astencode_tag: uint = tag_table_upvar_borrow_map as uint;
impl astencode_tag {
pub fn from_uint(value : uint) -> Option<astencode_tag> {
let is_a_tag = first_astencode_tag <= value && value <= last_astencode_tag;
Expand Down
43 changes: 42 additions & 1 deletion src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use driver::session::Session;
use metadata::decoder;
use middle::def;
use e = metadata::encoder;
use middle::freevars;
use middle::freevars::freevar_entry;
use middle::region;
use metadata::tydecode;
Expand Down Expand Up @@ -551,6 +552,15 @@ impl tr for freevar_entry {
}
}

impl tr for ty::UpvarBorrow {
fn tr(&self, xcx: &ExtendedDecodeContext) -> ty::UpvarBorrow {
ty::UpvarBorrow {
kind: self.kind,
region: self.region.tr(xcx)
}
}
}

// ______________________________________________________________________
// Encoding and decoding of MethodCallee

Expand Down Expand Up @@ -1061,7 +1071,29 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
Ok(encode_freevar_entry(rbml_w, fv_entry))
});
})
})
});

for freevar in fv.iter() {
match freevars::get_capture_mode(tcx, id) {
freevars::CaptureByRef => {
rbml_w.tag(c::tag_table_upvar_borrow_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
let var_id = freevar.def.def_id().node;
let upvar_id = ty::UpvarId {
var_id: var_id,
closure_expr_id: id
};
let upvar_borrow = tcx.upvar_borrow_map.borrow()
.get_copy(&upvar_id);
var_id.encode(rbml_w);
upvar_borrow.encode(rbml_w);
})
})
}
_ => {}
}
}
}

let lid = ast::DefId { krate: ast::LOCAL_CRATE, node: id };
Expand Down Expand Up @@ -1468,6 +1500,15 @@ fn decode_side_tables(xcx: &ExtendedDecodeContext,
}).unwrap().move_iter().collect();
dcx.tcx.freevars.borrow_mut().insert(id, fv_info);
}
c::tag_table_upvar_borrow_map => {
let var_id: ast::NodeId = Decodable::decode(val_dsr).unwrap();
let upvar_id = ty::UpvarId {
var_id: xcx.tr_id(var_id),
closure_expr_id: id
};
let ub: ty::UpvarBorrow = Decodable::decode(val_dsr).unwrap();
dcx.tcx.upvar_borrow_map.borrow_mut().insert(upvar_id, ub.tr(xcx));
}
c::tag_table_tcache => {
let pty = val_dsr.read_polytype(xcx);
let lid = ast::DefId { krate: ast::LOCAL_CRATE, node: id };
Expand Down
8 changes: 3 additions & 5 deletions src/librustc/middle/freevars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![allow(non_camel_case_types)]

use middle::def;
use middle::mem_categorization::Typer;
use middle::resolve;
use middle::ty;
use util::nodemap::{DefIdSet, NodeMap, NodeSet};
Expand Down Expand Up @@ -147,11 +148,8 @@ pub fn with_freevars<T>(tcx: &ty::ctxt, fid: ast::NodeId, f: |&[freevar_entry]|
}
}

pub fn get_capture_mode(tcx: &ty::ctxt,
closure_expr_id: ast::NodeId)
-> CaptureMode
{
let fn_ty = ty::node_id_to_type(tcx, closure_expr_id);
pub fn get_capture_mode<T: Typer>(tcx: &T, closure_expr_id: ast::NodeId) -> CaptureMode {
let fn_ty = tcx.node_ty(closure_expr_id).ok().expect("couldn't find closure ty?");
match ty::ty_closure_store(fn_ty) {
ty::RegionTraitStore(..) => CaptureByRef,
ty::UniqTraitStore => CaptureByValue
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ pub struct UpvarId {
pub closure_expr_id: ast::NodeId,
}

#[deriving(Clone, PartialEq, Eq, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Hash, Show, Encodable, Decodable)]
pub enum BorrowKind {
/// Data must be immutable and is aliasable.
ImmBorrow,
Expand Down Expand Up @@ -634,7 +634,7 @@ pub enum BorrowKind {
* the closure, so sometimes it is necessary for them to be larger
* than the closure lifetime itself.
*/
#[deriving(PartialEq, Clone)]
#[deriving(PartialEq, Clone, Encodable, Decodable)]
pub struct UpvarBorrow {
pub kind: BorrowKind,
pub region: ty::Region,
Expand Down

9 comments on commit 71df8e6

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at luqmana@71df8e6

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging luqmana/rust/match-drop = 71df8e6 into auto

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luqmana/rust/match-drop = 71df8e6 merged ok, testing candidate = 8335a5f0

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at luqmana@71df8e6

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging luqmana/rust/match-drop = 71df8e6 into auto

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luqmana/rust/match-drop = 71df8e6 merged ok, testing candidate = 69c58bc

@bors
Copy link
Contributor

@bors bors commented on 71df8e6 Aug 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 69c58bc

Please sign in to comment.