From 77f131da1afcaf5582259e7a15048105fea9591d Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 24 Feb 2017 00:32:21 +0100 Subject: [PATCH] Review changes * use more convenient mk_substs function * remove type annotations * use map_bound one level farther outside * style improvements --- src/librustc_trans/mir/constant.rs | 14 +++----------- src/librustc_trans/mir/rvalue.rs | 14 +++----------- src/librustc_typeck/check/coercion.rs | 15 +++++++-------- src/test/compile-fail/closure-no-fn.rs | 6 +++--- .../feature-gate-closure_to_fn_coercion.rs | 4 ++-- src/test/run-pass/closure-to-fn-coercion.rs | 16 ++++++++-------- 6 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/librustc_trans/mir/constant.rs b/src/librustc_trans/mir/constant.rs index e7582061b5419..e6cae2f9f3296 100644 --- a/src/librustc_trans/mir/constant.rs +++ b/src/librustc_trans/mir/constant.rs @@ -20,7 +20,7 @@ use rustc::mir; use rustc::mir::tcx::LvalueTy; use rustc::ty::{self, layout, Ty, TyCtxt, TypeFoldable}; use rustc::ty::cast::{CastTy, IntTy}; -use rustc::ty::subst::Substs; +use rustc::ty::subst::{Kind, Substs}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use {abi, adt, base, Disr, machine}; use callee::Callee; @@ -589,16 +589,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> { .unwrap().def_id; // Now create its substs [Closure, Tuple] let input = tcx.closure_type(def_id, substs).sig.input(0); - let substs = Substs::for_item(tcx, - call_once, - |_, _| {bug!()}, - |def, _| { match def.index { - 0 => operand.ty.clone(), - 1 => input.skip_binder(), - _ => bug!(), - } } - ); - + let substs = tcx.mk_substs([operand.ty, input.skip_binder()] + .iter().cloned().map(Kind::from)); Callee::def(self.ccx, call_once, substs) .reify(self.ccx) } diff --git a/src/librustc_trans/mir/rvalue.rs b/src/librustc_trans/mir/rvalue.rs index 95bb1dd7e0f34..6f6d81a25350f 100644 --- a/src/librustc_trans/mir/rvalue.rs +++ b/src/librustc_trans/mir/rvalue.rs @@ -12,7 +12,7 @@ use llvm::{self, ValueRef}; use rustc::ty::{self, Ty}; use rustc::ty::cast::{CastTy, IntTy}; use rustc::ty::layout::Layout; -use rustc::ty::subst::Substs; +use rustc::ty::subst::Kind; use rustc::mir::tcx::LvalueTy; use rustc::mir; use middle::lang_items::ExchangeMallocFnLangItem; @@ -202,16 +202,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> { .unwrap().def_id; // Now create its substs [Closure, Tuple] let input = bcx.tcx().closure_type(def_id, substs).sig.input(0); - let substs = Substs::for_item(bcx.tcx(), - call_once, - |_, _| {bug!()}, - |def, _| { match def.index { - 0 => operand.ty.clone(), - 1 => input.skip_binder(), - _ => bug!(), - } } - ); - + let substs = bcx.tcx().mk_substs([operand.ty, input.skip_binder()] + .iter().cloned().map(Kind::from)); OperandValue::Immediate( Callee::def(bcx.ccx, call_once, substs) .reify(bcx.ccx)) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index ab498b453dde1..424e3143929f4 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -67,12 +67,11 @@ use rustc::hir::def_id::DefId; use rustc::infer::{Coercion, InferOk, TypeTrace}; use rustc::traits::{self, ObligationCause, ObligationCauseCode}; use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow}; -use rustc::ty::{self, LvaluePreference, TypeVariants, TypeAndMut, +use rustc::ty::{self, LvaluePreference, TypeAndMut, Ty, ClosureSubsts}; use rustc::ty::fold::TypeFoldable; use rustc::ty::error::TypeError; use rustc::ty::relate::RelateResult; -use syntax::ast::NodeId; use syntax::abi; use syntax::feature_gate; use util::common::indent; @@ -573,7 +572,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { let b = self.shallow_resolve(b); - let node_id_a :NodeId = self.tcx.hir.as_local_node_id(def_id_a).unwrap(); + let node_id_a = self.tcx.hir.as_local_node_id(def_id_a).unwrap(); match b.sty { ty::TyFnPtr(_) if self.tcx.with_freevars(node_id_a, |v| v.is_empty()) => { if !self.tcx.sess.features.borrow().closure_to_fn_coercion { @@ -589,16 +588,16 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { // to // `fn(arg0,arg1,...) -> _` let sig = self.closure_type(def_id_a, substs_a).sig; - let converted_sig = sig.input(0).map_bound(|v| { - let params_iter = match v.sty { - TypeVariants::TyTuple(params, _) => { + let converted_sig = sig.map_bound(|s| { + let params_iter = match s.inputs()[0].sty { + ty::TyTuple(params, _) => { params.into_iter().cloned() } _ => bug!(), }; self.tcx.mk_fn_sig(params_iter, - sig.output().skip_binder(), - sig.variadic()) + s.output(), + s.variadic) }); let fn_ty = self.tcx.mk_bare_fn(ty::BareFnTy { unsafety: hir::Unsafety::Normal, diff --git a/src/test/compile-fail/closure-no-fn.rs b/src/test/compile-fail/closure-no-fn.rs index 6ad65523833a9..fe179e8a48f16 100644 --- a/src/test/compile-fail/closure-no-fn.rs +++ b/src/test/compile-fail/closure-no-fn.rs @@ -13,12 +13,12 @@ fn main() { let mut a = 0u8; - let foo :fn(u8) -> u8 = |v: u8| { a += v; a }; + let foo: fn(u8) -> u8 = |v: u8| { a += v; a }; //~^ ERROR mismatched types let b = 0u8; - let bar :fn() -> u8 = || { b }; + let bar: fn() -> u8 = || { b }; //~^ ERROR mismatched types - let baz :fn() -> u8 = || { b } as fn() -> u8; + let baz: fn() -> u8 = || { b } as fn() -> u8; //~^ ERROR mismatched types //~^^ ERROR non-scalar cast } diff --git a/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs b/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs index 20ed24f025b89..d074a35628ea8 100644 --- a/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs +++ b/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs @@ -13,7 +13,7 @@ #[cfg(a)] mod a { - const FOO :fn(u8) -> u8 = |v: u8| { v }; + const FOO: fn(u8) -> u8 = |v: u8| { v }; //[a]~^ ERROR non-capturing closure to fn coercion is experimental //[a]~^^ ERROR mismatched types @@ -34,7 +34,7 @@ mod b { fn foo() { // Items assert_eq!(func_specific()(), 42); - let foo :fn(u8) -> u8 = |v: u8| { v }; + let foo: fn(u8) -> u8 = |v: u8| { v }; //[b]~^ ERROR non-capturing closure to fn coercion is experimental //[b]~^^ ERROR mismatched types } diff --git a/src/test/run-pass/closure-to-fn-coercion.rs b/src/test/run-pass/closure-to-fn-coercion.rs index f3f0736041b54..13d1d6aa13900 100644 --- a/src/test/run-pass/closure-to-fn-coercion.rs +++ b/src/test/run-pass/closure-to-fn-coercion.rs @@ -12,7 +12,7 @@ #![feature(closure_to_fn_coercion)] -const FOO :fn(u8) -> u8 = |v: u8| { v }; +const FOO: fn(u8) -> u8 = |v: u8| { v }; const BAR: [fn(&mut u32); 5] = [ |_: &mut u32| {}, @@ -28,14 +28,14 @@ fn func_specific() -> (fn() -> u32) { fn main() { // Items assert_eq!(func_specific()(), 42); - let foo :fn(u8) -> u8 = |v: u8| { v }; + let foo: fn(u8) -> u8 = |v: u8| { v }; assert_eq!(foo(31), 31); // Constants assert_eq!(FOO(31), 31); - let mut a :u32 = 0; - assert_eq!({BAR[0](&mut a); a }, 0); - assert_eq!({BAR[1](&mut a); a }, 1); - assert_eq!({BAR[2](&mut a); a }, 3); - assert_eq!({BAR[3](&mut a); a }, 6); - assert_eq!({BAR[4](&mut a); a }, 10); + let mut a: u32 = 0; + assert_eq!({ BAR[0](&mut a); a }, 0); + assert_eq!({ BAR[1](&mut a); a }, 1); + assert_eq!({ BAR[2](&mut a); a }, 3); + assert_eq!({ BAR[3](&mut a); a }, 6); + assert_eq!({ BAR[4](&mut a); a }, 10); }