]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/resolve.rs
Auto merge of #59033 - GuillaumeGomez:duplicated-bounds, r=Dylan-DPC
[rust.git] / src / librustc / infer / resolve.rs
1 use super::{InferCtxt, FixupError, FixupResult, Span, type_variable::TypeVariableOrigin};
2 use crate::mir::interpret::ConstValue;
3 use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst, TypeFlags};
4 use crate::ty::fold::{TypeFolder, TypeVisitor};
5
6 ///////////////////////////////////////////////////////////////////////////
7 // OPPORTUNISTIC VAR RESOLVER
8
9 /// The opportunistic resolver can be used at any time. It simply replaces
10 /// type/const variables that have been unified with the things they have
11 /// been unified with (similar to `shallow_resolve`, but deep). This is
12 /// useful for printing messages etc but also required at various
13 /// points for correctness.
14 pub struct OpportunisticVarResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
15     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
16 }
17
18 impl<'a, 'gcx, 'tcx> OpportunisticVarResolver<'a, 'gcx, 'tcx> {
19     #[inline]
20     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
21         OpportunisticVarResolver { infcx }
22     }
23 }
24
25 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticVarResolver<'a, 'gcx, 'tcx> {
26     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
27         self.infcx.tcx
28     }
29
30     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
31         if !t.has_infer_types() {
32             t // micro-optimize -- if there is nothing in this type that this fold affects...
33         } else {
34             let t = self.infcx.shallow_resolve(t);
35             t.super_fold_with(self)
36         }
37     }
38
39     fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
40         if !ct.has_type_flags(TypeFlags::HAS_CT_INFER) {
41             ct // micro-optimize -- if there is nothing in this const that this fold affects...
42         } else {
43             let ct = self.infcx.shallow_resolve(ct);
44             ct.super_fold_with(self)
45         }
46     }
47 }
48
49 /// The opportunistic type and region resolver is similar to the
50 /// opportunistic type resolver, but also opportunistically resolves
51 /// regions. It is useful for canonicalization.
52 pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
53     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
54 }
55
56 impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
57     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
58         OpportunisticTypeAndRegionResolver { infcx }
59     }
60 }
61
62 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
63     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
64         self.infcx.tcx
65     }
66
67     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
68         if !t.needs_infer() {
69             t // micro-optimize -- if there is nothing in this type that this fold affects...
70         } else {
71             let t0 = self.infcx.shallow_resolve(t);
72             t0.super_fold_with(self)
73         }
74     }
75
76     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
77         match *r {
78             ty::ReVar(rid) =>
79                 self.infcx.borrow_region_constraints()
80                           .opportunistic_resolve_var(self.tcx(), rid),
81             _ =>
82                 r,
83         }
84     }
85
86     fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
87         if !ct.needs_infer() {
88             ct // micro-optimize -- if there is nothing in this const that this fold affects...
89         } else {
90             let c0 = self.infcx.shallow_resolve(ct);
91             c0.super_fold_with(self)
92         }
93     }
94 }
95
96 ///////////////////////////////////////////////////////////////////////////
97 // UNRESOLVED TYPE FINDER
98
99 /// The unresolved type **finder** walks a type searching for
100 /// type variables that don't yet have a value. The first unresolved type is stored.
101 /// It does not construct the fully resolved type (which might
102 /// involve some hashing and so forth).
103 pub struct UnresolvedTypeFinder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
104     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
105
106     /// Used to find the type parameter name and location for error reporting.
107     pub first_unresolved: Option<(Ty<'tcx>,Option<Span>)>,
108 }
109
110 impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
111     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
112         UnresolvedTypeFinder { infcx, first_unresolved: None }
113     }
114 }
115
116 impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
117     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
118         let t = self.infcx.shallow_resolve(t);
119         if t.has_infer_types() {
120             if let ty::Infer(infer_ty) = t.sty {
121                 // Since we called `shallow_resolve` above, this must
122                 // be an (as yet...) unresolved inference variable.
123                 let ty_var_span =
124                 if let ty::TyVar(ty_vid) = infer_ty {
125                     let ty_vars = self.infcx.type_variables.borrow();
126                     if let TypeVariableOrigin::TypeParameterDefinition(span, _name)
127                         = *ty_vars.var_origin(ty_vid)
128                     {
129                         Some(span)
130                     } else {
131                         None
132                     }
133                 } else {
134                     None
135                 };
136                 self.first_unresolved = Some((t, ty_var_span));
137                 true  // Halt visiting.
138             } else {
139                 // Otherwise, visit its contents.
140                 t.super_visit_with(self)
141             }
142         } else {
143             // All type variables in inference types must already be resolved,
144             // - no need to visit the contents, continue visiting.
145             false
146         }
147     }
148 }
149
150
151 ///////////////////////////////////////////////////////////////////////////
152 // FULL TYPE RESOLUTION
153
154 /// Full type resolution replaces all type and region variables with
155 /// their concrete results. If any variable cannot be replaced (never unified, etc)
156 /// then an `Err` result is returned.
157 pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
158                                         value: &T) -> FixupResult<'tcx, T>
159     where T : TypeFoldable<'tcx>
160 {
161     let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
162     let result = value.fold_with(&mut full_resolver);
163     match full_resolver.err {
164         None => Ok(result),
165         Some(e) => Err(e),
166     }
167 }
168
169 // N.B. This type is not public because the protocol around checking the
170 // `err` field is not enforcable otherwise.
171 struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
172     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
173     err: Option<FixupError<'tcx>>,
174 }
175
176 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
177     fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
178         self.infcx.tcx
179     }
180
181     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
182         if !t.needs_infer() && !ty::keep_local(&t) {
183             t // micro-optimize -- if there is nothing in this type that this fold affects...
184               // ^ we need to have the `keep_local` check to un-default
185               // defaulted tuples.
186         } else {
187             let t = self.infcx.shallow_resolve(t);
188             match t.sty {
189                 ty::Infer(ty::TyVar(vid)) => {
190                     self.err = Some(FixupError::UnresolvedTy(vid));
191                     self.tcx().types.err
192                 }
193                 ty::Infer(ty::IntVar(vid)) => {
194                     self.err = Some(FixupError::UnresolvedIntTy(vid));
195                     self.tcx().types.err
196                 }
197                 ty::Infer(ty::FloatVar(vid)) => {
198                     self.err = Some(FixupError::UnresolvedFloatTy(vid));
199                     self.tcx().types.err
200                 }
201                 ty::Infer(_) => {
202                     bug!("Unexpected type in full type resolver: {:?}", t);
203                 }
204                 _ => {
205                     t.super_fold_with(self)
206                 }
207             }
208         }
209     }
210
211     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
212         match *r {
213             ty::ReVar(rid) => self.infcx.lexical_region_resolutions
214                                         .borrow()
215                                         .as_ref()
216                                         .expect("region resolution not performed")
217                                         .resolve_var(rid),
218             _ => r,
219         }
220     }
221
222     fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
223         if !c.needs_infer() && !ty::keep_local(&c) {
224             c // micro-optimize -- if there is nothing in this const that this fold affects...
225               // ^ we need to have the `keep_local` check to un-default
226               // defaulted tuples.
227         } else {
228             let c = self.infcx.shallow_resolve(c);
229             match c.val {
230                 ConstValue::Infer(InferConst::Var(vid)) => {
231                     self.err = Some(FixupError::UnresolvedConst(vid));
232                     return self.tcx().consts.err;
233                 }
234                 ConstValue::Infer(InferConst::Fresh(_)) => {
235                     bug!("Unexpected const in full const resolver: {:?}", c);
236                 }
237                 _ => {}
238             }
239             c.super_fold_with(self)
240         }
241     }
242 }