From 6e290998c3d8b2caf25715c932d416fb0104472e Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 11 Mar 2016 02:33:20 +0200 Subject: [PATCH] infer: Turn normalize_associated_type into a method on TyCtxt. --- src/librustc/infer/mod.rs | 54 +++++++++++----------- src/librustc_lint/types.rs | 7 ++- src/librustc_trans/base.rs | 7 ++- src/librustc_trans/callee.rs | 9 ++-- src/librustc_trans/closure.rs | 8 ++-- src/librustc_trans/debuginfo/metadata.rs | 3 +- src/librustc_trans/debuginfo/mod.rs | 5 +- src/librustc_trans/debuginfo/type_names.rs | 3 +- src/librustc_trans/declare.rs | 3 +- src/librustc_trans/intrinsic.rs | 5 +- src/librustc_trans/meth.rs | 4 +- src/librustc_trans/monomorphize.rs | 5 +- src/librustc_trans/type_of.rs | 4 +- 13 files changed, 55 insertions(+), 62 deletions(-) diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 2ea7952f598..407278c283f 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -523,34 +523,36 @@ pub struct CombinedSnapshot { } // NOTE: Callable from trans only! -pub fn normalize_associated_type<'tcx,T>(tcx: &TyCtxt<'tcx>, value: &T) -> T - where T : TypeFoldable<'tcx> -{ - debug!("normalize_associated_type(t={:?})", value); +impl<'tcx> TyCtxt<'tcx> { + pub fn normalize_associated_type(&self, value: &T) -> T + where T : TypeFoldable<'tcx> + { + debug!("normalize_associated_type(t={:?})", value); - let value = tcx.erase_regions(value); + let value = self.erase_regions(value); - if !value.has_projection_types() { - return value; + if !value.has_projection_types() { + return value; + } + + let infcx = InferCtxt::new(self, &self.tables, None, ProjectionMode::Any); + let mut selcx = traits::SelectionContext::new(&infcx); + let cause = traits::ObligationCause::dummy(); + let traits::Normalized { value: result, obligations } = + traits::normalize(&mut selcx, cause, &value); + + debug!("normalize_associated_type: result={:?} obligations={:?}", + result, + obligations); + + let mut fulfill_cx = traits::FulfillmentContext::new(); + + for obligation in obligations { + fulfill_cx.register_predicate_obligation(&infcx, obligation); + } + + drain_fulfillment_cx_or_panic(DUMMY_SP, &infcx, &mut fulfill_cx, &result) } - - let infcx = InferCtxt::new(tcx, &tcx.tables, None, ProjectionMode::Any); - let mut selcx = traits::SelectionContext::new(&infcx); - let cause = traits::ObligationCause::dummy(); - let traits::Normalized { value: result, obligations } = - traits::normalize(&mut selcx, cause, &value); - - debug!("normalize_associated_type: result={:?} obligations={:?}", - result, - obligations); - - let mut fulfill_cx = traits::FulfillmentContext::new(); - - for obligation in obligations { - fulfill_cx.register_predicate_obligation(&infcx, obligation); - } - - drain_fulfillment_cx_or_panic(DUMMY_SP, &infcx, &mut fulfill_cx, &result) } pub fn drain_fulfillment_cx_or_panic<'a,'tcx,T>(span: Span, @@ -1617,7 +1619,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { substs); if self.normalize { - normalize_associated_type(&self.tcx, &closure_ty) + self.tcx.normalize_associated_type(&closure_ty) } else { closure_ty } diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index b81a4a0efe7..63dc0f303ff 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -11,7 +11,6 @@ #![allow(non_snake_case)] use rustc::hir::def_id::DefId; -use rustc::infer; use rustc::ty::subst::Substs; use rustc::ty::{self, Ty, TyCtxt}; use middle::const_val::ConstVal; @@ -439,7 +438,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { } for field in &def.struct_variant().fields { - let field_ty = infer::normalize_associated_type(cx, &field.ty(cx, substs)); + let field_ty = cx.normalize_associated_type(&field.ty(cx, substs)); let r = self.check_type_for_ffi(cache, field_ty); match r { FfiSafe => {} @@ -494,7 +493,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { // Check the contained variants. for variant in &def.variants { for field in &variant.fields { - let arg = infer::normalize_associated_type(cx, &field.ty(cx, substs)); + let arg = cx.normalize_associated_type(&field.ty(cx, substs)); let r = self.check_type_for_ffi(cache, arg); match r { FfiSafe => {} @@ -596,7 +595,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { fn check_type_for_ffi_and_report_errors(&mut self, sp: Span, ty: Ty<'tcx>) { // it is only OK to use this function because extern fns cannot have // any generic types right now: - let ty = infer::normalize_associated_type(self.cx.tcx, &ty); + let ty = self.cx.tcx.normalize_associated_type(&ty); match self.check_type_for_ffi(&mut FnvHashSet(), ty) { FfiResult::FfiSafe => {} diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index 191b14f8a41..f095b60ac48 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -36,7 +36,6 @@ use llvm::{BasicBlockRef, Linkage, ValueRef, Vector, get_param}; use llvm; use rustc::cfg; use rustc::hir::def_id::DefId; -use rustc::infer; use middle::lang_items::{LangItem, ExchangeMallocFnLangItem, StartFnLangItem}; use middle::weak_lang_items; use rustc::hir::pat_util::simple_name; @@ -1923,7 +1922,7 @@ pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let fn_ty = ccx.tcx().lookup_item_type(def_id).ty; let fn_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fn_ty); let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig()); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let abi = fn_ty.fn_abi(); trans_closure(ccx, decl, @@ -1947,7 +1946,7 @@ pub fn trans_named_tuple_constructor<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let ccx = bcx.fcx.ccx; let sig = ccx.tcx().erase_late_bound_regions(&ctor_ty.fn_sig()); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let result_ty = sig.output.unwrap(); // Get location to store the result. If the user does not care about @@ -2017,7 +2016,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let ctor_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &ctor_ty); let sig = ccx.tcx().erase_late_bound_regions(&ctor_ty.fn_sig()); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]); let (arena, fcx): (TypedArena<_>, FunctionContext); diff --git a/src/librustc_trans/callee.rs b/src/librustc_trans/callee.rs index db605e4dc5d..656fac54a82 100644 --- a/src/librustc_trans/callee.rs +++ b/src/librustc_trans/callee.rs @@ -22,7 +22,6 @@ use back::symbol_names; use llvm::{self, ValueRef, get_params}; use middle::cstore::LOCAL_CRATE; use rustc::hir::def_id::DefId; -use rustc::infer; use rustc::ty::subst; use rustc::traits; use rustc::hir::map as hir_map; @@ -221,7 +220,7 @@ impl<'tcx> Callee<'tcx> { extra_args: &[Ty<'tcx>]) -> FnType { let abi = self.ty.fn_abi(); let sig = ccx.tcx().erase_late_bound_regions(self.ty.fn_sig()); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let mut fn_ty = FnType::unadjusted(ccx, abi, &sig, extra_args); if let Virtual(_) = self.data { // Don't pass the vtable, it's not an argument of the virtual fn. @@ -361,7 +360,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( } }; let sig = tcx.erase_late_bound_regions(sig); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let tuple_input_ty = tcx.mk_tup(sig.inputs.to_vec()); let sig = ty::FnSig { inputs: vec![bare_fn_ty_maybe_ref, @@ -491,7 +490,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let fn_ptr_ty = match ty.sty { ty::TyFnDef(_, _, fty) => { // Create a fn pointer with the normalized signature. - tcx.mk_fn_ptr(infer::normalize_associated_type(tcx, fty)) + tcx.mk_fn_ptr(tcx.normalize_associated_type(fty)) } _ => bug!("expected fn item type, found {}", ty) }; @@ -623,7 +622,7 @@ fn trans_call_inner<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let abi = callee.ty.fn_abi(); let sig = callee.ty.fn_sig(); let output = bcx.tcx().erase_late_bound_regions(&sig.output()); - let output = infer::normalize_associated_type(bcx.tcx(), &output); + let output = bcx.tcx().normalize_associated_type(&output); let extra_args = match args { ArgExprs(args) if abi != Abi::RustCall => { diff --git a/src/librustc_trans/closure.rs b/src/librustc_trans/closure.rs index 0633dd95d59..86a91d261fc 100644 --- a/src/librustc_trans/closure.rs +++ b/src/librustc_trans/closure.rs @@ -12,7 +12,7 @@ use arena::TypedArena; use back::symbol_names; use llvm::{ValueRef, get_param, get_params}; use rustc::hir::def_id::DefId; -use rustc::infer::{self, InferCtxt}; +use rustc::infer::InferCtxt; use rustc::traits::ProjectionMode; use abi::{Abi, FnType}; use adt; @@ -158,7 +158,7 @@ fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let infcx = InferCtxt::normalizing(tcx, &tcx.tables, ProjectionMode::Any); let sig = &infcx.closure_type(closure_id, &substs).sig; let sig = tcx.erase_late_bound_regions(sig); - let sig = infer::normalize_associated_type(tcx, &sig); + let sig = tcx.normalize_associated_type(&sig); let closure_type = tcx.mk_closure_from_closure_substs(closure_id, Box::new(substs)); let function_type = tcx.mk_fn_ptr(ty::BareFnTy { unsafety: hir::Unsafety::Normal, @@ -224,7 +224,7 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>, let function_type = infcx.closure_type(closure_def_id, closure_substs); let sig = tcx.erase_late_bound_regions(&function_type.sig); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let closure_type = tcx.mk_closure_from_closure_substs(closure_def_id, Box::new(closure_substs.clone())); @@ -369,7 +369,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>( sig.0.inputs[0] = closure_ty; let sig = tcx.erase_late_bound_regions(&sig); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); let fn_ty = FnType::new(ccx, abi, &sig, &[]); let llonce_fn_ty = tcx.mk_fn_ptr(ty::BareFnTy { diff --git a/src/librustc_trans/debuginfo/metadata.rs b/src/librustc_trans/debuginfo/metadata.rs index cc20751a0b0..55b5bc2beb8 100644 --- a/src/librustc_trans/debuginfo/metadata.rs +++ b/src/librustc_trans/debuginfo/metadata.rs @@ -24,7 +24,6 @@ use llvm::{self, ValueRef}; use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor, DICompositeType}; use rustc::hir::def_id::DefId; -use rustc::infer; use rustc::hir::pat_util; use rustc::ty::subst; use rustc::hir::map as hir_map; @@ -263,7 +262,7 @@ impl<'tcx> TypeMap<'tcx> { unique_type_id.push_str(" fn("); let sig = cx.tcx().erase_late_bound_regions(sig); - let sig = infer::normalize_associated_type(cx.tcx(), &sig); + let sig = cx.tcx().normalize_associated_type(&sig); for ¶meter_type in &sig.inputs { let parameter_type_id = diff --git a/src/librustc_trans/debuginfo/mod.rs b/src/librustc_trans/debuginfo/mod.rs index dac593b7509..f1ac89ebf52 100644 --- a/src/librustc_trans/debuginfo/mod.rs +++ b/src/librustc_trans/debuginfo/mod.rs @@ -34,7 +34,6 @@ use rustc::hir; use abi::Abi; use common::{NodeIdAndSpan, CrateContext, FunctionContext, Block, BlockAndBuilder}; use monomorphize::Instance; -use rustc::infer::normalize_associated_type; use rustc::ty::{self, Ty}; use session::config::{self, FullDebugInfo, LimitedDebugInfo, NoDebugInfo}; use util::nodemap::{DefIdMap, NodeMap, FnvHashMap, FnvHashSet}; @@ -369,7 +368,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, name_to_append_suffix_to.push('<'); for (i, &actual_type) in actual_types.iter().enumerate() { - let actual_type = normalize_associated_type(cx.tcx(), &actual_type); + let actual_type = cx.tcx().normalize_associated_type(&actual_type); // Add actual type name to <...> clause of function name let actual_type_name = compute_debuginfo_type_name(cx, actual_type, @@ -385,7 +384,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, // Again, only create type information if full debuginfo is enabled let template_params: Vec<_> = if cx.sess().opts.debuginfo == FullDebugInfo { generics.types.as_slice().iter().enumerate().map(|(i, param)| { - let actual_type = normalize_associated_type(cx.tcx(), &actual_types[i]); + let actual_type = cx.tcx().normalize_associated_type(&actual_types[i]); let actual_type_metadata = type_metadata(cx, actual_type, codemap::DUMMY_SP); let name = CString::new(param.name.as_str().as_bytes()).unwrap(); unsafe { diff --git a/src/librustc_trans/debuginfo/type_names.rs b/src/librustc_trans/debuginfo/type_names.rs index 6fdd6a2c1d1..3e0fc7b3120 100644 --- a/src/librustc_trans/debuginfo/type_names.rs +++ b/src/librustc_trans/debuginfo/type_names.rs @@ -12,7 +12,6 @@ use common::CrateContext; use rustc::hir::def_id::DefId; -use rustc::infer; use rustc::ty::subst; use rustc::ty::{self, Ty}; @@ -114,7 +113,7 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, output.push_str("fn("); let sig = cx.tcx().erase_late_bound_regions(sig); - let sig = infer::normalize_associated_type(cx.tcx(), &sig); + let sig = cx.tcx().normalize_associated_type(&sig); if !sig.inputs.is_empty() { for ¶meter_type in &sig.inputs { push_debuginfo_type_name(cx, parameter_type, true, output); diff --git a/src/librustc_trans/declare.rs b/src/librustc_trans/declare.rs index 9a4d20ca301..e6db695943b 100644 --- a/src/librustc_trans/declare.rs +++ b/src/librustc_trans/declare.rs @@ -21,7 +21,6 @@ //! * When in doubt, define. use llvm::{self, ValueRef}; use rustc::ty; -use rustc::infer; use abi::{Abi, FnType}; use attributes; use context::CrateContext; @@ -105,7 +104,7 @@ pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str, debug!("declare_rust_fn(name={:?}, fn_type={:?})", name, fn_type); let abi = fn_type.fn_abi(); let sig = ccx.tcx().erase_late_bound_regions(fn_type.fn_sig()); - let sig = infer::normalize_associated_type(ccx.tcx(), &sig); + let sig = ccx.tcx().normalize_associated_type(&sig); debug!("declare_rust_fn (after region erasure) sig={:?}", sig); let fty = FnType::new(ccx, abi, &sig, &[]); diff --git a/src/librustc_trans/intrinsic.rs b/src/librustc_trans/intrinsic.rs index e1d5a3f7ee1..04c7e872f0e 100644 --- a/src/librustc_trans/intrinsic.rs +++ b/src/librustc_trans/intrinsic.rs @@ -15,7 +15,6 @@ use intrinsics::{self, Intrinsic}; use libc; use llvm; use llvm::{ValueRef, TypeKind}; -use rustc::infer; use rustc::ty::subst; use rustc::ty::subst::FnSpace; use abi::{Abi, FnType}; @@ -114,7 +113,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let (def_id, substs, sig) = match callee_ty.sty { ty::TyFnDef(def_id, substs, fty) => { let sig = tcx.erase_late_bound_regions(&fty.sig); - (def_id, substs, infer::normalize_associated_type(tcx, &sig)) + (def_id, substs, tcx.normalize_associated_type(&sig)) } _ => bug!("expected fn item type, found {}", callee_ty) }; @@ -1352,7 +1351,7 @@ fn generic_simd_intrinsic<'blk, 'tcx, 'a> let tcx = bcx.tcx(); let sig = tcx.erase_late_bound_regions(callee_ty.fn_sig()); - let sig = infer::normalize_associated_type(tcx, &sig); + let sig = tcx.normalize_associated_type(&sig); let arg_tys = sig.inputs; // every intrinsic takes a SIMD vector as its first argument diff --git a/src/librustc_trans/meth.rs b/src/librustc_trans/meth.rs index c7606584cff..22177fe9451 100644 --- a/src/librustc_trans/meth.rs +++ b/src/librustc_trans/meth.rs @@ -14,7 +14,7 @@ use arena::TypedArena; use back::symbol_names; use llvm::{ValueRef, get_params}; use rustc::hir::def_id::DefId; -use rustc::infer::{self, InferCtxt}; +use rustc::infer::InferCtxt; use rustc::ty::subst::{FnSpace, Subst, Substs}; use rustc::ty::subst; use rustc::traits::{self, ProjectionMode}; @@ -86,7 +86,7 @@ pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>, method_ty); let sig = tcx.erase_late_bound_regions(&method_ty.fn_sig()); - let sig = infer::normalize_associated_type(tcx, &sig); + let sig = tcx.normalize_associated_type(&sig); let fn_ty = FnType::new(ccx, method_ty.fn_abi(), &sig, &[]); let function_name = diff --git a/src/librustc_trans/monomorphize.rs b/src/librustc_trans/monomorphize.rs index ef0da37f0b2..ee94d6fa48e 100644 --- a/src/librustc_trans/monomorphize.rs +++ b/src/librustc_trans/monomorphize.rs @@ -12,7 +12,6 @@ use back::symbol_names; use llvm::ValueRef; use llvm; use rustc::hir::def_id::DefId; -use rustc::infer::normalize_associated_type; use rustc::ty::subst; use rustc::ty::subst::{Subst, Substs}; use rustc::ty::{self, Ty, TypeFoldable, TyCtxt}; @@ -197,7 +196,7 @@ pub fn apply_param_substs<'tcx,T>(tcx: &TyCtxt<'tcx>, where T : TypeFoldable<'tcx> { let substituted = value.subst(tcx, param_substs); - normalize_associated_type(tcx, &substituted) + tcx.normalize_associated_type(&substituted) } @@ -207,5 +206,5 @@ pub fn field_ty<'tcx>(tcx: &TyCtxt<'tcx>, f: ty::FieldDef<'tcx>) -> Ty<'tcx> { - normalize_associated_type(tcx, &f.ty(tcx, param_substs)) + tcx.normalize_associated_type(&f.ty(tcx, param_substs)) } diff --git a/src/librustc_trans/type_of.rs b/src/librustc_trans/type_of.rs index 13780148e97..8b8d9984bc9 100644 --- a/src/librustc_trans/type_of.rs +++ b/src/librustc_trans/type_of.rs @@ -11,7 +11,7 @@ #![allow(non_camel_case_types)] use rustc::hir::def_id::DefId; -use rustc::infer::{self, InferCtxt}; +use rustc::infer::InferCtxt; use rustc::ty::subst; use abi::FnType; use adt; @@ -296,7 +296,7 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> ty::TyFnDef(..) => Type::nil(cx), ty::TyFnPtr(f) => { let sig = cx.tcx().erase_late_bound_regions(&f.sig); - let sig = infer::normalize_associated_type(cx.tcx(), &sig); + let sig = cx.tcx().normalize_associated_type(&sig); FnType::new(cx, f.abi, &sig, &[]).llvm_type(cx).ptr_to() } ty::TyTuple(ref tys) if tys.is_empty() => Type::nil(cx),