Skip to content

Commit

Permalink
auto merge of #13203 : Kimundi/rust/de-map-vec3, r=cmr
Browse files Browse the repository at this point in the history
They required unnecessary temporaries, are replaced with iterators, and would conflict with a possible future `Iterable` trait.
  • Loading branch information
bors committed Mar 30, 2014
2 parents 86890b9 + c356e3b commit d79fbba
Show file tree
Hide file tree
Showing 58 changed files with 278 additions and 325 deletions.
5 changes: 4 additions & 1 deletion src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,10 @@ access local variables in the enclosing scope.
~~~~
let mut max = 0;
[1, 2, 3].map(|x| if *x > max { max = *x });
let f = |x: int| if x > max { max = x };
for x in [1, 2, 3].iter() {
f(*x);
}
~~~~
Stack closures are very efficient because their environment is
Expand Down
6 changes: 4 additions & 2 deletions src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: proc:(**libc::c_char) -> T) -> T
// Next, convert each of the byte strings into a pointer. This is
// technically unsafe as the caller could leak these pointers out of our
// scope.
let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
let mut ptrs: Vec<_> = tmps.iter().map(|tmp| tmp.with_ref(|buf| buf)).collect();

// Finally, make sure we add a null pointer.
ptrs.push(ptr::null());
Expand All @@ -622,7 +622,9 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: proc:(*c_void) -> T) -> T {
}

// Once again, this is unsafe.
let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
let mut ptrs: Vec<*libc::c_char> = tmps.iter()
.map(|tmp| tmp.with_ref(|buf| buf))
.collect();
ptrs.push(ptr::null());

cb(ptrs.as_ptr() as *c_void)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
}

