Review changes

* use more convenient mk_substs function
	* remove type annotations
	* use map_bound one level farther outside
	* style improvements
This commit is contained in:
est31 2017-02-24 00:32:21 +01:00
parent 21c38988f4
commit 77f131da1a
6 changed files with 26 additions and 43 deletions

View File

@ -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)
}

View File

@ -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))

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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);
}