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.
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.
16 use rustc
::ty
::{self, Ty, TraitRef}
;
17 use rustc
::ty
::{ToPredicate, TypeFoldable}
;
18 use rustc
::ty
::{MethodCall, MethodCallee}
;
19 use rustc
::ty
::{LvaluePreference, NoPreference, PreferMutLvalue}
;
23 use syntax
::symbol
::Symbol
;
25 #[derive(Copy, Clone, Debug)]
31 pub struct Autoderef
<'a
, 'gcx
: 'tcx
, 'tcx
: 'a
> {
32 fcx
: &'a FnCtxt
<'a
, 'gcx
, 'tcx
>,
33 steps
: Vec
<(Ty
<'tcx
>, AutoderefKind
)>,
35 obligations
: Vec
<traits
::PredicateObligation
<'tcx
>>,
40 impl<'a
, 'gcx
, 'tcx
> Iterator
for Autoderef
<'a
, 'gcx
, 'tcx
> {
41 type Item
= (Ty
<'tcx
>, usize);
43 fn next(&mut self) -> Option
<Self::Item
> {
44 let tcx
= self.fcx
.tcx
;
46 debug
!("autoderef: steps={:?}, cur_ty={:?}",
50 self.at_start
= false;
51 debug
!("autoderef stage #0 is {:?}", self.cur_ty
);
52 return Some((self.cur_ty
, 0));
55 if self.steps
.len() == tcx
.sess
.recursion_limit
.get() {
56 // We've reached the recursion limit, error gracefully.
57 struct_span_err
!(tcx
.sess
,
60 "reached the recursion limit while auto-dereferencing {:?}",
62 .span_label(self.span
, &format
!("deref recursion limit reached"))
67 if self.cur_ty
.is_ty_var() {
71 // Otherwise, deref if type is derefable:
72 let (kind
, new_ty
) = if let Some(mt
) = self.cur_ty
.builtin_deref(false, NoPreference
) {
73 (AutoderefKind
::Builtin
, mt
.ty
)
75 match self.overloaded_deref_ty(self.cur_ty
) {
76 Some(ty
) => (AutoderefKind
::Overloaded
, ty
),
81 if new_ty
.references_error() {
85 self.steps
.push((self.cur_ty
, kind
));
86 debug
!("autoderef stage #{:?} is {:?} from {:?}",
92 Some((self.cur_ty
, self.steps
.len()))
96 impl<'a
, 'gcx
, 'tcx
> Autoderef
<'a
, 'gcx
, 'tcx
> {
97 fn overloaded_deref_ty(&mut self, ty
: Ty
<'tcx
>) -> Option
<Ty
<'tcx
>> {
98 debug
!("overloaded_deref_ty({:?})", ty
);
100 let tcx
= self.fcx
.tcx();
103 let trait_ref
= TraitRef
{
104 def_id
: match tcx
.lang_items
.deref_trait() {
108 substs
: tcx
.mk_substs_trait(self.cur_ty
, &[]),
111 let cause
= traits
::ObligationCause
::misc(self.span
, self.fcx
.body_id
);
113 let mut selcx
= traits
::SelectionContext
::new(self.fcx
);
114 let obligation
= traits
::Obligation
::new(cause
.clone(), trait_ref
.to_predicate());
115 if !selcx
.evaluate_obligation(&obligation
) {
116 debug
!("overloaded_deref_ty: cannot match obligation");
120 let normalized
= traits
::normalize_projection_type(&mut selcx
,
122 trait_ref
: trait_ref
,
123 item_name
: Symbol
::intern("Target"),
128 debug
!("overloaded_deref_ty({:?}) = {:?}", ty
, normalized
);
129 self.obligations
.extend(normalized
.obligations
);
131 Some(self.fcx
.resolve_type_vars_if_possible(&normalized
.value
))
134 pub fn unambiguous_final_ty(&self) -> Ty
<'tcx
> {
135 self.fcx
.structurally_resolved_type(self.span
, self.cur_ty
)
138 pub fn finalize
<'b
, I
>(self, pref
: LvaluePreference
, exprs
: I
)
139 where I
: IntoIterator
<Item
= &'b hir
::Expr
>
141 let methods
: Vec
<_
> = self.steps
144 if let AutoderefKind
::Overloaded
= kind
{
145 self.fcx
.try_overloaded_deref(self.span
, None
, ty
, pref
)
152 debug
!("finalize({:?}) - {:?},{:?}",
158 debug
!("finalize - finalizing #{} - {:?}", expr
.id
, expr
);
159 for (n
, method
) in methods
.iter().enumerate() {
160 if let &Some(method
) = method
{
161 let method_call
= MethodCall
::autoderef(expr
.id
, n
as u32);
162 self.fcx
.tables
.borrow_mut().method_map
.insert(method_call
, method
);
167 for obligation
in self.obligations
{
168 self.fcx
.register_predicate(obligation
);
173 impl<'a
, 'gcx
, 'tcx
> FnCtxt
<'a
, 'gcx
, 'tcx
> {
174 pub fn autoderef(&'a
self, span
: Span
, base_ty
: Ty
<'tcx
>) -> Autoderef
<'a
, 'gcx
, 'tcx
> {
178 cur_ty
: self.resolve_type_vars_if_possible(&base_ty
),
185 pub fn try_overloaded_deref(&self,
187 base_expr
: Option
<&hir
::Expr
>,
189 lvalue_pref
: LvaluePreference
)
190 -> Option
<MethodCallee
<'tcx
>> {
191 debug
!("try_overloaded_deref({:?},{:?},{:?},{:?})",
196 // Try DerefMut first, if preferred.
197 let method
= match (lvalue_pref
, self.tcx
.lang_items
.deref_mut_trait()) {
198 (PreferMutLvalue
, Some(trait_did
)) => {
199 self.lookup_method_in_trait(span
,
201 Symbol
::intern("deref_mut"),
209 // Otherwise, fall back to Deref.
210 let method
= match (method
, self.tcx
.lang_items
.deref_trait()) {
211 (None
, Some(trait_did
)) => {
212 self.lookup_method_in_trait(span
,
214 Symbol
::intern("deref"),
219 (method
, _
) => method
,