Skip to content

Commit

Permalink
Refactor FnSig to contain a Slice for its inputs and outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Dec 6, 2016
1 parent 1eab19d commit 296ec5f
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 114 deletions.
11 changes: 11 additions & 0 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

pub fn mk_fn_sig<I>(self, inputs: I, output: I::Item, variadic: bool)
-> <I::Item as InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>>::Output
where I: Iterator,
I::Item: InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>
{
inputs.chain(iter::once(output)).intern_with(|xs| ty::FnSig {
inputs_and_output: self.intern_type_list(xs),
variadic: variadic
})
}

pub fn mk_existential_predicates<I: InternAs<[ExistentialPredicate<'tcx>],
&'tcx Slice<ExistentialPredicate<'tcx>>>>(self, iter: I)
-> I::Output {
Expand Down
27 changes: 19 additions & 8 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use ty::subst::{Kind, Substs};
use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::error::{ExpectedFound, TypeError};
use std::rc::Rc;
use std::iter;
use syntax::abi;
use hir as ast;
use rustc_data_structures::accumulate_vec::AccumulateVec;

pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;

Expand Down Expand Up @@ -180,21 +182,30 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
-> RelateResult<'tcx, ty::FnSig<'tcx>>
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
{
if a.variadic() != b.variadic() {
if a.variadic != b.variadic {
return Err(TypeError::VariadicMismatch(
expected_found(relation, &a.variadic(), &b.variadic())));
expected_found(relation, &a.variadic, &b.variadic)));
}

if a.inputs().len() != b.inputs().len() {
return Err(TypeError::ArgCount);
}

let inputs = a.inputs().iter().zip(b.inputs()).map(|(&a, &b)| {
relation.relate_with_variance(ty::Contravariant, &a, &b)
}).collect::<Result<Vec<_>, _>>()?;
let output = relation.relate(&a.output(), &b.output())?;

Ok(ty::FnSig::new(inputs, output, a.variadic()))
let inputs_and_output = a.inputs().iter().cloned()
.zip(b.inputs().iter().cloned())
.map(|x| (x, false))
.chain(iter::once(((a.output(), b.output()), true)))
.map(|((a, b), is_output)| {
if is_output {
relation.relate(&a, &b)
} else {
relation.relate_with_variance(ty::Contravariant, &a, &b)
}
}).collect::<Result<AccumulateVec<[_; 8]>, _>>()?;
Ok(ty::FnSig {
inputs_and_output: relation.tcx().intern_type_list(&inputs_and_output),
variadic: a.variadic
})
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
type Lifted = ty::FnSig<'tcx>;
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
tcx.lift(self.inputs()).and_then(|inputs| {
tcx.lift(&self.output()).map(|output| {
ty::FnSig::new(inputs, output, self.variadic())
})
tcx.lift(&self.inputs_and_output).map(|x| {
ty::FnSig {
inputs_and_output: x,
variadic: self.variadic
}
})
}
}
Expand Down Expand Up @@ -585,17 +586,20 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {

impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
ty::FnSig::new(self.inputs().to_owned().fold_with(folder),
self.output().fold_with(folder),
self.variadic())
let inputs_and_output = self.inputs_and_output.fold_with(folder);
ty::FnSig {
inputs_and_output: folder.tcx().intern_type_list(&inputs_and_output),
variadic: self.variadic,
}
}

fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
folder.fold_fn_sig(self)
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.inputs().to_owned().visit_with(visitor) || self.output().visit_with(visitor)
self.inputs().iter().any(|i| i.visit_with(visitor)) ||
self.output().visit_with(visitor)
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,40 +563,31 @@ pub struct ClosureTy<'tcx> {
/// - `variadic` indicates whether this is a variadic function. (only true for foreign fns)
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct FnSig<'tcx> {
inputs: Vec<Ty<'tcx>>,
output: Ty<'tcx>,
variadic: bool
pub inputs_and_output: &'tcx Slice<Ty<'tcx>>,
pub variadic: bool
}

impl<'tcx> FnSig<'tcx> {
pub fn new(inputs: Vec<Ty<'tcx>>, output: Ty<'tcx>, variadic: bool) -> Self {
FnSig { inputs: inputs, output: output, variadic: variadic }
}

pub fn inputs(&self) -> &[Ty<'tcx>] {
&self.inputs
&self.inputs_and_output[..self.inputs_and_output.len() - 1]
}

pub fn output(&self) -> Ty<'tcx> {
self.output
}

pub fn variadic(&self) -> bool {
self.variadic
self.inputs_and_output[self.inputs_and_output.len() - 1]
}
}

pub type PolyFnSig<'tcx> = Binder<FnSig<'tcx>>;

