core: More option demoding

This commit is contained in:
Brian Anderson 2012-09-27 17:01:28 -07:00
parent 7b0ed94bdc
commit 438bdd687b
24 changed files with 60 additions and 69 deletions

View File

@ -41,11 +41,11 @@ fn load_props(testfile: &Path) -> test_props {
}
do parse_aux_build(ln).iter |ab| {
aux_builds.push(ab);
aux_builds.push(*ab);
}
do parse_exec_env(ln).iter |ee| {
exec_env.push(ee);
exec_env.push(*ee);
}
};
return {

View File

@ -289,8 +289,8 @@ fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: IT, f: fn(T) -> U) -> BU {
#[inline(always)]
pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
lhs: IT, rhs: IT) -> BT {
let size_opt = lhs.size_hint().chain(
|sz1| rhs.size_hint().map(|sz2| sz1+*sz2));
let size_opt = lhs.size_hint().chain_ref(
|sz1| rhs.size_hint().map(|sz2| *sz1+*sz2));
do build_sized_opt(size_opt) |push| {
for lhs.each |x| { push(*x); }
for rhs.each |x| { push(*x); }

View File

@ -75,13 +75,18 @@ enum Option<T> {
if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None }
}
pure fn chain<T, U>(opt: &Option<T>, f: fn(T) -> Option<U>) -> Option<U> {
pure fn chain<T, U>(+opt: Option<T>, f: fn(+t: T) -> Option<U>) -> Option<U> {
/*!
* Update an optional value by optionally running its content through a
* function that returns an option.
*/
match *opt { Some(x) => f(x), None => None }
// XXX write with move match
if opt.is_some() {
f(unwrap(opt))
} else {
None
}
}
pure fn chain_ref<T, U>(opt: &Option<T>,
@ -139,14 +144,7 @@ enum Option<T> {
match *opt { None => move def, Some(ref t) => f(t) }
}
// This should change to by-copy mode; use iter_ref below for by reference
pure fn iter<T>(opt: &Option<T>, f: fn(T)) {
//! Performs an operation on the contained value or does nothing
match *opt { None => (), Some(t) => f(t) }
}
pure fn iter_ref<T>(opt: &Option<T>, f: fn(x: &T)) {
pure fn iter<T>(opt: &Option<T>, f: fn(x: &T)) {
//! Performs an operation on the contained value by reference
match *opt { None => (), Some(ref t) => f(t) }
}
@ -182,13 +180,6 @@ fn swap_unwrap<T>(opt: &mut Option<T>) -> T {
// Some of these should change to be &Option<T>, some should not. See below.
impl<T> Option<T> {
/**
* Update an optional value by optionally running its content through a
* function that returns an option.
*/
pure fn chain<U>(f: fn(T) -> Option<U>) -> Option<U> { chain(&self, f) }
/// Performs an operation on the contained value or does nothing
pure fn iter(f: fn(T)) { iter(&self, f) }
/// Returns true if the option equals `none`
pure fn is_none() -> bool { is_none(&self) }
/// Returns true if the option contains some value
@ -207,7 +198,7 @@ impl<T> &Option<T> {
pure fn map_default<U>(+def: U, f: fn(x: &T) -> U) -> U
{ map_default(self, move def, f) }
/// Performs an operation on the contained value by reference
pure fn iter_ref(f: fn(x: &T)) { iter_ref(self, f) }
pure fn iter(f: fn(x: &T)) { iter(self, f) }
/// Maps a `some` value from one type to another by reference
pure fn map<U>(f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
/// Gets an immutable reference to the value inside a `some`.

View File

@ -884,7 +884,7 @@ fn homedir() {
setenv(~"HOME", ~"");
assert os::homedir().is_none();
oldhome.iter(|s| setenv(~"HOME", s));
oldhome.iter(|s| setenv(~"HOME", *s));
}
#[test]
@ -911,9 +911,9 @@ fn homedir() {
setenv(~"USERPROFILE", ~"/home/PaloAlto");
assert os::homedir() == Some(Path("/home/MountainView"));
option::iter(&oldhome, |s| setenv(~"HOME", s));
option::iter(&oldhome, |s| setenv(~"HOME", *s));
option::iter(&olduserprofile,
|s| setenv(~"USERPROFILE", s));
|s| setenv(~"USERPROFILE", *s));
}
#[test]

View File

@ -240,7 +240,7 @@ fn iterate(ancestors: &AncestorList,
if need_unwind && !nobe_is_dead {
do bail_opt.iter |bail_blk| {
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
bail_blk(tg_opt)
(*bail_blk)(tg_opt)
}
}
}

View File

@ -979,7 +979,7 @@ fn print_mac(s: ps, m: ast::mac) {
Some(@{node: ast::expr_vec(_, _), _}) => (),
_ => word(s.s, ~" ")
}
arg.iter(|a| print_expr(s, a));
arg.iter(|a| print_expr(s, *a));
// FIXME: extension 'body' (#2339)
}
ast::mac_invoc_tt(pth, tts) => {
@ -1177,7 +1177,7 @@ fn print_field(s: ps, field: ast::field) {
ast::expr_loop(blk, opt_ident) => {
head(s, ~"loop");
space(s.s);
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
print_block(s, blk);
}
ast::expr_match(expr, arms) => {
@ -1360,12 +1360,12 @@ fn print_field(s: ps, field: ast::field) {
ast::expr_break(opt_ident) => {
word(s.s, ~"break");
space(s.s);
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
}
ast::expr_again(opt_ident) => {
word(s.s, ~"loop");
space(s.s);
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
}
ast::expr_ret(result) => {
word(s.s, ~"return");

View File

@ -241,7 +241,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
v.visit_pat(inner, e, v),
pat_ident(_, path, inner) => {
visit_path(path, e, v);
do option::iter(&inner) |subpat| { v.visit_pat(subpat, e, v)};
do option::iter(&inner) |subpat| { v.visit_pat(*subpat, e, v)};
}
pat_lit(ex) => v.visit_expr(ex, e, v),
pat_range(e1, e2) => { v.visit_expr(e1, e, v); v.visit_expr(e2, e, v); }
@ -342,10 +342,10 @@ fn visit_struct_def<E>(sd: @struct_def, nm: ast::ident, tps: ~[ty_param],
visit_path(p.path, e, v);
}
do option::iter(&sd.ctor) |ctor| {
visit_class_ctor_helper(ctor, nm, tps, ast_util::local_def(id), e, v);
visit_class_ctor_helper(*ctor, nm, tps, ast_util::local_def(id), e, v);
};
do option::iter(&sd.dtor) |dtor| {
visit_class_dtor_helper(dtor, tps, ast_util::local_def(id), e, v)
visit_class_dtor_helper(*dtor, tps, ast_util::local_def(id), e, v)
};
}

View File

@ -204,7 +204,7 @@ fn field_mutability(d: ebml::Doc) -> ast::class_mutability {
}
fn variant_disr_val(d: ebml::Doc) -> Option<int> {
do option::chain(&ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
int::parse_bytes(ebml::doc_data(val_doc), 10u)
}
}

View File

@ -609,7 +609,7 @@ fn add_to_index_(item: @item, ebml_w: ebml::Writer,
ecx.tcx.sess.str_of(item.ident) +
~"_dtor"),
path, if tps.len() > 0u {
Some(ii_dtor(dtor, item.ident, tps,
Some(ii_dtor(*dtor, item.ident, tps,
local_def(item.id))) }
else { None }, tps);
}
@ -715,7 +715,7 @@ fn add_to_index_(item: @item, ebml_w: ebml::Writer,
ebml_w.end_tag();
}
do opt_trait.iter() |associated_trait| {
encode_trait_ref(ebml_w, ecx, associated_trait)
encode_trait_ref(ebml_w, ecx, *associated_trait)
}
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
ebml_w.end_tag();

View File

@ -723,7 +723,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_def) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ast::serialize_def(ebml_w, def)
ast::serialize_def(ebml_w, *def)
}
}
}
@ -731,7 +731,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_node_type) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ebml_w.emit_ty(ecx, ty);
ebml_w.emit_ty(ecx, *ty);
}
}
}
@ -740,7 +740,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_node_type_subst) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ebml_w.emit_tys(ecx, tys)
ebml_w.emit_tys(ecx, *tys)
}
}
}
@ -749,7 +749,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_freevars) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
do ebml_w.emit_from_vec(*fv) |fv_entry| {
do ebml_w.emit_from_vec(**fv) |fv_entry| {
encode_freevar_entry(ebml_w, *fv_entry)
}
}
@ -761,7 +761,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_tcache) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ebml_w.emit_tpbt(ecx, tpbt);
ebml_w.emit_tpbt(ecx, *tpbt);
}
}
}
@ -770,7 +770,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_param_bounds) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ebml_w.emit_bounds(ecx, pbs)
ebml_w.emit_bounds(ecx, *pbs)
}
}
}
@ -810,7 +810,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_method_map) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
serialize_method_map_entry(ecx, ebml_w, mme)
serialize_method_map_entry(ecx, ebml_w, *mme)
}
}
}
@ -819,7 +819,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_vtable_map) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
encode_vtable_res(ecx, ebml_w, dr);
encode_vtable_res(ecx, ebml_w, *dr);
}
}
}
@ -828,7 +828,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
do ebml_w.tag(c::tag_table_adjustments) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ty::serialize_AutoAdjustment(ebml_w, *adj)
ty::serialize_AutoAdjustment(ebml_w, **adj)
}
}
}

