Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
	* use more convenient mk_substs function
	* remove type annotations
	* use map_bound one level farther outside
	* style improvements
  • Loading branch information
est31 committed Feb 23, 2017
1 parent 21c3898 commit 77f131d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 43 deletions.
14 changes: 3 additions & 11 deletions src/librustc_trans/mir/constant.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 3 additions & 11 deletions src/librustc_trans/mir/rvalue.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 7 additions & 8 deletions src/librustc_typeck/check/coercion.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/closure-no-fn.rs
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs
Expand Up @@ -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

Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/run-pass/closure-to-fn-coercion.rs
Expand Up @@ -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| {},
Expand All @@ -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);
}

0 comments on commit 77f131d

Please sign in to comment.