impl<'tcx> PolyFnSig<'tcx> {
pub fn inputs<'a>(&'a self) -> Binder<&[Ty<'tcx>]> {
Binder(self.0.inputs())
pub fn inputs(&self) -> Binder<&[Ty<'tcx>]> {
Binder(self.skip_binder().inputs())
}
pub fn input(&self, index: usize) -> ty::Binder<Ty<'tcx>> {
self.map_bound_ref(|fn_sig| fn_sig.inputs[index])
self.map_bound_ref(|fn_sig| fn_sig.inputs()[index])
}
pub fn output(&self) -> ty::Binder<Ty<'tcx>> {
self.map_bound_ref(|fn_sig| fn_sig.output.clone())
self.map_bound_ref(|fn_sig| fn_sig.output().clone())
}
pub fn variadic(&self) -> bool {
self.skip_binder().variadic
Expand Down Expand Up @@ -1261,8 +1252,8 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
}

// Type accessors for substructures of types
pub fn fn_args(&self) -> ty::Binder<Vec<Ty<'tcx>>> {
ty::Binder(self.fn_sig().inputs().skip_binder().iter().cloned().collect::<Vec<_>>())
pub fn fn_args(&self) -> ty::Binder<&[Ty<'tcx>]> {
self.fn_sig().inputs()
}

pub fn fn_ret(&self) -> Binder<Ty<'tcx>> {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "fn")?;
fn_sig(f, self.inputs(), self.variadic(), self.output())
fn_sig(f, self.inputs(), self.variadic, self.output())
}
}

Expand Down Expand Up @@ -625,7 +625,7 @@ impl fmt::Debug for ty::RegionVid {

impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?}; variadic: {})->{:?}", self.inputs(), self.variadic(), self.output())
write!(f, "({:?}; variadic: {})->{:?}", self.inputs(), self.variadic, self.output())
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,10 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
}