// Internalize everything but the reachable symbols of the current module
let cstrs = reachable.map(|s| s.to_c_str());
let arr = cstrs.map(|c| c.with_ref(|p| p));
let cstrs: Vec<::std::c_str::CString> = reachable.iter().map(|s| s.to_c_str()).collect();
let arr: Vec<*i8> = cstrs.iter().map(|c| c.with_ref(|p| p)).collect();
let ptr = arr.as_ptr();
unsafe {
llvm::LLVMRustRunRestrictionPass(llmod, ptr as **libc::c_char,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,9 @@ pub fn build_session_options(matches: &getopts::Matches) -> session::Options {
NoDebugInfo
};

let addl_lib_search_paths = matches.opt_strs("L").map(|s| {
let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| {
Path::new(s.as_slice())
}).move_iter().collect();
}).collect();

let cfg = parse_cfgspecs(matches.opt_strs("cfg").move_iter().collect());
let test = matches.opt_present("test");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ impl TypeNames {
}

pub fn types_to_str(&self, tys: &[Type]) -> ~str {
let strs = tys.map(|t| self.type_to_str(*t));
let strs: Vec<~str> = tys.iter().map(|t| self.type_to_str(*t)).collect();
format!("[{}]", strs.connect(","))
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ pub fn get_rust_path() -> Option<~str> {
pub fn rust_path() -> Vec<Path> {
let mut env_rust_path: Vec<Path> = match get_rust_path() {
Some(env_path) => {
let env_path_components: Vec<&str> =
env_path.split_str(PATH_ENTRY_SEPARATOR).collect();
env_path_components.map(|&s| Path::new(s))
let env_path_components =
env_path.split_str(PATH_ENTRY_SEPARATOR);
env_path_components.map(|s| Path::new(s)).collect()
}
None => Vec::new()
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn raw_pat(p: @Pat) -> @Pat {

fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, pats: Vec<@Pat> ) {
assert!((!pats.is_empty()));
let ext = match is_useful(cx, &pats.map(|p| vec!(*p)), [wild()]) {
let ext = match is_useful(cx, &pats.iter().map(|p| vec!(*p)).collect(), [wild()]) {
not_useful => {
// This is good, wildcard pattern isn't reachable
return;
Expand Down Expand Up @@ -692,12 +692,12 @@ fn specialize(cx: &MatchCheckCtxt,
DefVariant(_, variant_id, _) => {
if variant(variant_id) == *ctor_id {
let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id);
let args = struct_fields.map(|sf| {
let args = struct_fields.iter().map(|sf| {
match pattern_fields.iter().find(|f| f.ident.name == sf.name) {
Some(f) => f.pat,
_ => wild()
}
});
}).collect();
Some(vec::append(args, r.tail()))
} else {
None
Expand Down
16 changes: 10 additions & 6 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4707,18 +4707,20 @@ impl<'a> Resolver<'a> {
path: &Path,
namespace: Namespace)
-> Option<(Def, LastPrivate)> {
let module_path_idents = path.segments.init().map(|ps| ps.identifier);
let module_path_idents = path.segments.init().iter()
.map(|ps| ps.identifier)
.collect::<Vec<_>>();

let containing_module;
let last_private;
match self.resolve_module_path(self.current_module,
module_path_idents,
module_path_idents.as_slice(),
UseLexicalScope,
path.span,
PathSearch) {
Failed => {
let msg = format!("use of undeclared module `{}`",
self.idents_to_str(module_path_idents));
self.idents_to_str(module_path_idents.as_slice()));
self.resolve_error(path.span, msg);
return None;
}
Expand Down Expand Up @@ -4772,21 +4774,23 @@ impl<'a> Resolver<'a> {
path: &Path,
namespace: Namespace)
-> Option<(Def, LastPrivate)> {
let module_path_idents = path.segments.init().map(|ps| ps.identifier);
let module_path_idents = path.segments.init().iter()
.map(|ps| ps.identifier)
.collect::<Vec<_>>();

let root_module = self.graph_root.get_module();

let containing_module;
let last_private;
match self.resolve_module_path_from_root(root_module,
module_path_idents,
module_path_idents.as_slice(),
0,
path.span,
PathSearch,
LastMod(AllPublic)) {
Failed => {
let msg = format!("use of undeclared module `::{}`",
self.idents_to_str(module_path_idents));
self.idents_to_str(module_path_idents.as_slice()));
self.resolve_error(path.span, msg);
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<'a> LifetimeContext<'a> {
referenced_idents={:?} \
early_count={}",
n,
referenced_idents.map(lifetime_show),
referenced_idents.iter().map(lifetime_show).collect::<Vec<token::InternedString>>(),
early_count);
if referenced_idents.is_empty() {
let scope1 = LateScope(n, &generics.lifetimes, scope);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<T:Subst> Subst for Vec<T> {
fn subst_spanned(&self, tcx: &ty::ctxt,
substs: &ty::substs,
span: Option<Span>) -> Vec<T> {
self.map(|t| t.subst_spanned(tcx, substs, span))
self.iter().map(|t| t.subst_spanned(tcx, substs, span)).collect()
}
}
impl<T:Subst> Subst for Rc<T> {
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Subst for ty::substs {
ty::substs {
regions: self.regions.subst_spanned(tcx, substs, span),
self_ty: self.self_ty.map(|typ| typ.subst_spanned(tcx, substs, span)),
tps: self.tps.map(|typ| typ.subst_spanned(tcx, substs, span))
tps: self.tps.iter().map(|typ| typ.subst_spanned(tcx, substs, span)).collect()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,10 +1578,10 @@ fn compile_submatch_continue<'r,
let pat_ty = node_id_type(bcx, pat_id);
let pat_repr = adt::represent_type(bcx.ccx(), pat_ty);
expr::with_field_tys(tcx, pat_ty, Some(pat_id), |discr, field_tys| {
let rec_vals = rec_fields.map(|field_name| {
let rec_vals = rec_fields.iter().map(|field_name| {
let ix = ty::field_idx_strict(tcx, field_name.name, field_tys);
adt::trans_field_ptr(bcx, pat_repr, val, discr, ix)
});
}).collect();
compile_submatch(
bcx,
enter_rec_or_struct(bcx,
Expand Down
32 changes: 16 additions & 16 deletions src/librustc/middle/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
}
ty::ty_struct(def_id, ref substs) => {
let fields = ty::lookup_struct_fields(cx.tcx(), def_id);
let mut ftys = fields.map(|field| {
let mut ftys = fields.iter().map(|field| {
ty::lookup_field_type(cx.tcx(), def_id, field.id, substs)
});
}).collect::<Vec<_>>();
let packed = ty::lookup_packed(cx.tcx(), def_id);
let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag();
if dtor { ftys.push(ty::mk_bool()); }
Expand All @@ -158,7 +158,7 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {

if cases.iter().all(|c| c.tys.len() == 0) {
// All bodies empty -> intlike
let discrs = cases.map(|c| c.discr);
let discrs: Vec<u64> = cases.iter().map(|c| c.discr).collect();
let bounds = IntBounds {
ulo: *discrs.iter().min().unwrap(),
uhi: *discrs.iter().max().unwrap(),
Expand Down Expand Up @@ -218,12 +218,12 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
let bounds = IntBounds { ulo: 0, uhi: (cases.len() - 1) as u64,
slo: 0, shi: (cases.len() - 1) as i64 };
let ity = range_to_inttype(cx, hint, &bounds);
return General(ity, cases.map(|c| {
return General(ity, cases.iter().map(|c| {
let discr = vec!(ty_of_inttype(ity));
mk_struct(cx,
vec::append(discr, c.tys.as_slice()).as_slice(),
false)
}))
}).collect())
}
_ => cx.sess().bug("adt::represent_type called on non-ADT type")
}
Expand Down Expand Up @@ -270,18 +270,18 @@ impl Case {
}

fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &ty::substs) -> Vec<Case> {
ty::enum_variants(tcx, def_id).map(|vi| {
let arg_tys = vi.args.map(|&raw_ty| {
ty::enum_variants(tcx, def_id).iter().map(|vi| {
let arg_tys = vi.args.iter().map(|&raw_ty| {
ty::subst(tcx, substs, raw_ty)
});
}).collect();
Case { discr: vi.disr_val, tys: arg_tys }
})
}).collect()
}


fn mk_struct(cx: &CrateContext, tys: &[ty::t], packed: bool) -> Struct {
let lltys = tys.map(|&ty| type_of::sizing_type_of(cx, ty));
let llty_rec = Type::struct_(cx, lltys, packed);
let lltys = tys.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
Struct {
size: machine::llsize_of_alloc(cx, llty_rec) /*bad*/as u64,
align: machine::llalign_of_min(cx, llty_rec) /*bad*/as u64,
Expand Down Expand Up @@ -464,9 +464,9 @@ fn generic_type_of(cx: &CrateContext, r: &Repr, name: Option<&str>, sizing: bool

fn struct_llfields(cx: &CrateContext, st: &Struct, sizing: bool) -> Vec<Type> {
if sizing {
st.fields.map(|&ty| type_of::sizing_type_of(cx, ty))
st.fields.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
} else {
st.fields.map(|&ty| type_of::type_of(cx, ty))
st.fields.iter().map(|&ty| type_of::type_of(cx, ty)).collect()
}
}

Expand Down Expand Up @@ -700,7 +700,7 @@ fn struct_field_ptr(bcx: &Block, st: &Struct, val: ValueRef, ix: uint,
let ccx = bcx.ccx();

let val = if needs_cast {
let fields = st.fields.map(|&ty| type_of::type_of(ccx, ty));
let fields = st.fields.iter().map(|&ty| type_of::type_of(ccx, ty)).collect::<Vec<_>>();
let real_ty = Type::struct_(ccx, fields.as_slice(), st.packed);
PointerCast(bcx, val, real_ty.ptr_to())
} else {
Expand Down Expand Up @@ -773,11 +773,11 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
vals).as_slice(),
false)
} else {
let vals = nonnull.fields.map(|&ty| {
let vals = nonnull.fields.iter().map(|&ty| {
// Always use null even if it's not the `ptrfield`th
// field; see #8506.
C_null(type_of::sizing_type_of(ccx, ty))
}).move_iter().collect::<Vec<ValueRef> >();
}).collect::<Vec<ValueRef>>();
C_struct(ccx, build_const_struct(ccx,
nonnull,
vals.as_slice()).as_slice(),
Expand Down
13 changes: 8 additions & 5 deletions src/librustc/middle/trans/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
let temp_scope = fcx.push_custom_cleanup_scope();

// Prepare the output operands
let outputs = ia.outputs.map(|&(ref c, out)| {
let outputs = ia.outputs.iter().map(|&(ref c, out)| {
constraints.push((*c).clone());

let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
out_datum.val

});
}).collect::<Vec<_>>();

// Now the input operands
let inputs = ia.inputs.map(|&(ref c, input)| {
let inputs = ia.inputs.iter().map(|&(ref c, input)| {
constraints.push((*c).clone());

let in_datum = unpack_datum!(bcx, expr::trans(bcx, input));
Expand All @@ -57,12 +57,15 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
cleanup::CustomScope(temp_scope),
callee::DontAutorefArg)
})
});
}).collect::<Vec<_>>();

// no failure occurred preparing operands, no need to cleanup
fcx.pop_custom_cleanup_scope(temp_scope);

let mut constraints = constraints.map(|s| s.get().to_str()).connect(",");
let mut constraints = constraints.iter()
.map(|s| s.get().to_str())
.collect::<Vec<~str>>()
.connect(",");

let mut clobbers = getClobbers();
if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn Invoke(cx: &Block,
terminate(cx, "Invoke");
debug!("Invoke({} with arguments ({}))",
cx.val_to_str(fn_),
args.map(|a| cx.val_to_str(*a)).connect(", "));
args.iter().map(|a| cx.val_to_str(*a)).collect::<Vec<~str>>().connect(", "));
B(cx).invoke(fn_, args, then, catch, attributes)
}

Expand Down
11 changes: 7 additions & 4 deletions src/librustc/middle/trans/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,13 @@ impl<'a> Builder<'a> {
let alignstack = if alignstack { lib::llvm::True }
else { lib::llvm::False };

let argtys = inputs.map(|v| {
let argtys = inputs.iter().map(|v| {
debug!("Asm Input Type: {:?}", self.ccx.tn.val_to_str(*v));
val_ty(*v)
});
}).collect::<Vec<_>>();

debug!("Asm Output Type: {:?}", self.ccx.tn.type_to_str(output));
let fty = Type::func(argtys, &output);
let fty = Type::func(argtys.as_slice(), &output);
unsafe {
let v = llvm::LLVMInlineAsm(
fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
Expand All @@ -800,7 +800,10 @@ impl<'a> Builder<'a> {

debug!("Call {} with args ({})",
self.ccx.tn.val_to_str(llfn),
args.map(|&v| self.ccx.tn.val_to_str(v)).connect(", "));
args.iter()
.map(|&v| self.ccx.tn.val_to_str(v))
.collect::<Vec<~str>>()
.connect(", "));

unsafe {
let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,10 @@ pub fn node_id_type_params(bcx: &Block, node: ExprOrMethodCall) -> Vec<ty::t> {
if !params.iter().all(|t| !ty::type_needs_infer(*t)) {
bcx.sess().bug(
format!("type parameters for node {:?} include inference types: {}",
node, params.map(|t| bcx.ty_to_str(*t)).connect(",")));
node, params.iter()
.map(|t| bcx.ty_to_str(*t))
.collect::<Vec<~str>>()
.connect(",")));
}

match bcx.fcx.param_substs {
Expand Down
Loading

0 comments on commit d79fbba

Please sign in to comment.