From 5bda576cd6b3be40f62a37e134ee7245e911fb8b Mon Sep 17 00:00:00 2001 From: James Miller Date: Thu, 21 Apr 2016 11:43:01 +1200 Subject: [PATCH] Factor out function call checking to a helper method The logic for checking `call` and `invoke` instructions was duplicated between them, so factor it out to a helper method. --- src/librustc_trans/builder.rs | 77 +++++++++++++-------------------- src/librustc_trans/mir/block.rs | 2 +- 2 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/librustc_trans/builder.rs b/src/librustc_trans/builder.rs index c7de7fd3695..9f032cdbfe5 100644 --- a/src/librustc_trans/builder.rs +++ b/src/librustc_trans/builder.rs @@ -175,30 +175,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .collect::>() .join(", ")); - if cfg!(debug_assertions) { - let mut fn_ty = val_ty(llfn); - // Strip off pointers - while fn_ty.kind() == llvm::TypeKind::Pointer { - fn_ty = fn_ty.element_type(); - } - - assert!(fn_ty.kind() == llvm::TypeKind::Function, - "builder::invoke not passed a function"); - - let param_tys = fn_ty.func_params(); - - let iter = param_tys.into_iter() - .zip(args.iter().map(|&v| val_ty(v))); - for (i, (expected_ty, actual_ty)) in iter.enumerate() { - if expected_ty != actual_ty { - bug!("Type mismatch in invoke of {:?}. \ - Expected {:?} for param {}, got {:?}", - Value(llfn), - expected_ty, i, actual_ty); - - } - } - } + check_call("invoke", llfn, args); let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _); @@ -880,30 +857,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .collect::>() .join(", ")); - if cfg!(debug_assertions) { - let mut fn_ty = val_ty(llfn); - // Strip off pointers - while fn_ty.kind() == llvm::TypeKind::Pointer { - fn_ty = fn_ty.element_type(); - } - - assert!(fn_ty.kind() == llvm::TypeKind::Function, - "builder::call not passed a function"); - - let param_tys = fn_ty.func_params(); - - let iter = param_tys.into_iter() - .zip(args.iter().map(|&v| val_ty(v))); - for (i, (expected_ty, actual_ty)) in iter.enumerate() { - if expected_ty != actual_ty { - bug!("Type mismatch in function call of {:?}. \ - Expected {:?} for param {}, got {:?}", - Value(llfn), - expected_ty, i, actual_ty); - - } - } - } + check_call("call", llfn, args); let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _); @@ -1147,3 +1101,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } } + +fn check_call(typ: &str, llfn: ValueRef, args: &[ValueRef]) { + if cfg!(debug_assertions) { + let mut fn_ty = val_ty(llfn); + // Strip off pointers + while fn_ty.kind() == llvm::TypeKind::Pointer { + fn_ty = fn_ty.element_type(); + } + + assert!(fn_ty.kind() == llvm::TypeKind::Function, + "builder::{} not passed a function", typ); + + let param_tys = fn_ty.func_params(); + + let iter = param_tys.into_iter() + .zip(args.iter().map(|&v| val_ty(v))); + for (i, (expected_ty, actual_ty)) in iter.enumerate() { + if expected_ty != actual_ty { + bug!("Type mismatch in function call of {:?}. \ + Expected {:?} for param {}, got {:?}", + Value(llfn), + expected_ty, i, actual_ty); + + } + } + } +} diff --git a/src/librustc_trans/mir/block.rs b/src/librustc_trans/mir/block.rs index d6a42bea22a..d0b47934bcf 100644 --- a/src/librustc_trans/mir/block.rs +++ b/src/librustc_trans/mir/block.rs @@ -444,7 +444,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { "bad final argument to \"rust-call\" fn {:?}", tuple.ty) }; - // Handle both by-ref and immediate tuples. This gives us the option of + // Handle both by-ref and immediate tuples. match tuple.val { Ref(llval) => { let base_repr = adt::represent_type(bcx.ccx(), tuple.ty);