|
1 | | -use rustc::hir::def_id::DefId; |
2 | | -use rustc::middle::lang_items::DropInPlaceFnLangItem; |
3 | 1 | use rustc::traits; |
4 | 2 | use rustc::ty::adjustment::CustomCoerceUnsized; |
5 | 3 | use rustc::ty::{self, Ty, TyCtxt, Instance}; |
@@ -54,89 +52,6 @@ pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mon |
54 | 52 | } |
55 | 53 | } |
56 | 54 |
|
57 | | -fn fn_once_adapter_instance<'a, 'tcx>( |
58 | | - tcx: TyCtxt<'a, 'tcx, 'tcx>, |
59 | | - closure_did: DefId, |
60 | | - substs: ty::ClosureSubsts<'tcx>, |
61 | | - ) -> Instance<'tcx> { |
62 | | - debug!("fn_once_adapter_shim({:?}, {:?})", |
63 | | - closure_did, |
64 | | - substs); |
65 | | - let fn_once = tcx.lang_items().fn_once_trait().unwrap(); |
66 | | - let call_once = tcx.associated_items(fn_once) |
67 | | - .find(|it| it.kind == ty::AssocKind::Method) |
68 | | - .unwrap().def_id; |
69 | | - let def = ty::InstanceDef::ClosureOnceShim { call_once }; |
70 | | - |
71 | | - let self_ty = tcx.mk_closure(closure_did, substs); |
72 | | - |
73 | | - let sig = substs.closure_sig(closure_did, tcx); |
74 | | - let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig); |
75 | | - assert_eq!(sig.inputs().len(), 1); |
76 | | - let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]); |
77 | | - |
78 | | - debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig); |
79 | | - Instance { def, substs } |
80 | | -} |
81 | | - |
82 | | -fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind, |
83 | | - trait_closure_kind: ty::ClosureKind) |
84 | | - -> Result<bool, ()> |
85 | | -{ |
86 | | - match (actual_closure_kind, trait_closure_kind) { |
87 | | - (ty::ClosureKind::Fn, ty::ClosureKind::Fn) | |
88 | | - (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | |
89 | | - (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { |
90 | | - // No adapter needed. |
91 | | - Ok(false) |
92 | | - } |
93 | | - (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { |
94 | | - // The closure fn `llfn` is a `fn(&self, ...)`. We want a |
95 | | - // `fn(&mut self, ...)`. In fact, at codegen time, these are |
96 | | - // basically the same thing, so we can just return llfn. |
97 | | - Ok(false) |
98 | | - } |
99 | | - (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) | |
100 | | - (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { |
101 | | - // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut |
102 | | - // self, ...)`. We want a `fn(self, ...)`. We can produce |
103 | | - // this by doing something like: |
104 | | - // |
105 | | - // fn call_once(self, ...) { call_mut(&self, ...) } |
106 | | - // fn call_once(mut self, ...) { call_mut(&mut self, ...) } |
107 | | - // |
108 | | - // These are both the same at codegen time. |
109 | | - Ok(true) |
110 | | - } |
111 | | - _ => Err(()), |
112 | | - } |
113 | | -} |
114 | | - |
115 | | -pub fn resolve_closure<'a, 'tcx> ( |
116 | | - tcx: TyCtxt<'a, 'tcx, 'tcx>, |
117 | | - def_id: DefId, |
118 | | - substs: ty::ClosureSubsts<'tcx>, |
119 | | - requested_kind: ty::ClosureKind) |
120 | | - -> Instance<'tcx> |
121 | | -{ |
122 | | - let actual_kind = substs.closure_kind(def_id, tcx); |
123 | | - |
124 | | - match needs_fn_once_adapter_shim(actual_kind, requested_kind) { |
125 | | - Ok(true) => fn_once_adapter_instance(tcx, def_id, substs), |
126 | | - _ => Instance::new(def_id, substs.substs) |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -pub fn resolve_drop_in_place<'a, 'tcx>( |
131 | | - tcx: TyCtxt<'a, 'tcx, 'tcx>, |
132 | | - ty: Ty<'tcx>) |
133 | | - -> ty::Instance<'tcx> |
134 | | -{ |
135 | | - let def_id = tcx.require_lang_item(DropInPlaceFnLangItem); |
136 | | - let substs = tcx.intern_substs(&[ty.into()]); |
137 | | - Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap() |
138 | | -} |
139 | | - |
140 | 55 | pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
141 | 56 | source_ty: Ty<'tcx>, |
142 | 57 | target_ty: Ty<'tcx>) |
|
0 commit comments