1 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2 use super::{FixupError, FixupResult, InferCtxt, Span};
3 use rustc_middle::ty::fold::{TypeFolder, TypeVisitor};
4 use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
6 ///////////////////////////////////////////////////////////////////////////
7 // OPPORTUNISTIC VAR RESOLVER
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, 'tcx> {
15 infcx: &'a InferCtxt<'a, 'tcx>,
18 impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
20 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
21 OpportunisticVarResolver { infcx }
25 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
26 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
30 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
31 if !t.has_infer_types_or_consts() {
32 t // micro-optimize -- if there is nothing in this type that this fold affects...
34 let t = self.infcx.shallow_resolve(t);
35 t.super_fold_with(self)
39 fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
40 if !ct.has_infer_types_or_consts() {
41 ct // micro-optimize -- if there is nothing in this const that this fold affects...
43 let ct = self.infcx.shallow_resolve(ct);
44 ct.super_fold_with(self)
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, 'tcx> {
53 infcx: &'a InferCtxt<'a, 'tcx>,
56 impl<'a, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'tcx> {
57 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
58 OpportunisticTypeAndRegionResolver { infcx }
62 impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticTypeAndRegionResolver<'a, 'tcx> {
63 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
67 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
69 t // micro-optimize -- if there is nothing in this type that this fold affects...
71 let t0 = self.infcx.shallow_resolve(t);
72 t0.super_fold_with(self)
76 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
78 ty::ReVar(rid) => self
82 .unwrap_region_constraints()
83 .opportunistic_resolve_var(self.tcx(), rid),
88 fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
89 if !ct.needs_infer() {
90 ct // micro-optimize -- if there is nothing in this const that this fold affects...
92 let c0 = self.infcx.shallow_resolve(ct);
93 c0.super_fold_with(self)
98 ///////////////////////////////////////////////////////////////////////////
99 // UNRESOLVED TYPE FINDER
101 /// The unresolved type **finder** walks a type searching for
102 /// type variables that don't yet have a value. The first unresolved type is stored.
103 /// It does not construct the fully resolved type (which might
104 /// involve some hashing and so forth).
105 pub struct UnresolvedTypeFinder<'a, 'tcx> {
106 infcx: &'a InferCtxt<'a, 'tcx>,
108 /// Used to find the type parameter name and location for error reporting.
109 pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
112 impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
113 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
114 UnresolvedTypeFinder { infcx, first_unresolved: None }
118 impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
119 fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
120 let t = self.infcx.shallow_resolve(t);
121 if t.has_infer_types() {
122 if let ty::Infer(infer_ty) = t.kind {
123 // Since we called `shallow_resolve` above, this must
124 // be an (as yet...) unresolved inference variable.
125 let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
126 let mut inner = self.infcx.inner.borrow_mut();
127 let ty_vars = &inner.type_variables();
128 if let TypeVariableOrigin {
129 kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
131 } = *ty_vars.var_origin(ty_vid)
140 self.first_unresolved = Some((t, ty_var_span));
141 true // Halt visiting.
143 // Otherwise, visit its contents.
144 t.super_visit_with(self)
147 // All type variables in inference types must already be resolved,
148 // - no need to visit the contents, continue visiting.
154 ///////////////////////////////////////////////////////////////////////////
155 // FULL TYPE RESOLUTION
157 /// Full type resolution replaces all type and region variables with
158 /// their concrete results. If any variable cannot be replaced (never unified, etc)
159 /// then an `Err` result is returned.
160 pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T>
162 T: TypeFoldable<'tcx>,
164 let mut full_resolver = FullTypeResolver { infcx, err: None };
165 let result = value.fold_with(&mut full_resolver);
166 match full_resolver.err {
172 // N.B. This type is not public because the protocol around checking the
173 // `err` field is not enforceable otherwise.
174 struct FullTypeResolver<'a, 'tcx> {
175 infcx: &'a InferCtxt<'a, 'tcx>,
176 err: Option<FixupError<'tcx>>,
179 impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
180 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
184 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
185 if !t.needs_infer() {
186 t // micro-optimize -- if there is nothing in this type that this fold affects...
188 let t = self.infcx.shallow_resolve(t);
190 ty::Infer(ty::TyVar(vid)) => {
191 self.err = Some(FixupError::UnresolvedTy(vid));
194 ty::Infer(ty::IntVar(vid)) => {
195 self.err = Some(FixupError::UnresolvedIntTy(vid));
198 ty::Infer(ty::FloatVar(vid)) => {
199 self.err = Some(FixupError::UnresolvedFloatTy(vid));
203 bug!("Unexpected type in full type resolver: {:?}", t);
205 _ => t.super_fold_with(self),
210 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
212 ty::ReVar(rid) => self
214 .lexical_region_resolutions
217 .expect("region resolution not performed")
223 fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
224 if !c.needs_infer() {
225 c // micro-optimize -- if there is nothing in this const that this fold affects...
227 let c = self.infcx.shallow_resolve(c);
229 ty::ConstKind::Infer(InferConst::Var(vid)) => {
230 self.err = Some(FixupError::UnresolvedConst(vid));
231 return self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: c.ty });
233 ty::ConstKind::Infer(InferConst::Fresh(_)) => {
234 bug!("Unexpected const in full const resolver: {:?}", c);
238 c.super_fold_with(self)