Skip to content

Commit 77f131d

Browse files
committed
Review changes
* use more convenient mk_substs function * remove type annotations * use map_bound one level farther outside * style improvements
1 parent 21c3898 commit 77f131d

File tree

6 files changed

+26
-43
lines changed

6 files changed

+26
-43
lines changed

src/librustc_trans/mir/constant.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::mir;
2020
use rustc::mir::tcx::LvalueTy;
2121
use rustc::ty::{self, layout, Ty, TyCtxt, TypeFoldable};
2222
use rustc::ty::cast::{CastTy, IntTy};
23-
use rustc::ty::subst::Substs;
23+
use rustc::ty::subst::{Kind, Substs};
2424
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2525
use {abi, adt, base, Disr, machine};
2626
use callee::Callee;
@@ -589,16 +589,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
589589
.unwrap().def_id;
590590
// Now create its substs [Closure, Tuple]
591591
let input = tcx.closure_type(def_id, substs).sig.input(0);
592-
let substs = Substs::for_item(tcx,
593-
call_once,
594-
|_, _| {bug!()},
595-
|def, _| { match def.index {
596-
0 => operand.ty.clone(),
597-
1 => input.skip_binder(),
598-
_ => bug!(),
599-
} }
600-
);
601-
592+
let substs = tcx.mk_substs([operand.ty, input.skip_binder()]
593+
.iter().cloned().map(Kind::from));
602594
Callee::def(self.ccx, call_once, substs)
603595
.reify(self.ccx)
604596
}

src/librustc_trans/mir/rvalue.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use llvm::{self, ValueRef};
1212
use rustc::ty::{self, Ty};
1313
use rustc::ty::cast::{CastTy, IntTy};
1414
use rustc::ty::layout::Layout;
15-
use rustc::ty::subst::Substs;
15+
use rustc::ty::subst::Kind;
1616
use rustc::mir::tcx::LvalueTy;
1717
use rustc::mir;
1818
use middle::lang_items::ExchangeMallocFnLangItem;
@@ -202,16 +202,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
202202
.unwrap().def_id;
203203
// Now create its substs [Closure, Tuple]
204204
let input = bcx.tcx().closure_type(def_id, substs).sig.input(0);
205-
let substs = Substs::for_item(bcx.tcx(),
206-
call_once,
207-
|_, _| {bug!()},
208-
|def, _| { match def.index {
209-
0 => operand.ty.clone(),
210-
1 => input.skip_binder(),
211-
_ => bug!(),
212-
} }
213-
);
214-
205+
let substs = bcx.tcx().mk_substs([operand.ty, input.skip_binder()]
206+
.iter().cloned().map(Kind::from));
215207
OperandValue::Immediate(
216208
Callee::def(bcx.ccx, call_once, substs)
217209
.reify(bcx.ccx))

