|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use astconv::AstConv; |
| 12 | + |
| 13 | +use super::FnCtxt; |
| 14 | + |
| 15 | +use rustc::traits; |
| 16 | +use rustc::ty::{self, Ty, TraitRef}; |
| 17 | +use rustc::ty::{ToPredicate, TypeFoldable}; |
| 18 | +use rustc::ty::{MethodCall, MethodCallee}; |
| 19 | +use rustc::ty::subst::Substs; |
| 20 | +use rustc::ty::{LvaluePreference, NoPreference, PreferMutLvalue}; |
| 21 | +use rustc::hir; |
| 22 | + |
| 23 | +use syntax::codemap::Span; |
| 24 | +use syntax::parse::token; |
| 25 | + |
| 26 | +#[derive(Copy, Clone, Debug)] |
| 27 | +enum AutoderefKind { |
| 28 | + Builtin, |
| 29 | + Overloaded |
| 30 | +} |
| 31 | + |
| 32 | +pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> { |
| 33 | + fcx: &'a FnCtxt<'a, 'gcx, 'tcx>, |
| 34 | + steps: Vec<(Ty<'tcx>, AutoderefKind)>, |
| 35 | + cur_ty: Ty<'tcx>, |
| 36 | + obligations: Vec<traits::PredicateObligation<'tcx>>, |
| 37 | + at_start: bool, |
| 38 | + span: Span |
| 39 | +} |
| 40 | + |
| 41 | +impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> { |
| 42 | + type Item = (Ty<'tcx>, usize); |
| 43 | + |
| 44 | + fn next(&mut self) -> Option<Self::Item> { |
| 45 | + let tcx = self.fcx.tcx; |
| 46 | + |
| 47 | + debug!("autoderef: steps={:?}, cur_ty={:?}", |
| 48 | + self.steps, self.cur_ty); |
| 49 | + if self.at_start { |
| 50 | + self.at_start = false; |
| 51 | + debug!("autoderef stage #0 is {:?}", self.cur_ty); |
| 52 | + return Some((self.cur_ty, 0)); |
| 53 | + } |
| 54 | + |
| 55 | + if self.steps.len() == tcx.sess.recursion_limit.get() { |
| 56 | + // We've reached the recursion limit, error gracefully. |
| 57 | + span_err!(tcx.sess, self.span, E0055, |
| 58 | + "reached the recursion limit while auto-dereferencing {:?}", |
| 59 | + self.cur_ty); |
| 60 | + return None; |
| 61 | + } |
| 62 | + |
| 63 | + if self.cur_ty.is_ty_var() { |
| 64 | + return None; |
| 65 | + } |
| 66 | + |
| 67 | + // Otherwise, deref if type is derefable: |
| 68 | + let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(false, NoPreference) { |
| 69 | + (AutoderefKind::Builtin, mt.ty) |
| 70 | + } else { |
| 71 | + match self.overloaded_deref_ty(self.cur_ty) { |
| 72 | + Some(ty) => (AutoderefKind::Overloaded, ty), |
| 73 | + _ => return None |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + if new_ty.references_error() { |
| 78 | + return None; |
| 79 | + } |
| 80 | + |
| 81 | + self.steps.push((self.cur_ty, kind)); |
| 82 | + debug!("autoderef stage #{:?} is {:?} from {:?}", self.steps.len(), |
| 83 | + new_ty, (self.cur_ty, kind)); |
| 84 | + self.cur_ty = new_ty; |
| 85 | + |
| 86 | + Some((self.cur_ty, self.steps.len())) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> { |
| 91 | + fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> { |
| 92 | + debug!("overloaded_deref_ty({:?})", ty); |
| 93 | + |
| 94 | + let tcx = self.fcx.tcx(); |
| 95 | + |
| 96 | + // <cur_ty as Deref> |
| 97 | + let trait_ref = TraitRef { |
| 98 | + def_id: match tcx.lang_items.deref_trait() { |
| 99 | + Some(f) => f, |
| 100 | + None => return None |
| 101 | + }, |
| 102 | + substs: tcx.mk_substs(Substs::new_trait(vec![], vec![], self.cur_ty)) |
| 103 | + }; |
| 104 | + |
| 105 | + let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id); |
| 106 | + |
| 107 | + let mut selcx = traits::SelectionContext::new(self.fcx); |
| 108 | + let obligation = traits::Obligation::new(cause.clone(), trait_ref.to_predicate()); |
| 109 | + if !selcx.evaluate_obligation(&obligation) { |
| 110 | + debug!("overloaded_deref_ty: cannot match obligation"); |
| 111 | + return None; |
| 112 | + } |
| 113 | + |
| 114 | + let normalized = traits::normalize_projection_type( |
| 115 | + &mut selcx, |
| 116 | + ty::ProjectionTy { |
| 117 | + trait_ref: trait_ref, |
| 118 | + item_name: token::intern("Target") |
| 119 | + }, |
| 120 | + cause, |
| 121 | + 0 |
| 122 | + ); |
| 123 | + |
| 124 | + debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized); |
| 125 | + self.obligations.extend(normalized.obligations); |
| 126 | + |
| 127 | + Some(self.fcx.resolve_type_vars_if_possible(&normalized.value)) |
| 128 | + } |
| 129 | + |
| 130 | + pub fn unambiguous_final_ty(&self) -> Ty<'tcx> { |
| 131 | + self.fcx.structurally_resolved_type(self.span, self.cur_ty) |
| 132 | + } |
| 133 | + |
| 134 | + pub fn finalize<'b, I>(self, pref: LvaluePreference, exprs: I) |
| 135 | + where I: IntoIterator<Item=&'b hir::Expr> |
| 136 | + { |
| 137 | + let methods : Vec<_> = self.steps.iter().map(|&(ty, kind)| { |
| 138 | + if let AutoderefKind::Overloaded = kind { |
| 139 | + self.fcx.try_overloaded_deref(self.span, None, ty, pref) |
| 140 | + } else { |
| 141 | + None |
| 142 | + } |
| 143 | + }).collect(); |
| 144 | + |
| 145 | + debug!("finalize({:?}) - {:?},{:?}", pref, methods, self.obligations); |
| 146 | + |
| 147 | + for expr in exprs { |
| 148 | + debug!("finalize - finalizing #{} - {:?}", expr.id, expr); |
| 149 | + for (n, method) in methods.iter().enumerate() { |
| 150 | + if let &Some(method) = method { |
| 151 | + let method_call = MethodCall::autoderef(expr.id, n as u32); |
| 152 | + self.fcx.tables.borrow_mut().method_map.insert(method_call, method); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + for obligation in self.obligations { |
| 158 | + self.fcx.register_predicate(obligation); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { |
| 164 | + pub fn autoderef(&'a self, |
| 165 | + span: Span, |
| 166 | + base_ty: Ty<'tcx>) |
| 167 | + -> Autoderef<'a, 'gcx, 'tcx> |
| 168 | + { |
| 169 | + Autoderef { |
| 170 | + fcx: self, |
| 171 | + steps: vec![], |
| 172 | + cur_ty: self.resolve_type_vars_if_possible(&base_ty), |
| 173 | + obligations: vec![], |
| 174 | + at_start: true, |
| 175 | + span: span |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + pub fn try_overloaded_deref(&self, |
| 180 | + span: Span, |
| 181 | + base_expr: Option<&hir::Expr>, |
| 182 | + base_ty: Ty<'tcx>, |
| 183 | + lvalue_pref: LvaluePreference) |
| 184 | + -> Option<MethodCallee<'tcx>> |
| 185 | + { |
| 186 | + debug!("try_overloaded_deref({:?},{:?},{:?},{:?})", |
| 187 | + span, base_expr, base_ty, lvalue_pref); |
| 188 | + // Try DerefMut first, if preferred. |
| 189 | + let method = match (lvalue_pref, self.tcx.lang_items.deref_mut_trait()) { |
| 190 | + (PreferMutLvalue, Some(trait_did)) => { |
| 191 | + self.lookup_method_in_trait(span, base_expr, |
| 192 | + token::intern("deref_mut"), trait_did, |
| 193 | + base_ty, None) |
| 194 | + } |
| 195 | + _ => None |
| 196 | + }; |
| 197 | + |
| 198 | + // Otherwise, fall back to Deref. |
| 199 | + let method = match (method, self.tcx.lang_items.deref_trait()) { |
| 200 | + (None, Some(trait_did)) => { |
| 201 | + self.lookup_method_in_trait(span, base_expr, |
| 202 | + token::intern("deref"), trait_did, |
| 203 | + base_ty, None) |
| 204 | + } |
| 205 | + (method, _) => method |
| 206 | + }; |
| 207 | + |
| 208 | + method |
| 209 | + } |
| 210 | +} |
0 commit comments