pub fn t_fn(&self, input_tys: &[Ty<'tcx>], output_ty: Ty<'tcx>) -> Ty<'tcx> {
let input_args = input_tys.iter().cloned().collect();
self.infcx.tcx.mk_fn_ptr(self.infcx.tcx.mk_bare_fn(ty::BareFnTy {
unsafety: hir::Unsafety::Normal,
abi: Abi::Rust,
sig: ty::Binder(ty::FnSig {
inputs: input_args,
output: output_ty,
variadic: false,
}),
sig: ty::Binder(self.infcx.tcx.mk_fn_sig(input_tys.iter().cloned(), output_ty, false)),
}))
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
{
debug!("check_call_inputs({:?}, {:?})", sig, args);
if args.len() < sig.inputs().len() ||
(args.len() > sig.inputs().len() && !sig.variadic()) {
(args.len() > sig.inputs().len() && !sig.variadic) {
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
}
for (n, (fn_arg, op_arg)) in sig.inputs().iter().zip(args).enumerate() {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl FnType {

let mut inputs = sig.inputs();
let extra_args = if abi == RustCall {
assert!(!sig.variadic() && extra_args.is_empty());
assert!(!sig.variadic && extra_args.is_empty());

match sig.inputs().last().unwrap().sty {
ty::TyTuple(ref tupled_arguments) => {
Expand All @@ -382,7 +382,7 @@ impl FnType {
}
}
} else {
assert!(sig.variadic() || extra_args.is_empty());
assert!(sig.variadic || extra_args.is_empty());
extra_args
};

Expand Down Expand Up @@ -525,7 +525,7 @@ impl FnType {
FnType {
args: args,
ret: ret,
variadic: sig.variadic(),
variadic: sig.variadic,
cconv: cconv
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let dest_val = adt::MaybeSizedValue::sized(dest); // Can return unsized value
let mut llarg_idx = fcx.fn_ty.ret.is_indirect() as usize;
let mut arg_idx = 0;
for (i, arg_ty) in sig.inputs().into_iter().enumerate() {
for (i, arg_ty) in sig.inputs().iter().enumerate() {
let lldestptr = adt::trans_field_ptr(bcx, sig.output(), dest_val, Disr::from(disr), i);
let arg = &fcx.fn_ty.args[arg_idx];
arg_idx += 1;
Expand Down
24 changes: 9 additions & 15 deletions src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use type_of;
use Disr;
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::hir;
use std::iter;

use syntax_pos::DUMMY_SP;

Expand Down Expand Up @@ -329,13 +330,10 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(

// Make a version with the type of by-ref closure.
let ty::ClosureTy { unsafety, abi, mut sig } = tcx.closure_type(def_id, substs);
sig.0 = ty::FnSig::new({
let mut inputs = sig.0.inputs().to_owned();
inputs.insert(0, ref_closure_ty); // sig has no self type as of yet
inputs
},
sig.0 = tcx.mk_fn_sig(
iter::once(ref_closure_ty).chain(sig.0.inputs().iter().cloned()),
sig.0.output(),
sig.0.variadic()
sig.0.variadic
);
let llref_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
unsafety: unsafety,
Expand All @@ -349,14 +347,10 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
// Make a version of the closure type with the same arguments, but
// with argument #0 being by value.
assert_eq!(abi, Abi::RustCall);
sig.0 = ty::FnSig::new(
{
let mut inputs = sig.0.inputs().to_owned();
inputs[0] = closure_ty;
inputs
},
sig.0 = tcx.mk_fn_sig(
iter::once(closure_ty).chain(sig.0.inputs().iter().skip(1).cloned()),
sig.0.output(),
sig.0.variadic()
sig.0.variadic
);

let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
Expand Down Expand Up @@ -507,8 +501,8 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
};
let sig = tcx.erase_late_bound_regions_and_normalize(sig);
let tuple_input_ty = tcx.intern_tup(sig.inputs());
let sig = ty::FnSig::new(
vec![bare_fn_ty_maybe_ref, tuple_input_ty],
let sig = tcx.mk_fn_sig(
[bare_fn_ty_maybe_ref, tuple_input_ty].iter().cloned(),
sig.output(),
false
);
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
let ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
unsafety: hir::Unsafety::Unsafe,
abi: Abi::C,
sig: ty::Binder(ty::FnSig::new(
vec![tcx.mk_mut_ptr(tcx.types.u8)],
sig: ty::Binder(tcx.mk_fn_sig(
iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
tcx.types.never,
false
)),
Expand Down Expand Up @@ -1091,10 +1091,10 @@ pub fn ty_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
ty::ClosureKind::FnOnce => ty,
};

let sig = sig.map_bound(|sig| ty::FnSig::new(
iter::once(env_ty).chain(sig.inputs().into_iter().cloned()).collect(),
let sig = sig.map_bound(|sig| tcx.mk_fn_sig(
iter::once(env_ty).chain(sig.inputs().iter().cloned()),
sig.output(),
sig.variadic()
sig.variadic
));
Cow::Owned(ty::BareFnTy { unsafety: unsafety, abi: abi, sig: sig })
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
output.pop();
}

if sig.variadic() {
if sig.variadic {
if !sig.inputs().is_empty() {
output.push_str(", ...");
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rustc::session::Session;
use syntax_pos::{Span, DUMMY_SP};

use std::cmp::Ordering;
use std::iter;

fn get_simple_intrinsic(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
let llvm_name = match name {
Expand Down Expand Up @@ -1012,7 +1013,7 @@ fn gen_fn<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>,
trans: &mut for<'b> FnMut(Block<'b, 'tcx>))
-> ValueRef {
let ccx = fcx.ccx;
let sig = ty::FnSig::new(inputs, output, false);
let sig = ccx.tcx().mk_fn_sig(inputs.into_iter(), output, false);
let fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]);

let rust_fn_ty = ccx.tcx().mk_fn_ptr(ccx.tcx().mk_bare_fn(ty::BareFnTy {
Expand Down Expand Up @@ -1047,7 +1048,7 @@ fn get_rust_try_fn<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>,
let fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
unsafety: hir::Unsafety::Unsafe,
abi: Abi::Rust,
sig: ty::Binder(ty::FnSig::new(vec![i8p], tcx.mk_nil(), false)),
sig: ty::Binder(tcx.mk_fn_sig(iter::once(i8p), tcx.mk_nil(), false)),
}));
let output = tcx.types.i32;
let rust_try = gen_fn(fcx, "__rust_try", vec![fn_ty, i8p, i8p], output, trans);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a, 'tcx> TransItem<'tcx> {
assert_eq!(dg.ty(), glue::get_drop_glue_type(tcx, dg.ty()));
let t = dg.ty();

let sig = ty::FnSig::new(vec![tcx.mk_mut_ptr(tcx.types.i8)], tcx.mk_nil(), false);
let sig = tcx.mk_fn_sig(iter::once(tcx.mk_mut_ptr(tcx.types.i8)), tcx.mk_nil(), false);

// Create a FnType for fn(*mut i8) and substitute the real type in
// later - that prevents FnType from splitting fat pointers up.
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<'a, 'tcx> DefPathBasedNames<'a, 'tcx> {
output.pop();
}

if sig.variadic() {
if sig.variadic {
if !sig.inputs().is_empty() {
output.push_str(", ...");
} else {
Expand Down
Loading

0 comments on commit 296ec5f

Please sign in to comment.