src/librustc_typeck/check/coercion.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ use rustc::hir::def_id::DefId;
6767
use rustc::infer::{Coercion, InferOk, TypeTrace};
6868
use rustc::traits::{self, ObligationCause, ObligationCauseCode};
6969
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
70-
use rustc::ty::{self, LvaluePreference, TypeVariants, TypeAndMut,
70+
use rustc::ty::{self, LvaluePreference, TypeAndMut,
7171
Ty, ClosureSubsts};
7272
use rustc::ty::fold::TypeFoldable;
7373
use rustc::ty::error::TypeError;
7474
use rustc::ty::relate::RelateResult;
75-
use syntax::ast::NodeId;
7675
use syntax::abi;
7776
use syntax::feature_gate;
7877
use util::common::indent;
@@ -573,7 +572,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
573572
574573
let b = self.shallow_resolve(b);
575574

576-
let node_id_a :NodeId = self.tcx.hir.as_local_node_id(def_id_a).unwrap();
575+
let node_id_a = self.tcx.hir.as_local_node_id(def_id_a).unwrap();
577576
match b.sty {
578577
ty::TyFnPtr(_) if self.tcx.with_freevars(node_id_a, |v| v.is_empty()) => {
579578
if !self.tcx.sess.features.borrow().closure_to_fn_coercion {
@@ -589,16 +588,16 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
589588
// to
590589
// `fn(arg0,arg1,...) -> _`
591590
let sig = self.closure_type(def_id_a, substs_a).sig;
592-
let converted_sig = sig.input(0).map_bound(|v| {
593-
let params_iter = match v.sty {
594-
TypeVariants::TyTuple(params, _) => {
591+
let converted_sig = sig.map_bound(|s| {
592+
let params_iter = match s.inputs()[0].sty {
593+
ty::TyTuple(params, _) => {
595594
params.into_iter().cloned()
596595
}
597596
_ => bug!(),
598597
};
599598
self.tcx.mk_fn_sig(params_iter,
600-
sig.output().skip_binder(),
601-
sig.variadic())
599+
s.output(),
600+
s.variadic)
602601
});
603602
let fn_ty = self.tcx.mk_bare_fn(ty::BareFnTy {
604603
unsafety: hir::Unsafety::Normal,

src/test/compile-fail/closure-no-fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
fn main() {
1515
let mut a = 0u8;
16-
let foo :fn(u8) -> u8 = |v: u8| { a += v; a };
16+
let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
1717
//~^ ERROR mismatched types
1818
let b = 0u8;
19-
let bar :fn() -> u8 = || { b };
19+
let bar: fn() -> u8 = || { b };
2020
//~^ ERROR mismatched types
21-
let baz :fn() -> u8 = || { b } as fn() -> u8;
21+
let baz: fn() -> u8 = || { b } as fn() -> u8;
2222
//~^ ERROR mismatched types
2323
//~^^ ERROR non-scalar cast
2424
}

src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#[cfg(a)]
1515
mod a {
16-
const FOO :fn(u8) -> u8 = |v: u8| { v };
16+
const FOO: fn(u8) -> u8 = |v: u8| { v };
1717
//[a]~^ ERROR non-capturing closure to fn coercion is experimental
1818
//[a]~^^ ERROR mismatched types
1919

@@ -34,7 +34,7 @@ mod b {
3434
fn foo() {
3535
// Items
3636
assert_eq!(func_specific()(), 42);
37-
let foo :fn(u8) -> u8 = |v: u8| { v };
37+
let foo: fn(u8) -> u8 = |v: u8| { v };
3838
//[b]~^ ERROR non-capturing closure to fn coercion is experimental
3939
//[b]~^^ ERROR mismatched types
4040
}

src/test/run-pass/closure-to-fn-coercion.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#![feature(closure_to_fn_coercion)]
1414

15-
const FOO :fn(u8) -> u8 = |v: u8| { v };
15+
const FOO: fn(u8) -> u8 = |v: u8| { v };
1616

1717
const BAR: [fn(&mut u32); 5] = [
1818
|_: &mut u32| {},
@@ -28,14 +28,14 @@ fn func_specific() -> (fn() -> u32) {
2828
fn main() {
2929
// Items
3030
assert_eq!(func_specific()(), 42);
31-
let foo :fn(u8) -> u8 = |v: u8| { v };
31+
let foo: fn(u8) -> u8 = |v: u8| { v };
3232
assert_eq!(foo(31), 31);
3333
// Constants
3434
assert_eq!(FOO(31), 31);
35-
let mut a :u32 = 0;
36-
assert_eq!({BAR[0](&mut a); a }, 0);
37-
assert_eq!({BAR[1](&mut a); a }, 1);
38-
assert_eq!({BAR[2](&mut a); a }, 3);
39-
assert_eq!({BAR[3](&mut a); a }, 6);
40-
assert_eq!({BAR[4](&mut a); a }, 10);
35+
let mut a: u32 = 0;
36+
assert_eq!({ BAR[0](&mut a); a }, 0);
37+
assert_eq!({ BAR[1](&mut a); a }, 1);
38+
assert_eq!({ BAR[2](&mut a); a }, 3);
39+
assert_eq!({ BAR[3](&mut a); a }, 6);
40+
assert_eq!({ BAR[4](&mut a); a }, 10);
4141
}

0 commit comments

Comments
 (0)