View File

@ -269,7 +269,7 @@ fn missing_ctor(tcx: ty::ctxt, m: matrix, left_ty: ty::t) -> Option<ctor> {
let mut found = ~[];
for m.each |r| {
do option::iter(&pat_ctor_id(tcx, r[0])) |id| {
if !vec::contains(found, id) { found.push(id); }
if !vec::contains(found, *id) { found.push(*id); }
}
}
let variants = ty::enum_variants(tcx, eid);

View File

@ -28,7 +28,7 @@ fn check_item(sess: session, ast_map: ast_map::map,
item_enum(enum_definition, _) => {
for enum_definition.variants.each |var| {
do option::iter(&var.node.disr_expr) |ex| {
v.visit_expr(ex, true, v);
v.visit_expr(*ex, true, v);
}
}
}

View File

@ -264,15 +264,15 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
~"non path/method call expr has type substs??")
}
};
if vec::len(ts) != vec::len(*bounds) {
if vec::len(*ts) != vec::len(*bounds) {
// Fail earlier to make debugging easier
fail fmt!("Internal error: in kind::check_expr, length \
mismatch between actual and declared bounds: actual = \
%s (%u tys), declared = %? (%u tys)",
tys_to_str(cx.tcx, ts), ts.len(),
tys_to_str(cx.tcx, *ts), ts.len(),
*bounds, (*bounds).len());
}
do vec::iter2(ts, *bounds) |ty, bound| {
do vec::iter2(*ts, *bounds) |ty, bound| {
check_bounds(cx, id_to_use, e.span, ty, bound)
}
}
@ -376,7 +376,7 @@ fn check_ty(aty: @ty, cx: ctx, v: visit::vt<ctx>) {
do option::iter(&cx.tcx.node_type_substs.find(id)) |ts| {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(id));
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
do vec::iter2(ts, *bounds) |ty, bound| {
do vec::iter2(*ts, *bounds) |ty, bound| {
check_bounds(cx, aty.id, aty.span, ty, bound)
}
}

View File

@ -670,14 +670,14 @@ fn cat_deref<N:ast_node>(node: N,
match deref_kind(self.tcx, base_cmt.ty) {
deref_ptr(ptr) => {
let lp = do base_cmt.lp.chain |l| {
let lp = do base_cmt.lp.chain_ref |l| {
// Given that the ptr itself is loanable, we can
// loan out deref'd uniq ptrs as the data they are
// the only way to reach the data they point at.
// Other ptr types admit aliases and are therefore
// not loanable.
match ptr {
uniq_ptr => {Some(@lp_deref(l, ptr))}
uniq_ptr => {Some(@lp_deref(*l, ptr))}
gc_ptr | region_ptr(_) | unsafe_ptr => {None}
}
};

View File

@ -923,7 +923,7 @@ fn add_child(name: ident,
namespace_to_str(ns),
self.session.str_of(name)));
do child.span_for_namespace(ns).iter() |sp| {
self.session.span_note(sp,
self.session.span_note(*sp,
#fmt("First definition of %s %s here:",
namespace_to_str(ns),
self.session.str_of(name)));

View File

@ -1252,7 +1252,7 @@ fn alloc_local(cx: block, local: @ast::local) -> block {
let val = alloc_ty(cx, t);
if cx.sess().opts.debuginfo {
do option::iter(&simple_name) |name| {
str::as_c_str(cx.ccx().sess.str_of(name), |buf| {
str::as_c_str(cx.ccx().sess.str_of(*name), |buf| {
llvm::LLVMSetValueName(val, buf)
});
}
@ -1778,7 +1778,7 @@ fn trans_class_dtor(ccx: @crate_ctxt, path: path,
/* If we're monomorphizing, register the monomorphized decl
for the dtor */
do option::iter(&hash_id) |h_id| {
ccx.monomorphized.insert(h_id, lldecl);
ccx.monomorphized.insert(*h_id, lldecl);
}
/* Translate the dtor body */
trans_fn(ccx, path, ast_util::dtor_dec(),

View File

@ -281,7 +281,7 @@ fn build_closure(bcx0: block,
// variables:
do option::iter(&include_ret_handle) |flagptr| {
// Flag indicating we have returned (a by-ref bool):
let flag_datum = Datum {val: flagptr, ty: ty::mk_bool(tcx),
let flag_datum = Datum {val: *flagptr, ty: ty::mk_bool(tcx),
mode: ByRef, source: FromLvalue};
env_vals.push(EnvValue {action: EnvRef,
datum: flag_datum});

View File

@ -472,7 +472,7 @@ fn info() -> Option<node_info> {
impl optional_boxed_ast_expr: get_node_info {
fn info() -> Option<node_info> {
self.chain(|s| s.info())
self.chain_ref(|s| s.info())
}
}

View File

@ -204,7 +204,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
expr_path(_) => {
do cx.ccx.tcx.node_type_substs.find(e.id).iter |ts| {
let id = ast_util::def_id_of_def(cx.ccx.tcx.def_map.get(e.id));
vec::iter2(type_uses_for(cx.ccx, id, ts.len()), ts,
vec::iter2(type_uses_for(cx.ccx, id, ts.len()), *ts,
|uses, subst| {
type_needs(cx, uses, subst)
})
@ -238,7 +238,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
match mth.origin {
typeck::method_static(did) => {
do cx.ccx.tcx.node_type_substs.find(e.id).iter |ts| {
do vec::iter2(type_uses_for(cx.ccx, did, ts.len()), ts)
do vec::iter2(type_uses_for(cx.ccx, did, ts.len()), *ts)
|uses, subst| { type_needs(cx, uses, subst)}
}
}

View File

@ -902,7 +902,7 @@ fn rflags(r: region) -> uint {
fn sflags(substs: &substs) -> uint {
let mut f = 0u;
for substs.tps.each |tt| { f |= get(*tt).flags; }
substs.self_r.iter(|r| f |= rflags(r));
substs.self_r.iter(|r| f |= rflags(*r));
return f;
}
match st {

View File

@ -484,7 +484,7 @@ fn ty_of_fn_decl<AC: ast_conv, RS: region_scope Copy Owned>(
let rb = in_binding_rscope(rscope);
let input_tys = do decl.inputs.mapi |i, a| {
let expected_arg_ty = do expected_tys.chain |e| {
let expected_arg_ty = do expected_tys.chain_ref |e| {
// no guarantee that the correct number of expected args
// were supplied
if i < e.inputs.len() {Some(e.inputs[i])} else {None}

View File

@ -279,7 +279,7 @@ fn check_fn(ccx: @crate_ctxt,
// Update the self_info to contain an accurate self type (taking
// into account explicit self).
let self_info = do self_info.chain |info| {
let self_info = do self_info.chain_ref |info| {
// If the self type is sty_static, we don't have a self ty.
if info.explicit_self.node == ast::sty_static {
None
@ -288,7 +288,7 @@ fn check_fn(ccx: @crate_ctxt,
let ty = method::transform_self_type_for_method(
fcx.tcx(), self_region,
info.self_ty, info.explicit_self.node);
Some({self_ty: ty,.. info})
Some({self_ty: ty,.. *info})
}
};
@ -1864,7 +1864,7 @@ fn check_field(fcx: @fn_ctxt, expr: @ast::expr, is_callee: bool,
fcx.write_ty(id, typ);
}
ast::expr_rec(fields, base) => {
option::iter(&base, |b| { check_expr(fcx, b, expected); });
option::iter(&base, |b| { check_expr(fcx, *b, expected); });
let expected = if expected.is_none() && base.is_some() {
Some(fcx.expr_ty(base.get()))
} else { expected };
@ -1872,8 +1872,8 @@ fn check_field(fcx: @fn_ctxt, expr: @ast::expr, is_callee: bool,
match sty { ty::ty_rec(flds) => Some(flds), _ => None }
);
let fields_t = vec::map(fields, |f| {
bot |= check_expr(fcx, f.node.expr, flds.chain(|flds|
vec::find(flds, |tf| tf.ident == f.node.ident)
bot |= check_expr(fcx, f.node.expr, flds.chain_ref(|flds|
vec::find(*flds, |tf| tf.ident == f.node.ident)
).map(|tf| tf.mt.ty));
let expr_t = fcx.expr_ty(f.node.expr);
let expr_mt = {ty: expr_t, mutbl: f.node.mutbl};

View File

@ -164,7 +164,7 @@ fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path,
}
do subpats.iter() |pats| {
do vec::iter2(pats, arg_types) |subpat, arg_ty| {
do vec::iter2(*pats, arg_types) |subpat, arg_ty| {
check_pat(pcx, subpat, arg_ty);
}
};

View File

@ -484,7 +484,7 @@ fn check_privileged_scopes(crate: @crate) {
let trait_def_id =
self.trait_ref_to_trait_def_id(
trait_ref);
*trait_ref);
if trait_def_id.crate != local_crate {
let session =