]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_infer/src/infer/resolve.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / compiler / rustc_infer / src / infer / resolve.rs
CommitLineData
dc9dc135 1use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
dfeec247 2use super::{FixupError, FixupResult, InferCtxt, Span};
cdc7bbd5 3use rustc_middle::mir;
ba9703b0
XL
4use rustc_middle::ty::fold::{TypeFolder, TypeVisitor};
5use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
1a4d82fc 6
29967ef6
XL
7use std::ops::ControlFlow;
8
1a4d82fc 9///////////////////////////////////////////////////////////////////////////
dc9dc135 10// OPPORTUNISTIC VAR RESOLVER
1a4d82fc 11
dc9dc135
XL
12/// The opportunistic resolver can be used at any time. It simply replaces
13/// type/const variables that have been unified with the things they have
1a4d82fc
JJ
14/// been unified with (similar to `shallow_resolve`, but deep). This is
15/// useful for printing messages etc but also required at various
16/// points for correctness.
dc9dc135
XL
17pub struct OpportunisticVarResolver<'a, 'tcx> {
18 infcx: &'a InferCtxt<'a, 'tcx>,
1a4d82fc
JJ
19}
20
dc9dc135 21impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
a1dfa0c6 22 #[inline]
dc9dc135
XL
23 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
24 OpportunisticVarResolver { infcx }
1a4d82fc
JJ
25 }
26}
27
dc9dc135
XL
28impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
29 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1a4d82fc
JJ
30 self.infcx.tcx
31 }
32
33 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
74b04a01 34 if !t.has_infer_types_or_consts() {
1a4d82fc
JJ
35 t // micro-optimize -- if there is nothing in this type that this fold affects...
36 } else {
dc9dc135
XL
37 let t = self.infcx.shallow_resolve(t);
38 t.super_fold_with(self)
39 }
40 }
41
42 fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
74b04a01 43 if !ct.has_infer_types_or_consts() {
dc9dc135
XL
44 ct // micro-optimize -- if there is nothing in this const that this fold affects...
45 } else {
46 let ct = self.infcx.shallow_resolve(ct);
47 ct.super_fold_with(self)
9cc50fc6
SL
48 }
49 }
cdc7bbd5
XL
50
51 fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
52 constant.super_fold_with(self)
53 }
9cc50fc6
SL
54}
55
f035d41b
XL
56/// The opportunistic region resolver opportunistically resolves regions
57/// variables to the variable with the least variable id. It is used when
58/// normlizing projections to avoid hitting the recursion limit by creating
59/// many versions of a predicate for types that in the end have to unify.
60///
61/// If you want to resolve type and const variables as well, call
62/// [InferCtxt::resolve_vars_if_possible] first.
63pub struct OpportunisticRegionResolver<'a, 'tcx> {
dc9dc135 64 infcx: &'a InferCtxt<'a, 'tcx>,
9cc50fc6
SL
65}
66
f035d41b 67impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> {
dc9dc135 68 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
f035d41b 69 OpportunisticRegionResolver { infcx }
9cc50fc6
SL
70 }
71}
72
f035d41b 73impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
dc9dc135 74 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
9cc50fc6
SL
75 self.infcx.tcx
76 }
77
78 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
f035d41b 79 if !t.has_infer_regions() {
9cc50fc6
SL
80 t // micro-optimize -- if there is nothing in this type that this fold affects...
81 } else {
f035d41b 82 t.super_fold_with(self)
9cc50fc6
SL
83 }
84 }
85
7cac9316 86 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
9e0c209e 87 match *r {
f035d41b
XL
88 ty::ReVar(rid) => {
89 let resolved = self
90 .infcx
91 .inner
92 .borrow_mut()
93 .unwrap_region_constraints()
94 .opportunistic_resolve_var(rid);
95 self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
96 }
dfeec247 97 _ => r,
1a4d82fc
JJ
98 }
99 }
48663c56
XL
100
101 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
f035d41b 102 if !ct.has_infer_regions() {
48663c56
XL
103 ct // micro-optimize -- if there is nothing in this const that this fold affects...
104 } else {
f035d41b 105 ct.super_fold_with(self)
48663c56
XL
106 }
107 }
1a4d82fc
JJ
108}
109
3b2f2976
XL
110///////////////////////////////////////////////////////////////////////////
111// UNRESOLVED TYPE FINDER
112
48663c56
XL
113/// The unresolved type **finder** walks a type searching for
114/// type variables that don't yet have a value. The first unresolved type is stored.
115/// It does not construct the fully resolved type (which might
3b2f2976 116/// involve some hashing and so forth).
dc9dc135
XL
117pub struct UnresolvedTypeFinder<'a, 'tcx> {
118 infcx: &'a InferCtxt<'a, 'tcx>,
3b2f2976
XL
119}
120
dc9dc135
XL
121impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
122 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
fc512014 123 UnresolvedTypeFinder { infcx }
3b2f2976
XL
124 }
125}
126
dc9dc135 127impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
fc512014 128 type BreakTy = (Ty<'tcx>, Option<Span>);
94222f64
XL
129
130 fn tcx_for_anon_const_substs(&self) -> Option<TyCtxt<'tcx>> {
131 Some(self.infcx.tcx)
132 }
133
fc512014 134 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
3b2f2976
XL
135 let t = self.infcx.shallow_resolve(t);
136 if t.has_infer_types() {
1b1a35ee 137 if let ty::Infer(infer_ty) = *t.kind() {
3b2f2976
XL
138 // Since we called `shallow_resolve` above, this must
139 // be an (as yet...) unresolved inference variable.
dfeec247 140 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
f9f354fc
XL
141 let mut inner = self.infcx.inner.borrow_mut();
142 let ty_vars = &inner.type_variables();
dc9dc135 143 if let TypeVariableOrigin {
dfeec247 144 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
dc9dc135
XL
145 span,
146 } = *ty_vars.var_origin(ty_vid)
48663c56
XL
147 {
148 Some(span)
149 } else {
150 None
151 }
152 } else {
153 None
154 };
fc512014 155 ControlFlow::Break((t, ty_var_span))
3b2f2976
XL
156 } else {
157 // Otherwise, visit its contents.
158 t.super_visit_with(self)
159 }
160 } else {
48663c56
XL
161 // All type variables in inference types must already be resolved,
162 // - no need to visit the contents, continue visiting.
29967ef6 163 ControlFlow::CONTINUE
3b2f2976
XL
164 }
165 }
166}
167
1a4d82fc
JJ
168///////////////////////////////////////////////////////////////////////////
169// FULL TYPE RESOLUTION
170
171/// Full type resolution replaces all type and region variables with
172/// their concrete results. If any variable cannot be replaced (never unified, etc)
173/// then an `Err` result is returned.
fc512014 174pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> FixupResult<'tcx, T>
dc9dc135
XL
175where
176 T: TypeFoldable<'tcx>,
1a4d82fc 177{
74b04a01 178 let mut full_resolver = FullTypeResolver { infcx, err: None };
1a4d82fc
JJ
179 let result = value.fold_with(&mut full_resolver);
180 match full_resolver.err {
181 None => Ok(result),
182 Some(e) => Err(e),
183 }
184}
185
186// N.B. This type is not public because the protocol around checking the
74b04a01 187// `err` field is not enforceable otherwise.
dc9dc135
XL
188struct FullTypeResolver<'a, 'tcx> {
189 infcx: &'a InferCtxt<'a, 'tcx>,
48663c56 190 err: Option<FixupError<'tcx>>,
1a4d82fc
JJ
191}
192
dc9dc135
XL
193impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
194 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1a4d82fc
JJ
195 self.infcx.tcx
196 }
197
198 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
ba9703b0 199 if !t.needs_infer() {
1a4d82fc
JJ
200 t // micro-optimize -- if there is nothing in this type that this fold affects...
201 } else {
202 let t = self.infcx.shallow_resolve(t);
1b1a35ee 203 match *t.kind() {
b7449926 204 ty::Infer(ty::TyVar(vid)) => {
c1a9b12d 205 self.err = Some(FixupError::UnresolvedTy(vid));
f035d41b 206 self.tcx().ty_error()
1a4d82fc 207 }
b7449926 208 ty::Infer(ty::IntVar(vid)) => {
c1a9b12d 209 self.err = Some(FixupError::UnresolvedIntTy(vid));
f035d41b 210 self.tcx().ty_error()
1a4d82fc 211 }
b7449926 212 ty::Infer(ty::FloatVar(vid)) => {
c1a9b12d 213 self.err = Some(FixupError::UnresolvedFloatTy(vid));
f035d41b 214 self.tcx().ty_error()
1a4d82fc 215 }
b7449926 216 ty::Infer(_) => {
54a0048b 217 bug!("Unexpected type in full type resolver: {:?}", t);
1a4d82fc 218 }
dfeec247 219 _ => t.super_fold_with(self),
1a4d82fc
JJ
220 }
221 }
222 }
223
7cac9316 224 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
9e0c209e 225 match *r {
dfeec247
XL
226 ty::ReVar(rid) => self
227 .infcx
228 .lexical_region_resolutions
229 .borrow()
230 .as_ref()
231 .expect("region resolution not performed")
232 .resolve_var(rid),
9e0c209e 233 _ => r,
1a4d82fc
JJ
234 }
235 }
48663c56
XL
236
237 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
ba9703b0 238 if !c.needs_infer() {
48663c56 239 c // micro-optimize -- if there is nothing in this const that this fold affects...
48663c56
XL
240 } else {
241 let c = self.infcx.shallow_resolve(c);
242 match c.val {
60c5eb7d 243 ty::ConstKind::Infer(InferConst::Var(vid)) => {
48663c56 244 self.err = Some(FixupError::UnresolvedConst(vid));
f035d41b 245 return self.tcx().const_error(c.ty);
48663c56 246 }
60c5eb7d 247 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
48663c56
XL
248 bug!("Unexpected const in full const resolver: {:?}", c);
249 }
250 _ => {}
251 }
252 c.super_fold_with(self)
253 }
254 }
1a4d82fc 255}