]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_infer/src/infer/resolve.rs
New upstream version 1.60.0+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;
a2a8927a 4use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeVisitor};
ba9703b0 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
5099ac24 42 fn fold_const(&mut self, ct: Const<'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 100
5099ac24 101 fn fold_const(&mut self, ct: ty::Const<'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
XL
128 type BreakTy = (Ty<'tcx>, Option<Span>);
129 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
3b2f2976
XL
130 let t = self.infcx.shallow_resolve(t);
131 if t.has_infer_types() {
1b1a35ee 132 if let ty::Infer(infer_ty) = *t.kind() {
3b2f2976
XL
133 // Since we called `shallow_resolve` above, this must
134 // be an (as yet...) unresolved inference variable.
dfeec247 135 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
f9f354fc
XL
136 let mut inner = self.infcx.inner.borrow_mut();
137 let ty_vars = &inner.type_variables();
dc9dc135 138 if let TypeVariableOrigin {
dfeec247 139 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
dc9dc135
XL
140 span,
141 } = *ty_vars.var_origin(ty_vid)
48663c56
XL
142 {
143 Some(span)
144 } else {
145 None
146 }
147 } else {
148 None
149 };
fc512014 150 ControlFlow::Break((t, ty_var_span))
3b2f2976
XL
151 } else {
152 // Otherwise, visit its contents.
153 t.super_visit_with(self)
154 }
155 } else {
48663c56
XL
156 // All type variables in inference types must already be resolved,
157 // - no need to visit the contents, continue visiting.
29967ef6 158 ControlFlow::CONTINUE
3b2f2976
XL
159 }
160 }
161}
162
1a4d82fc
JJ
163///////////////////////////////////////////////////////////////////////////
164// FULL TYPE RESOLUTION
165
166/// Full type resolution replaces all type and region variables with
167/// their concrete results. If any variable cannot be replaced (never unified, etc)
168/// then an `Err` result is returned.
fc512014 169pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> FixupResult<'tcx, T>
dc9dc135
XL
170where
171 T: TypeFoldable<'tcx>,
1a4d82fc 172{
a2a8927a 173 value.try_fold_with(&mut FullTypeResolver { infcx })
1a4d82fc
JJ
174}
175
176// N.B. This type is not public because the protocol around checking the
74b04a01 177// `err` field is not enforceable otherwise.
dc9dc135
XL
178struct FullTypeResolver<'a, 'tcx> {
179 infcx: &'a InferCtxt<'a, 'tcx>,
1a4d82fc
JJ
180}
181
dc9dc135 182impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
a2a8927a
XL
183 type Error = FixupError<'tcx>;
184
dc9dc135 185 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1a4d82fc
JJ
186 self.infcx.tcx
187 }
a2a8927a 188}
1a4d82fc 189
a2a8927a
XL
190impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
191 fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
ba9703b0 192 if !t.needs_infer() {
a2a8927a 193 Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects...
1a4d82fc
JJ
194 } else {
195 let t = self.infcx.shallow_resolve(t);
1b1a35ee 196 match *t.kind() {
a2a8927a
XL
197 ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
198 ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
199 ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
b7449926 200 ty::Infer(_) => {
54a0048b 201 bug!("Unexpected type in full type resolver: {:?}", t);
1a4d82fc 202 }
a2a8927a 203 _ => t.try_super_fold_with(self),
1a4d82fc
JJ
204 }
205 }
206 }
207
a2a8927a 208 fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
9e0c209e 209 match *r {
a2a8927a 210 ty::ReVar(rid) => Ok(self
dfeec247
XL
211 .infcx
212 .lexical_region_resolutions
213 .borrow()
214 .as_ref()
215 .expect("region resolution not performed")
a2a8927a
XL
216 .resolve_var(rid)),
217 _ => Ok(r),
1a4d82fc
JJ
218 }
219 }
48663c56 220
5099ac24 221 fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
ba9703b0 222 if !c.needs_infer() {
a2a8927a 223 Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
48663c56
XL
224 } else {
225 let c = self.infcx.shallow_resolve(c);
5099ac24 226 match c.val() {
60c5eb7d 227 ty::ConstKind::Infer(InferConst::Var(vid)) => {
a2a8927a 228 return Err(FixupError::UnresolvedConst(vid));
48663c56 229 }
60c5eb7d 230 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
48663c56
XL
231 bug!("Unexpected const in full const resolver: {:?}", c);
232 }
233 _ => {}
234 }
a2a8927a 235 c.try_super_fold_with(self)
48663c56
XL
236 }
237 }
1a4d82fc 238}