diff options
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r-- | crates/hir_ty/src/infer/coerce.rs | 56 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 216 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/pat.rs | 92 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/path.rs | 39 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/unify.rs | 154 |
5 files changed, 294 insertions, 263 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 9c62932b1..fd679f444 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs | |||
@@ -7,9 +7,7 @@ | |||
7 | use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; | 7 | use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; |
8 | use hir_def::lang_item::LangItemTarget; | 8 | use hir_def::lang_item::LangItemTarget; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{autoderef, Canonical, Interner, Solution, Ty, TyBuilder, TyExt, TyKind}; |
11 | autoderef, to_chalk_trait_id, traits::Solution, Interner, Substitution, TraitRef, Ty, TyKind, | ||
12 | }; | ||
13 | 11 | ||
14 | use super::{InEnvironment, InferenceContext}; | 12 | use super::{InEnvironment, InferenceContext}; |
15 | 13 | ||
@@ -36,7 +34,7 @@ impl<'a> InferenceContext<'a> { | |||
36 | ty1.clone() | 34 | ty1.clone() |
37 | } else { | 35 | } else { |
38 | if let (TyKind::FnDef(..), TyKind::FnDef(..)) = | 36 | if let (TyKind::FnDef(..), TyKind::FnDef(..)) = |
39 | (ty1.interned(&Interner), ty2.interned(&Interner)) | 37 | (ty1.kind(&Interner), ty2.kind(&Interner)) |
40 | { | 38 | { |
41 | cov_mark::hit!(coerce_fn_reification); | 39 | cov_mark::hit!(coerce_fn_reification); |
42 | // Special case: two function types. Try to coerce both to | 40 | // Special case: two function types. Try to coerce both to |
@@ -44,8 +42,8 @@ impl<'a> InferenceContext<'a> { | |||
44 | // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916 | 42 | // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916 |
45 | let sig1 = ty1.callable_sig(self.db).expect("FnDef without callable sig"); | 43 | let sig1 = ty1.callable_sig(self.db).expect("FnDef without callable sig"); |
46 | let sig2 = ty2.callable_sig(self.db).expect("FnDef without callable sig"); | 44 | let sig2 = ty2.callable_sig(self.db).expect("FnDef without callable sig"); |
47 | let ptr_ty1 = Ty::fn_ptr(sig1); | 45 | let ptr_ty1 = TyBuilder::fn_ptr(sig1); |
48 | let ptr_ty2 = Ty::fn_ptr(sig2); | 46 | let ptr_ty2 = TyBuilder::fn_ptr(sig2); |
49 | self.coerce_merge_branch(&ptr_ty1, &ptr_ty2) | 47 | self.coerce_merge_branch(&ptr_ty1, &ptr_ty2) |
50 | } else { | 48 | } else { |
51 | cov_mark::hit!(coerce_merge_fail_fallback); | 49 | cov_mark::hit!(coerce_merge_fail_fallback); |
@@ -55,7 +53,7 @@ impl<'a> InferenceContext<'a> { | |||
55 | } | 53 | } |
56 | 54 | ||
57 | fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { | 55 | fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { |
58 | match (from_ty.interned(&Interner), to_ty.interned(&Interner)) { | 56 | match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { |
59 | // Never type will make type variable to fallback to Never Type instead of Unknown. | 57 | // Never type will make type variable to fallback to Never Type instead of Unknown. |
60 | (TyKind::Never, TyKind::InferenceVar(tv, TyVariableKind::General)) => { | 58 | (TyKind::Never, TyKind::InferenceVar(tv, TyVariableKind::General)) => { |
61 | self.table.type_variable_table.set_diverging(*tv, true); | 59 | self.table.type_variable_table.set_diverging(*tv, true); |
@@ -73,7 +71,7 @@ impl<'a> InferenceContext<'a> { | |||
73 | } | 71 | } |
74 | 72 | ||
75 | // Pointer weakening and function to pointer | 73 | // Pointer weakening and function to pointer |
76 | match (from_ty.interned_mut(), to_ty.interned(&Interner)) { | 74 | match (from_ty.interned_mut(), to_ty.kind(&Interner)) { |
77 | // `*mut T` -> `*const T` | 75 | // `*mut T` -> `*const T` |
78 | // `&mut T` -> `&T` | 76 | // `&mut T` -> `&T` |
79 | (TyKind::Raw(m1, ..), TyKind::Raw(m2 @ Mutability::Not, ..)) | 77 | (TyKind::Raw(m1, ..), TyKind::Raw(m2 @ Mutability::Not, ..)) |
@@ -83,7 +81,7 @@ impl<'a> InferenceContext<'a> { | |||
83 | // `&T` -> `*const T` | 81 | // `&T` -> `*const T` |
84 | // `&mut T` -> `*mut T`/`*const T` | 82 | // `&mut T` -> `*mut T`/`*const T` |
85 | (TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..)) | 83 | (TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..)) |
86 | | (TyKind::Ref(Mutability::Mut, substs), &TyKind::Raw(m2, ..)) => { | 84 | | (TyKind::Ref(Mutability::Mut, _, substs), &TyKind::Raw(m2, ..)) => { |
87 | from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner); | 85 | from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner); |
88 | } | 86 | } |
89 | 87 | ||
@@ -95,12 +93,12 @@ impl<'a> InferenceContext<'a> { | |||
95 | (TyKind::FnDef(..), TyKind::Function { .. }) => match from_ty.callable_sig(self.db) { | 93 | (TyKind::FnDef(..), TyKind::Function { .. }) => match from_ty.callable_sig(self.db) { |
96 | None => return false, | 94 | None => return false, |
97 | Some(sig) => { | 95 | Some(sig) => { |
98 | from_ty = Ty::fn_ptr(sig); | 96 | from_ty = TyBuilder::fn_ptr(sig); |
99 | } | 97 | } |
100 | }, | 98 | }, |
101 | 99 | ||
102 | (TyKind::Closure(.., substs), TyKind::Function { .. }) => { | 100 | (TyKind::Closure(.., substs), TyKind::Function { .. }) => { |
103 | from_ty = substs[0].clone(); | 101 | from_ty = substs.at(&Interner, 0).assert_ty_ref(&Interner).clone(); |
104 | } | 102 | } |
105 | 103 | ||
106 | _ => {} | 104 | _ => {} |
@@ -111,9 +109,11 @@ impl<'a> InferenceContext<'a> { | |||
111 | } | 109 | } |
112 | 110 | ||
113 | // Auto Deref if cannot coerce | 111 | // Auto Deref if cannot coerce |
114 | match (from_ty.interned(&Interner), to_ty.interned(&Interner)) { | 112 | match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { |
115 | // FIXME: DerefMut | 113 | // FIXME: DerefMut |
116 | (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2), | 114 | (TyKind::Ref(.., st1), TyKind::Ref(.., st2)) => { |
115 | self.unify_autoderef_behind_ref(st1, st2) | ||
116 | } | ||
117 | 117 | ||
118 | // Otherwise, normal unify | 118 | // Otherwise, normal unify |
119 | _ => self.unify(&from_ty, to_ty), | 119 | _ => self.unify(&from_ty, to_ty), |
@@ -130,19 +130,16 @@ impl<'a> InferenceContext<'a> { | |||
130 | _ => return None, | 130 | _ => return None, |
131 | }; | 131 | }; |
132 | 132 | ||
133 | let generic_params = crate::utils::generics(self.db.upcast(), coerce_unsized_trait.into()); | 133 | let trait_ref = { |
134 | if generic_params.len() != 2 { | 134 | let b = TyBuilder::trait_ref(self.db, coerce_unsized_trait); |
135 | // The CoerceUnsized trait should have two generic params: Self and T. | 135 | if b.remaining() != 2 { |
136 | return None; | 136 | // The CoerceUnsized trait should have two generic params: Self and T. |
137 | } | 137 | return None; |
138 | } | ||
139 | b.push(from_ty.clone()).push(to_ty.clone()).build() | ||
140 | }; | ||
138 | 141 | ||
139 | let substs = Substitution::build_for_generics(&generic_params) | 142 | let goal = InEnvironment::new(&self.trait_env.env, trait_ref.cast(&Interner)); |
140 | .push(from_ty.clone()) | ||
141 | .push(to_ty.clone()) | ||
142 | .build(); | ||
143 | let trait_ref = | ||
144 | TraitRef { trait_id: to_chalk_trait_id(coerce_unsized_trait), substitution: substs }; | ||
145 | let goal = InEnvironment::new(self.trait_env.env.clone(), trait_ref.cast(&Interner)); | ||
146 | 143 | ||
147 | let canonicalizer = self.canonicalizer(); | 144 | let canonicalizer = self.canonicalizer(); |
148 | let canonicalized = canonicalizer.canonicalize_obligation(goal); | 145 | let canonicalized = canonicalizer.canonicalize_obligation(goal); |
@@ -151,7 +148,14 @@ impl<'a> InferenceContext<'a> { | |||
151 | 148 | ||
152 | match solution { | 149 | match solution { |
153 | Solution::Unique(v) => { | 150 | Solution::Unique(v) => { |
154 | canonicalized.apply_solution(self, v.0); | 151 | canonicalized.apply_solution( |
152 | self, | ||
153 | Canonical { | ||
154 | binders: v.binders, | ||
155 | // FIXME handle constraints | ||
156 | value: v.value.subst, | ||
157 | }, | ||
158 | ); | ||
155 | } | 159 | } |
156 | _ => return None, | 160 | _ => return None, |
157 | }; | 161 | }; |
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 3f3187ea2..9841988c5 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -15,15 +15,16 @@ use stdx::always; | |||
15 | use syntax::ast::RangeOp; | 15 | use syntax::ast::RangeOp; |
16 | 16 | ||
17 | use crate::{ | 17 | use crate::{ |
18 | autoderef, | 18 | autoderef, dummy_usize_const, |
19 | lower::lower_to_chalk_mutability, | 19 | lower::lower_to_chalk_mutability, |
20 | method_resolution, op, | 20 | method_resolution, op, |
21 | primitive::{self, UintTy}, | 21 | primitive::{self, UintTy}, |
22 | to_assoc_type_id, to_chalk_trait_id, | 22 | static_lifetime, to_chalk_trait_id, |
23 | traits::{chalk::from_chalk, FnTrait, InEnvironment}, | 23 | traits::{chalk::from_chalk, FnTrait}, |
24 | utils::{generics, variant_data, Generics}, | 24 | utils::{generics, Generics}, |
25 | AdtId, Binders, CallableDefId, DomainGoal, FnPointer, FnSig, Interner, Rawness, Scalar, | 25 | AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, |
26 | Substitution, TraitRef, Ty, TyKind, | 26 | ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, |
27 | TypeWalk, | ||
27 | }; | 28 | }; |
28 | 29 | ||
29 | use super::{ | 30 | use super::{ |
@@ -73,38 +74,33 @@ impl<'a> InferenceContext<'a> { | |||
73 | let fn_once_trait = FnTrait::FnOnce.get_id(self.db, krate)?; | 74 | let fn_once_trait = FnTrait::FnOnce.get_id(self.db, krate)?; |
74 | let output_assoc_type = | 75 | let output_assoc_type = |
75 | self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?; | 76 | self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?; |
76 | let generic_params = generics(self.db.upcast(), fn_once_trait.into()); | ||
77 | if generic_params.len() != 2 { | ||
78 | return None; | ||
79 | } | ||
80 | 77 | ||
81 | let mut param_builder = Substitution::builder(num_args); | ||
82 | let mut arg_tys = vec![]; | 78 | let mut arg_tys = vec![]; |
83 | for _ in 0..num_args { | 79 | let arg_ty = TyBuilder::tuple(num_args) |
84 | let arg = self.table.new_type_var(); | 80 | .fill(repeat_with(|| { |
85 | param_builder = param_builder.push(arg.clone()); | 81 | let arg = self.table.new_type_var(); |
86 | arg_tys.push(arg); | 82 | arg_tys.push(arg.clone()); |
87 | } | 83 | arg |
88 | let parameters = param_builder.build(); | 84 | })) |
89 | let arg_ty = TyKind::Tuple(num_args, parameters).intern(&Interner); | 85 | .build(); |
90 | let substs = | 86 | |
91 | Substitution::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); | 87 | let projection = { |
88 | let b = TyBuilder::assoc_type_projection(self.db, output_assoc_type); | ||
89 | if b.remaining() != 2 { | ||
90 | return None; | ||
91 | } | ||
92 | b.push(ty.clone()).push(arg_ty).build() | ||
93 | }; | ||
92 | 94 | ||
93 | let trait_env = self.trait_env.env.clone(); | 95 | let trait_env = self.trait_env.env.clone(); |
94 | let implements_fn_trait: DomainGoal = | 96 | let obligation = InEnvironment { |
95 | TraitRef { trait_id: to_chalk_trait_id(fn_once_trait), substitution: substs.clone() } | 97 | goal: projection.trait_ref(self.db).cast(&Interner), |
96 | .cast(&Interner); | ||
97 | let goal = self.canonicalizer().canonicalize_obligation(InEnvironment { | ||
98 | goal: implements_fn_trait.clone(), | ||
99 | environment: trait_env, | 98 | environment: trait_env, |
100 | }); | 99 | }; |
101 | if self.db.trait_solve(krate, goal.value).is_some() { | 100 | let canonical = self.canonicalizer().canonicalize_obligation(obligation.clone()); |
102 | self.obligations.push(implements_fn_trait); | 101 | if self.db.trait_solve(krate, canonical.value).is_some() { |
103 | let output_proj_ty = crate::ProjectionTy { | 102 | self.push_obligation(obligation.goal); |
104 | associated_ty_id: to_assoc_type_id(output_assoc_type), | 103 | let return_ty = self.normalize_projection_ty(projection); |
105 | substitution: substs, | ||
106 | }; | ||
107 | let return_ty = self.normalize_projection_ty(output_proj_ty); | ||
108 | Some((arg_tys, return_ty)) | 104 | Some((arg_tys, return_ty)) |
109 | } else { | 105 | } else { |
110 | None | 106 | None |
@@ -119,6 +115,8 @@ impl<'a> InferenceContext<'a> { | |||
119 | } | 115 | } |
120 | 116 | ||
121 | fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { | 117 | fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { |
118 | self.db.check_canceled(); | ||
119 | |||
122 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 120 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
123 | let ty = match &body[tgt_expr] { | 121 | let ty = match &body[tgt_expr] { |
124 | Expr::Missing => self.err_ty(), | 122 | Expr::Missing => self.err_ty(), |
@@ -136,7 +134,7 @@ impl<'a> InferenceContext<'a> { | |||
136 | both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe); | 134 | both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe); |
137 | let else_ty = match else_branch { | 135 | let else_ty = match else_branch { |
138 | Some(else_branch) => self.infer_expr_inner(*else_branch, &expected), | 136 | Some(else_branch) => self.infer_expr_inner(*else_branch, &expected), |
139 | None => Ty::unit(), | 137 | None => TyBuilder::unit(), |
140 | }; | 138 | }; |
141 | both_arms_diverge &= self.diverges; | 139 | both_arms_diverge &= self.diverges; |
142 | 140 | ||
@@ -183,7 +181,8 @@ impl<'a> InferenceContext<'a> { | |||
183 | let inner_ty = self.infer_expr(*body, &Expectation::none()); | 181 | let inner_ty = self.infer_expr(*body, &Expectation::none()); |
184 | let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body); | 182 | let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body); |
185 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); | 183 | let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); |
186 | TyKind::OpaqueType(opaque_ty_id, Substitution::single(inner_ty)).intern(&Interner) | 184 | TyKind::OpaqueType(opaque_ty_id, Substitution::from1(&Interner, inner_ty)) |
185 | .intern(&Interner) | ||
187 | } | 186 | } |
188 | Expr::Loop { body, label } => { | 187 | Expr::Loop { body, label } => { |
189 | self.breakables.push(BreakableContext { | 188 | self.breakables.push(BreakableContext { |
@@ -191,7 +190,7 @@ impl<'a> InferenceContext<'a> { | |||
191 | break_ty: self.table.new_type_var(), | 190 | break_ty: self.table.new_type_var(), |
192 | label: label.map(|label| self.body[label].name.clone()), | 191 | label: label.map(|label| self.body[label].name.clone()), |
193 | }); | 192 | }); |
194 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 193 | self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit())); |
195 | 194 | ||
196 | let ctxt = self.breakables.pop().expect("breakable stack broken"); | 195 | let ctxt = self.breakables.pop().expect("breakable stack broken"); |
197 | if ctxt.may_break { | 196 | if ctxt.may_break { |
@@ -215,11 +214,11 @@ impl<'a> InferenceContext<'a> { | |||
215 | *condition, | 214 | *condition, |
216 | &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)), | 215 | &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)), |
217 | ); | 216 | ); |
218 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 217 | self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit())); |
219 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); | 218 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); |
220 | // the body may not run, so it diverging doesn't mean we diverge | 219 | // the body may not run, so it diverging doesn't mean we diverge |
221 | self.diverges = Diverges::Maybe; | 220 | self.diverges = Diverges::Maybe; |
222 | Ty::unit() | 221 | TyBuilder::unit() |
223 | } | 222 | } |
224 | Expr::For { iterable, body, pat, label } => { | 223 | Expr::For { iterable, body, pat, label } => { |
225 | let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); | 224 | let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); |
@@ -234,11 +233,11 @@ impl<'a> InferenceContext<'a> { | |||
234 | 233 | ||
235 | self.infer_pat(*pat, &pat_ty, BindingMode::default()); | 234 | self.infer_pat(*pat, &pat_ty, BindingMode::default()); |
236 | 235 | ||
237 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 236 | self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit())); |
238 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); | 237 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); |
239 | // the body may not run, so it diverging doesn't mean we diverge | 238 | // the body may not run, so it diverging doesn't mean we diverge |
240 | self.diverges = Diverges::Maybe; | 239 | self.diverges = Diverges::Maybe; |
241 | Ty::unit() | 240 | TyBuilder::unit() |
242 | } | 241 | } |
243 | Expr::Lambda { body, args, ret_type, arg_types } => { | 242 | Expr::Lambda { body, args, ret_type, arg_types } => { |
244 | assert_eq!(args.len(), arg_types.len()); | 243 | assert_eq!(args.len(), arg_types.len()); |
@@ -262,14 +261,17 @@ impl<'a> InferenceContext<'a> { | |||
262 | }; | 261 | }; |
263 | sig_tys.push(ret_ty.clone()); | 262 | sig_tys.push(ret_ty.clone()); |
264 | let sig_ty = TyKind::Function(FnPointer { | 263 | let sig_ty = TyKind::Function(FnPointer { |
265 | num_args: sig_tys.len() - 1, | 264 | num_binders: 0, |
266 | sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false }, | 265 | sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false }, |
267 | substs: Substitution(sig_tys.clone().into()), | 266 | substitution: FnSubst( |
267 | Substitution::from_iter(&Interner, sig_tys.clone()).shifted_in(&Interner), | ||
268 | ), | ||
268 | }) | 269 | }) |
269 | .intern(&Interner); | 270 | .intern(&Interner); |
270 | let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into(); | 271 | let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into(); |
271 | let closure_ty = | 272 | let closure_ty = |
272 | TyKind::Closure(closure_id, Substitution::single(sig_ty)).intern(&Interner); | 273 | TyKind::Closure(closure_id, Substitution::from1(&Interner, sig_ty)) |
274 | .intern(&Interner); | ||
273 | 275 | ||
274 | // Eagerly try to relate the closure type with the expected | 276 | // Eagerly try to relate the closure type with the expected |
275 | // type, otherwise we often won't have enough information to | 277 | // type, otherwise we often won't have enough information to |
@@ -316,7 +318,13 @@ impl<'a> InferenceContext<'a> { | |||
316 | self.normalize_associated_types_in(ret_ty) | 318 | self.normalize_associated_types_in(ret_ty) |
317 | } | 319 | } |
318 | Expr::MethodCall { receiver, args, method_name, generic_args } => self | 320 | Expr::MethodCall { receiver, args, method_name, generic_args } => self |
319 | .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()), | 321 | .infer_method_call( |
322 | tgt_expr, | ||
323 | *receiver, | ||
324 | &args, | ||
325 | &method_name, | ||
326 | generic_args.as_deref(), | ||
327 | ), | ||
320 | Expr::Match { expr, arms } => { | 328 | Expr::Match { expr, arms } => { |
321 | let input_ty = self.infer_expr(*expr, &Expectation::none()); | 329 | let input_ty = self.infer_expr(*expr, &Expectation::none()); |
322 | 330 | ||
@@ -358,7 +366,7 @@ impl<'a> InferenceContext<'a> { | |||
358 | let val_ty = if let Some(expr) = expr { | 366 | let val_ty = if let Some(expr) = expr { |
359 | self.infer_expr(*expr, &Expectation::none()) | 367 | self.infer_expr(*expr, &Expectation::none()) |
360 | } else { | 368 | } else { |
361 | Ty::unit() | 369 | TyBuilder::unit() |
362 | }; | 370 | }; |
363 | 371 | ||
364 | let last_ty = | 372 | let last_ty = |
@@ -384,7 +392,7 @@ impl<'a> InferenceContext<'a> { | |||
384 | if let Some(expr) = expr { | 392 | if let Some(expr) = expr { |
385 | self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone())); | 393 | self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone())); |
386 | } else { | 394 | } else { |
387 | let unit = Ty::unit(); | 395 | let unit = TyBuilder::unit(); |
388 | self.coerce(&unit, &self.return_ty.clone()); | 396 | self.coerce(&unit, &self.return_ty.clone()); |
389 | } | 397 | } |
390 | TyKind::Never.intern(&Interner) | 398 | TyKind::Never.intern(&Interner) |
@@ -397,16 +405,19 @@ impl<'a> InferenceContext<'a> { | |||
397 | TyKind::Never.intern(&Interner) | 405 | TyKind::Never.intern(&Interner) |
398 | } | 406 | } |
399 | Expr::RecordLit { path, fields, spread } => { | 407 | Expr::RecordLit { path, fields, spread } => { |
400 | let (ty, def_id) = self.resolve_variant(path.as_ref()); | 408 | let (ty, def_id) = self.resolve_variant(path.as_deref()); |
401 | if let Some(variant) = def_id { | 409 | if let Some(variant) = def_id { |
402 | self.write_variant_resolution(tgt_expr.into(), variant); | 410 | self.write_variant_resolution(tgt_expr.into(), variant); |
403 | } | 411 | } |
404 | 412 | ||
405 | self.unify(&ty, &expected.ty); | 413 | self.unify(&ty, &expected.ty); |
406 | 414 | ||
407 | let substs = ty.substs().cloned().unwrap_or_else(Substitution::empty); | 415 | let substs = ty |
416 | .as_adt() | ||
417 | .map(|(_, s)| s.clone()) | ||
418 | .unwrap_or_else(|| Substitution::empty(&Interner)); | ||
408 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); | 419 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); |
409 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); | 420 | let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); |
410 | for field in fields.iter() { | 421 | for field in fields.iter() { |
411 | let field_def = | 422 | let field_def = |
412 | variant_data.as_ref().and_then(|it| match it.field(&field.name) { | 423 | variant_data.as_ref().and_then(|it| match it.field(&field.name) { |
@@ -418,11 +429,8 @@ impl<'a> InferenceContext<'a> { | |||
418 | None | 429 | None |
419 | } | 430 | } |
420 | }); | 431 | }); |
421 | if let Some(field_def) = field_def { | ||
422 | self.result.record_field_resolutions.insert(field.expr, field_def); | ||
423 | } | ||
424 | let field_ty = field_def.map_or(self.err_ty(), |it| { | 432 | let field_ty = field_def.map_or(self.err_ty(), |it| { |
425 | field_types[it.local_id].clone().subst(&substs) | 433 | field_types[it.local_id].clone().substitute(&Interner, &substs) |
426 | }); | 434 | }); |
427 | self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); | 435 | self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); |
428 | } | 436 | } |
@@ -453,10 +461,10 @@ impl<'a> InferenceContext<'a> { | |||
453 | }) | 461 | }) |
454 | .unwrap_or(true) | 462 | .unwrap_or(true) |
455 | }; | 463 | }; |
456 | match canonicalized.decanonicalize_ty(derefed_ty.value).interned(&Interner) { | 464 | match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) { |
457 | TyKind::Tuple(_, substs) => { | 465 | TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| { |
458 | name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) | 466 | substs.interned().get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned() |
459 | } | 467 | }), |
460 | TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { | 468 | TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { |
461 | let local_id = self.db.struct_data(*s).variant_data.field(name)?; | 469 | let local_id = self.db.struct_data(*s).variant_data.field(name)?; |
462 | let field = FieldId { parent: (*s).into(), local_id }; | 470 | let field = FieldId { parent: (*s).into(), local_id }; |
@@ -465,7 +473,7 @@ impl<'a> InferenceContext<'a> { | |||
465 | Some( | 473 | Some( |
466 | self.db.field_types((*s).into())[field.local_id] | 474 | self.db.field_types((*s).into())[field.local_id] |
467 | .clone() | 475 | .clone() |
468 | .subst(¶meters), | 476 | .substitute(&Interner, ¶meters), |
469 | ) | 477 | ) |
470 | } else { | 478 | } else { |
471 | None | 479 | None |
@@ -479,7 +487,7 @@ impl<'a> InferenceContext<'a> { | |||
479 | Some( | 487 | Some( |
480 | self.db.field_types((*u).into())[field.local_id] | 488 | self.db.field_types((*u).into())[field.local_id] |
481 | .clone() | 489 | .clone() |
482 | .subst(¶meters), | 490 | .substitute(&Interner, ¶meters), |
483 | ) | 491 | ) |
484 | } else { | 492 | } else { |
485 | None | 493 | None |
@@ -526,24 +534,17 @@ impl<'a> InferenceContext<'a> { | |||
526 | let inner_ty = self.infer_expr_inner(*expr, &expectation); | 534 | let inner_ty = self.infer_expr_inner(*expr, &expectation); |
527 | match rawness { | 535 | match rawness { |
528 | Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), | 536 | Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), |
529 | Rawness::Ref => TyKind::Ref(mutability, inner_ty), | 537 | Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty), |
530 | } | 538 | } |
531 | .intern(&Interner) | 539 | .intern(&Interner) |
532 | } | 540 | } |
533 | Expr::Box { expr } => { | 541 | Expr::Box { expr } => { |
534 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); | 542 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); |
535 | if let Some(box_) = self.resolve_boxed_box() { | 543 | if let Some(box_) = self.resolve_boxed_box() { |
536 | let mut sb = | 544 | TyBuilder::adt(self.db, box_) |
537 | Substitution::build_for_generics(&generics(self.db.upcast(), box_.into())); | 545 | .push(inner_ty) |
538 | sb = sb.push(inner_ty); | 546 | .fill_with_defaults(self.db, || self.table.new_type_var()) |
539 | match self.db.generic_defaults(box_.into()).get(1) { | 547 | .build() |
540 | Some(alloc_ty) if !alloc_ty.value.is_unknown() && sb.remaining() > 0 => { | ||
541 | sb = sb.push(alloc_ty.value.clone()); | ||
542 | } | ||
543 | _ => (), | ||
544 | } | ||
545 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); | ||
546 | Ty::adt_ty(box_, sb.build()) | ||
547 | } else { | 548 | } else { |
548 | self.err_ty() | 549 | self.err_ty() |
549 | } | 550 | } |
@@ -571,7 +572,7 @@ impl<'a> InferenceContext<'a> { | |||
571 | None => self.err_ty(), | 572 | None => self.err_ty(), |
572 | }, | 573 | }, |
573 | UnaryOp::Neg => { | 574 | UnaryOp::Neg => { |
574 | match inner_ty.interned(&Interner) { | 575 | match inner_ty.kind(&Interner) { |
575 | // Fast path for builtins | 576 | // Fast path for builtins |
576 | TyKind::Scalar(Scalar::Int(_)) | 577 | TyKind::Scalar(Scalar::Int(_)) |
577 | | TyKind::Scalar(Scalar::Uint(_)) | 578 | | TyKind::Scalar(Scalar::Uint(_)) |
@@ -584,7 +585,7 @@ impl<'a> InferenceContext<'a> { | |||
584 | } | 585 | } |
585 | } | 586 | } |
586 | UnaryOp::Not => { | 587 | UnaryOp::Not => { |
587 | match inner_ty.interned(&Interner) { | 588 | match inner_ty.kind(&Interner) { |
588 | // Fast path for builtins | 589 | // Fast path for builtins |
589 | TyKind::Scalar(Scalar::Bool) | 590 | TyKind::Scalar(Scalar::Bool) |
590 | | TyKind::Scalar(Scalar::Int(_)) | 591 | | TyKind::Scalar(Scalar::Int(_)) |
@@ -633,31 +634,31 @@ impl<'a> InferenceContext<'a> { | |||
633 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); | 634 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); |
634 | match (range_type, lhs_ty, rhs_ty) { | 635 | match (range_type, lhs_ty, rhs_ty) { |
635 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { | 636 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { |
636 | Some(adt) => Ty::adt_ty(adt, Substitution::empty()), | 637 | Some(adt) => TyBuilder::adt(self.db, adt).build(), |
637 | None => self.err_ty(), | 638 | None => self.err_ty(), |
638 | }, | 639 | }, |
639 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { | 640 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { |
640 | Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)), | 641 | Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(), |
641 | None => self.err_ty(), | 642 | None => self.err_ty(), |
642 | }, | 643 | }, |
643 | (RangeOp::Inclusive, None, Some(ty)) => { | 644 | (RangeOp::Inclusive, None, Some(ty)) => { |
644 | match self.resolve_range_to_inclusive() { | 645 | match self.resolve_range_to_inclusive() { |
645 | Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)), | 646 | Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(), |
646 | None => self.err_ty(), | 647 | None => self.err_ty(), |
647 | } | 648 | } |
648 | } | 649 | } |
649 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { | 650 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { |
650 | Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)), | 651 | Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(), |
651 | None => self.err_ty(), | 652 | None => self.err_ty(), |
652 | }, | 653 | }, |
653 | (RangeOp::Inclusive, Some(_), Some(ty)) => { | 654 | (RangeOp::Inclusive, Some(_), Some(ty)) => { |
654 | match self.resolve_range_inclusive() { | 655 | match self.resolve_range_inclusive() { |
655 | Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)), | 656 | Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(), |
656 | None => self.err_ty(), | 657 | None => self.err_ty(), |
657 | } | 658 | } |
658 | } | 659 | } |
659 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { | 660 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { |
660 | Some(adt) => Ty::adt_ty(adt, Substitution::single(ty)), | 661 | Some(adt) => TyBuilder::adt(self.db, adt).push(ty).build(), |
661 | None => self.err_ty(), | 662 | None => self.err_ty(), |
662 | }, | 663 | }, |
663 | (RangeOp::Inclusive, _, None) => self.err_ty(), | 664 | (RangeOp::Inclusive, _, None) => self.err_ty(), |
@@ -690,10 +691,10 @@ impl<'a> InferenceContext<'a> { | |||
690 | } | 691 | } |
691 | } | 692 | } |
692 | Expr::Tuple { exprs } => { | 693 | Expr::Tuple { exprs } => { |
693 | let mut tys = match expected.ty.interned(&Interner) { | 694 | let mut tys = match expected.ty.kind(&Interner) { |
694 | TyKind::Tuple(_, substs) => substs | 695 | TyKind::Tuple(_, substs) => substs |
695 | .iter() | 696 | .iter(&Interner) |
696 | .cloned() | 697 | .map(|a| a.assert_ty_ref(&Interner).clone()) |
697 | .chain(repeat_with(|| self.table.new_type_var())) | 698 | .chain(repeat_with(|| self.table.new_type_var())) |
698 | .take(exprs.len()) | 699 | .take(exprs.len()) |
699 | .collect::<Vec<_>>(), | 700 | .collect::<Vec<_>>(), |
@@ -704,11 +705,11 @@ impl<'a> InferenceContext<'a> { | |||
704 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); | 705 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); |
705 | } | 706 | } |
706 | 707 | ||
707 | TyKind::Tuple(tys.len(), Substitution(tys.into())).intern(&Interner) | 708 | TyKind::Tuple(tys.len(), Substitution::from_iter(&Interner, tys)).intern(&Interner) |
708 | } | 709 | } |
709 | Expr::Array(array) => { | 710 | Expr::Array(array) => { |
710 | let elem_ty = match expected.ty.interned(&Interner) { | 711 | let elem_ty = match expected.ty.kind(&Interner) { |
711 | TyKind::Array(st) | TyKind::Slice(st) => st.clone(), | 712 | TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(), |
712 | _ => self.table.new_type_var(), | 713 | _ => self.table.new_type_var(), |
713 | }; | 714 | }; |
714 | 715 | ||
@@ -732,17 +733,19 @@ impl<'a> InferenceContext<'a> { | |||
732 | } | 733 | } |
733 | } | 734 | } |
734 | 735 | ||
735 | TyKind::Array(elem_ty).intern(&Interner) | 736 | TyKind::Array(elem_ty, dummy_usize_const()).intern(&Interner) |
736 | } | 737 | } |
737 | Expr::Literal(lit) => match lit { | 738 | Expr::Literal(lit) => match lit { |
738 | Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), | 739 | Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), |
739 | Literal::String(..) => { | 740 | Literal::String(..) => { |
740 | TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner) | 741 | TyKind::Ref(Mutability::Not, static_lifetime(), TyKind::Str.intern(&Interner)) |
742 | .intern(&Interner) | ||
741 | } | 743 | } |
742 | Literal::ByteString(..) => { | 744 | Literal::ByteString(..) => { |
743 | let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); | 745 | let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); |
744 | let array_type = TyKind::Array(byte_type).intern(&Interner); | 746 | let array_type = |
745 | TyKind::Ref(Mutability::Not, array_type).intern(&Interner) | 747 | TyKind::Array(byte_type, dummy_usize_const()).intern(&Interner); |
748 | TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner) | ||
746 | } | 749 | } |
747 | Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), | 750 | Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), |
748 | Literal::Int(_v, ty) => match ty { | 751 | Literal::Int(_v, ty) => match ty { |
@@ -767,6 +770,7 @@ impl<'a> InferenceContext<'a> { | |||
767 | None => self.table.new_float_var(), | 770 | None => self.table.new_float_var(), |
768 | }, | 771 | }, |
769 | }, | 772 | }, |
773 | Expr::MacroStmts { tail } => self.infer_expr(*tail, expected), | ||
770 | }; | 774 | }; |
771 | // use a new type variable if we got unknown here | 775 | // use a new type variable if we got unknown here |
772 | let ty = self.insert_type_vars_shallow(ty); | 776 | let ty = self.insert_type_vars_shallow(ty); |
@@ -821,8 +825,8 @@ impl<'a> InferenceContext<'a> { | |||
821 | // we don't even make an attempt at coercion | 825 | // we don't even make an attempt at coercion |
822 | self.table.new_maybe_never_var() | 826 | self.table.new_maybe_never_var() |
823 | } else { | 827 | } else { |
824 | self.coerce(&Ty::unit(), &expected.coercion_target()); | 828 | self.coerce(&TyBuilder::unit(), &expected.coercion_target()); |
825 | Ty::unit() | 829 | TyBuilder::unit() |
826 | } | 830 | } |
827 | }; | 831 | }; |
828 | ty | 832 | ty |
@@ -858,10 +862,10 @@ impl<'a> InferenceContext<'a> { | |||
858 | self.write_method_resolution(tgt_expr, func); | 862 | self.write_method_resolution(tgt_expr, func); |
859 | (ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into()))) | 863 | (ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into()))) |
860 | } | 864 | } |
861 | None => (receiver_ty, Binders::new(0, self.err_ty()), None), | 865 | None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None), |
862 | }; | 866 | }; |
863 | let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty); | 867 | let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty); |
864 | let method_ty = method_ty.subst(&substs); | 868 | let method_ty = method_ty.substitute(&Interner, &substs); |
865 | let method_ty = self.insert_type_vars(method_ty); | 869 | let method_ty = self.insert_type_vars(method_ty); |
866 | self.register_obligations_for_call(&method_ty); | 870 | self.register_obligations_for_call(&method_ty); |
867 | let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) { | 871 | let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) { |
@@ -877,7 +881,9 @@ impl<'a> InferenceContext<'a> { | |||
877 | // Apply autoref so the below unification works correctly | 881 | // Apply autoref so the below unification works correctly |
878 | // FIXME: return correct autorefs from lookup_method | 882 | // FIXME: return correct autorefs from lookup_method |
879 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { | 883 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { |
880 | Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner), | 884 | Some((_, lifetime, mutability)) => { |
885 | TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner) | ||
886 | } | ||
881 | _ => derefed_receiver_ty, | 887 | _ => derefed_receiver_ty, |
882 | }; | 888 | }; |
883 | self.unify(&expected_receiver_ty, &actual_receiver_ty); | 889 | self.unify(&expected_receiver_ty, &actual_receiver_ty); |
@@ -950,18 +956,20 @@ impl<'a> InferenceContext<'a> { | |||
950 | substs.push(self.err_ty()); | 956 | substs.push(self.err_ty()); |
951 | } | 957 | } |
952 | assert_eq!(substs.len(), total_len); | 958 | assert_eq!(substs.len(), total_len); |
953 | Substitution(substs.into()) | 959 | Substitution::from_iter(&Interner, substs) |
954 | } | 960 | } |
955 | 961 | ||
956 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { | 962 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { |
957 | if let TyKind::FnDef(fn_def, parameters) = callable_ty.interned(&Interner) { | 963 | if let TyKind::FnDef(fn_def, parameters) = callable_ty.kind(&Interner) { |
958 | let def: CallableDefId = from_chalk(self.db, *fn_def); | 964 | let def: CallableDefId = from_chalk(self.db, *fn_def); |
959 | let generic_predicates = self.db.generic_predicates(def.into()); | 965 | let generic_predicates = self.db.generic_predicates(def.into()); |
960 | for predicate in generic_predicates.iter() { | 966 | for predicate in generic_predicates.iter() { |
961 | let (predicate, binders) = | 967 | let (predicate, binders) = predicate |
962 | predicate.clone().subst(parameters).into_value_and_skipped_binders(); | 968 | .clone() |
963 | always!(binders == 0); // quantified where clauses not yet handled | 969 | .substitute(&Interner, parameters) |
964 | self.obligations.push(predicate.cast(&Interner)); | 970 | .into_value_and_skipped_binders(); |
971 | always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled | ||
972 | self.push_obligation(predicate.cast(&Interner)); | ||
965 | } | 973 | } |
966 | // add obligation for trait implementation, if this is a trait method | 974 | // add obligation for trait implementation, if this is a trait method |
967 | match def { | 975 | match def { |
@@ -969,9 +977,11 @@ impl<'a> InferenceContext<'a> { | |||
969 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container | 977 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container |
970 | { | 978 | { |
971 | // construct a TraitRef | 979 | // construct a TraitRef |
972 | let substs = | 980 | let substs = crate::subst_prefix( |
973 | parameters.prefix(generics(self.db.upcast(), trait_.into()).len()); | 981 | &*parameters, |
974 | self.obligations.push( | 982 | generics(self.db.upcast(), trait_.into()).len(), |
983 | ); | ||
984 | self.push_obligation( | ||
975 | TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs } | 985 | TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs } |
976 | .cast(&Interner), | 986 | .cast(&Interner), |
977 | ); | 987 | ); |
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 474363709..a41e8e116 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs | |||
@@ -7,15 +7,13 @@ use chalk_ir::Mutability; | |||
7 | use hir_def::{ | 7 | use hir_def::{ |
8 | expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, | 8 | expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, |
9 | path::Path, | 9 | path::Path, |
10 | FieldId, | ||
11 | }; | 10 | }; |
12 | use hir_expand::name::Name; | 11 | use hir_expand::name::Name; |
13 | 12 | ||
14 | use super::{BindingMode, Expectation, InferenceContext}; | 13 | use super::{BindingMode, Expectation, InferenceContext}; |
15 | use crate::{ | 14 | use crate::{ |
16 | lower::lower_to_chalk_mutability, | 15 | lower::lower_to_chalk_mutability, static_lifetime, Interner, Substitution, Ty, TyBuilder, |
17 | utils::{generics, variant_data}, | 16 | TyExt, TyKind, |
18 | Interner, Substitution, Ty, TyKind, | ||
19 | }; | 17 | }; |
20 | 18 | ||
21 | impl<'a> InferenceContext<'a> { | 19 | impl<'a> InferenceContext<'a> { |
@@ -29,13 +27,14 @@ impl<'a> InferenceContext<'a> { | |||
29 | ellipsis: Option<usize>, | 27 | ellipsis: Option<usize>, |
30 | ) -> Ty { | 28 | ) -> Ty { |
31 | let (ty, def) = self.resolve_variant(path); | 29 | let (ty, def) = self.resolve_variant(path); |
32 | let var_data = def.map(|it| variant_data(self.db.upcast(), it)); | 30 | let var_data = def.map(|it| it.variant_data(self.db.upcast())); |
33 | if let Some(variant) = def { | 31 | if let Some(variant) = def { |
34 | self.write_variant_resolution(id.into(), variant); | 32 | self.write_variant_resolution(id.into(), variant); |
35 | } | 33 | } |
36 | self.unify(&ty, expected); | 34 | self.unify(&ty, expected); |
37 | 35 | ||
38 | let substs = ty.substs().cloned().unwrap_or_else(Substitution::empty); | 36 | let substs = |
37 | ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); | ||
39 | 38 | ||
40 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 39 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
41 | let (pre, post) = match ellipsis { | 40 | let (pre, post) = match ellipsis { |
@@ -50,7 +49,9 @@ impl<'a> InferenceContext<'a> { | |||
50 | let expected_ty = var_data | 49 | let expected_ty = var_data |
51 | .as_ref() | 50 | .as_ref() |
52 | .and_then(|d| d.field(&Name::new_tuple_field(i))) | 51 | .and_then(|d| d.field(&Name::new_tuple_field(i))) |
53 | .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs)); | 52 | .map_or(self.err_ty(), |field| { |
53 | field_tys[field].clone().substitute(&Interner, &substs) | ||
54 | }); | ||
54 | let expected_ty = self.normalize_associated_types_in(expected_ty); | 55 | let expected_ty = self.normalize_associated_types_in(expected_ty); |
55 | self.infer_pat(subpat, &expected_ty, default_bm); | 56 | self.infer_pat(subpat, &expected_ty, default_bm); |
56 | } | 57 | } |
@@ -67,25 +68,22 @@ impl<'a> InferenceContext<'a> { | |||
67 | id: PatId, | 68 | id: PatId, |
68 | ) -> Ty { | 69 | ) -> Ty { |
69 | let (ty, def) = self.resolve_variant(path); | 70 | let (ty, def) = self.resolve_variant(path); |
70 | let var_data = def.map(|it| variant_data(self.db.upcast(), it)); | 71 | let var_data = def.map(|it| it.variant_data(self.db.upcast())); |
71 | if let Some(variant) = def { | 72 | if let Some(variant) = def { |
72 | self.write_variant_resolution(id.into(), variant); | 73 | self.write_variant_resolution(id.into(), variant); |
73 | } | 74 | } |
74 | 75 | ||
75 | self.unify(&ty, expected); | 76 | self.unify(&ty, expected); |
76 | 77 | ||
77 | let substs = ty.substs().cloned().unwrap_or_else(Substitution::empty); | 78 | let substs = |
79 | ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner)); | ||
78 | 80 | ||
79 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 81 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
80 | for subpat in subpats { | 82 | for subpat in subpats { |
81 | let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name)); | 83 | let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name)); |
82 | if let Some(local_id) = matching_field { | 84 | let expected_ty = matching_field.map_or(self.err_ty(), |field| { |
83 | let field_def = FieldId { parent: def.unwrap(), local_id }; | 85 | field_tys[field].clone().substitute(&Interner, &substs) |
84 | self.result.record_pat_field_resolutions.insert(subpat.pat, field_def); | 86 | }); |
85 | } | ||
86 | |||
87 | let expected_ty = matching_field | ||
88 | .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs)); | ||
89 | let expected_ty = self.normalize_associated_types_in(expected_ty); | 87 | let expected_ty = self.normalize_associated_types_in(expected_ty); |
90 | self.infer_pat(subpat.pat, &expected_ty, default_bm); | 88 | self.infer_pat(subpat.pat, &expected_ty, default_bm); |
91 | } | 89 | } |
@@ -102,7 +100,7 @@ impl<'a> InferenceContext<'a> { | |||
102 | let body = Arc::clone(&self.body); // avoid borrow checker problem | 100 | let body = Arc::clone(&self.body); // avoid borrow checker problem |
103 | 101 | ||
104 | if is_non_ref_pat(&body, pat) { | 102 | if is_non_ref_pat(&body, pat) { |
105 | while let Some((inner, mutability)) = expected.as_reference() { | 103 | while let Some((inner, _lifetime, mutability)) = expected.as_reference() { |
106 | expected = inner; | 104 | expected = inner; |
107 | default_bm = match default_bm { | 105 | default_bm = match default_bm { |
108 | BindingMode::Move => BindingMode::Ref(mutability), | 106 | BindingMode::Move => BindingMode::Ref(mutability), |
@@ -124,7 +122,7 @@ impl<'a> InferenceContext<'a> { | |||
124 | let ty = match &body[pat] { | 122 | let ty = match &body[pat] { |
125 | &Pat::Tuple { ref args, ellipsis } => { | 123 | &Pat::Tuple { ref args, ellipsis } => { |
126 | let expectations = match expected.as_tuple() { | 124 | let expectations = match expected.as_tuple() { |
127 | Some(parameters) => &*parameters.0, | 125 | Some(parameters) => &*parameters.interned().as_slice(), |
128 | _ => &[], | 126 | _ => &[], |
129 | }; | 127 | }; |
130 | 128 | ||
@@ -134,7 +132,8 @@ impl<'a> InferenceContext<'a> { | |||
134 | }; | 132 | }; |
135 | let n_uncovered_patterns = expectations.len().saturating_sub(args.len()); | 133 | let n_uncovered_patterns = expectations.len().saturating_sub(args.len()); |
136 | let err_ty = self.err_ty(); | 134 | let err_ty = self.err_ty(); |
137 | let mut expectations_iter = expectations.iter().chain(repeat(&err_ty)); | 135 | let mut expectations_iter = |
136 | expectations.iter().map(|a| a.assert_ty_ref(&Interner)).chain(repeat(&err_ty)); | ||
138 | let mut infer_pat = |(&pat, ty)| self.infer_pat(pat, ty, default_bm); | 137 | let mut infer_pat = |(&pat, ty)| self.infer_pat(pat, ty, default_bm); |
139 | 138 | ||
140 | let mut inner_tys = Vec::with_capacity(n_uncovered_patterns + args.len()); | 139 | let mut inner_tys = Vec::with_capacity(n_uncovered_patterns + args.len()); |
@@ -142,7 +141,8 @@ impl<'a> InferenceContext<'a> { | |||
142 | inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); | 141 | inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); |
143 | inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); | 142 | inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); |
144 | 143 | ||
145 | TyKind::Tuple(inner_tys.len(), Substitution(inner_tys.into())).intern(&Interner) | 144 | TyKind::Tuple(inner_tys.len(), Substitution::from_iter(&Interner, inner_tys)) |
145 | .intern(&Interner) | ||
146 | } | 146 | } |
147 | Pat::Or(ref pats) => { | 147 | Pat::Or(ref pats) => { |
148 | if let Some((first_pat, rest)) = pats.split_first() { | 148 | if let Some((first_pat, rest)) = pats.split_first() { |
@@ -158,7 +158,7 @@ impl<'a> InferenceContext<'a> { | |||
158 | Pat::Ref { pat, mutability } => { | 158 | Pat::Ref { pat, mutability } => { |
159 | let mutability = lower_to_chalk_mutability(*mutability); | 159 | let mutability = lower_to_chalk_mutability(*mutability); |
160 | let expectation = match expected.as_reference() { | 160 | let expectation = match expected.as_reference() { |
161 | Some((inner_ty, exp_mut)) => { | 161 | Some((inner_ty, _lifetime, exp_mut)) => { |
162 | if mutability != exp_mut { | 162 | if mutability != exp_mut { |
163 | // FIXME: emit type error? | 163 | // FIXME: emit type error? |
164 | } | 164 | } |
@@ -167,10 +167,10 @@ impl<'a> InferenceContext<'a> { | |||
167 | _ => self.result.standard_types.unknown.clone(), | 167 | _ => self.result.standard_types.unknown.clone(), |
168 | }; | 168 | }; |
169 | let subty = self.infer_pat(*pat, &expectation, default_bm); | 169 | let subty = self.infer_pat(*pat, &expectation, default_bm); |
170 | TyKind::Ref(mutability, subty).intern(&Interner) | 170 | TyKind::Ref(mutability, static_lifetime(), subty).intern(&Interner) |
171 | } | 171 | } |
172 | Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( | 172 | Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( |
173 | p.as_ref(), | 173 | p.as_deref(), |
174 | subpats, | 174 | subpats, |
175 | expected, | 175 | expected, |
176 | default_bm, | 176 | default_bm, |
@@ -178,7 +178,7 @@ impl<'a> InferenceContext<'a> { | |||
178 | *ellipsis, | 178 | *ellipsis, |
179 | ), | 179 | ), |
180 | Pat::Record { path: p, args: fields, ellipsis: _ } => { | 180 | Pat::Record { path: p, args: fields, ellipsis: _ } => { |
181 | self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat) | 181 | self.infer_record_pat(p.as_deref(), fields, expected, default_bm, pat) |
182 | } | 182 | } |
183 | Pat::Path(path) => { | 183 | Pat::Path(path) => { |
184 | // FIXME use correct resolver for the surrounding expression | 184 | // FIXME use correct resolver for the surrounding expression |
@@ -200,7 +200,8 @@ impl<'a> InferenceContext<'a> { | |||
200 | 200 | ||
201 | let bound_ty = match mode { | 201 | let bound_ty = match mode { |
202 | BindingMode::Ref(mutability) => { | 202 | BindingMode::Ref(mutability) => { |
203 | TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner) | 203 | TyKind::Ref(mutability, static_lifetime(), inner_ty.clone()) |
204 | .intern(&Interner) | ||
204 | } | 205 | } |
205 | BindingMode::Move => inner_ty.clone(), | 206 | BindingMode::Move => inner_ty.clone(), |
206 | }; | 207 | }; |
@@ -209,17 +210,20 @@ impl<'a> InferenceContext<'a> { | |||
209 | return inner_ty; | 210 | return inner_ty; |
210 | } | 211 | } |
211 | Pat::Slice { prefix, slice, suffix } => { | 212 | Pat::Slice { prefix, slice, suffix } => { |
212 | let (container_ty, elem_ty): (fn(_) -> _, _) = match expected.interned(&Interner) { | 213 | let elem_ty = match expected.kind(&Interner) { |
213 | TyKind::Array(st) => (TyKind::Array, st.clone()), | 214 | TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(), |
214 | TyKind::Slice(st) => (TyKind::Slice, st.clone()), | 215 | _ => self.err_ty(), |
215 | _ => (TyKind::Slice, self.err_ty()), | ||
216 | }; | 216 | }; |
217 | 217 | ||
218 | for pat_id in prefix.iter().chain(suffix) { | 218 | for pat_id in prefix.iter().chain(suffix) { |
219 | self.infer_pat(*pat_id, &elem_ty, default_bm); | 219 | self.infer_pat(*pat_id, &elem_ty, default_bm); |
220 | } | 220 | } |
221 | 221 | ||
222 | let pat_ty = container_ty(elem_ty).intern(&Interner); | 222 | let pat_ty = match expected.kind(&Interner) { |
223 | TyKind::Array(_, const_) => TyKind::Array(elem_ty, const_.clone()), | ||
224 | _ => TyKind::Slice(elem_ty), | ||
225 | } | ||
226 | .intern(&Interner); | ||
223 | if let Some(slice_pat_id) = slice { | 227 | if let Some(slice_pat_id) = slice { |
224 | self.infer_pat(*slice_pat_id, &pat_ty, default_bm); | 228 | self.infer_pat(*slice_pat_id, &pat_ty, default_bm); |
225 | } | 229 | } |
@@ -236,30 +240,20 @@ impl<'a> InferenceContext<'a> { | |||
236 | Pat::Box { inner } => match self.resolve_boxed_box() { | 240 | Pat::Box { inner } => match self.resolve_boxed_box() { |
237 | Some(box_adt) => { | 241 | Some(box_adt) => { |
238 | let (inner_ty, alloc_ty) = match expected.as_adt() { | 242 | let (inner_ty, alloc_ty) = match expected.as_adt() { |
239 | Some((adt, subst)) if adt == box_adt => { | 243 | Some((adt, subst)) if adt == box_adt => ( |
240 | (subst[0].clone(), subst.get(1).cloned()) | 244 | subst.at(&Interner, 0).assert_ty_ref(&Interner).clone(), |
241 | } | 245 | subst.interned().get(1).and_then(|a| a.ty(&Interner).cloned()), |
246 | ), | ||
242 | _ => (self.result.standard_types.unknown.clone(), None), | 247 | _ => (self.result.standard_types.unknown.clone(), None), |
243 | }; | 248 | }; |
244 | 249 | ||
245 | let inner_ty = self.infer_pat(*inner, &inner_ty, default_bm); | 250 | let inner_ty = self.infer_pat(*inner, &inner_ty, default_bm); |
246 | let mut sb = Substitution::build_for_generics(&generics( | 251 | let mut b = TyBuilder::adt(self.db, box_adt).push(inner_ty); |
247 | self.db.upcast(), | 252 | |
248 | box_adt.into(), | 253 | if let Some(alloc_ty) = alloc_ty { |
249 | )); | 254 | b = b.push(alloc_ty); |
250 | sb = sb.push(inner_ty); | ||
251 | if sb.remaining() == 1 { | ||
252 | sb = sb.push(match alloc_ty { | ||
253 | Some(alloc_ty) if !alloc_ty.is_unknown() => alloc_ty, | ||
254 | _ => match self.db.generic_defaults(box_adt.into()).get(1) { | ||
255 | Some(alloc_ty) if !alloc_ty.value.is_unknown() => { | ||
256 | alloc_ty.value.clone() | ||
257 | } | ||
258 | _ => self.table.new_type_var(), | ||
259 | }, | ||
260 | }); | ||
261 | } | 255 | } |
262 | Ty::adt_ty(box_adt, sb.build()) | 256 | b.fill_with_defaults(self.db, || self.table.new_type_var()).build() |
263 | } | 257 | } |
264 | None => self.err_ty(), | 258 | None => self.err_ty(), |
265 | }, | 259 | }, |
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index cefa38509..f8955aa32 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs | |||
@@ -11,7 +11,8 @@ use hir_def::{ | |||
11 | use hir_expand::name::Name; | 11 | use hir_expand::name::Name; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | method_resolution, to_chalk_trait_id, Interner, Substitution, Ty, TyKind, ValueTyDefId, | 14 | method_resolution, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, |
15 | ValueTyDefId, | ||
15 | }; | 16 | }; |
16 | 17 | ||
17 | use super::{ExprOrPatId, InferenceContext, TraitRef}; | 18 | use super::{ExprOrPatId, InferenceContext, TraitRef}; |
@@ -82,10 +83,10 @@ impl<'a> InferenceContext<'a> { | |||
82 | } | 83 | } |
83 | ValueNs::ImplSelf(impl_id) => { | 84 | ValueNs::ImplSelf(impl_id) => { |
84 | let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); | 85 | let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); |
85 | let substs = Substitution::type_params_for_generics(self.db, &generics); | 86 | let substs = generics.type_params_subst(self.db); |
86 | let ty = self.db.impl_self_ty(impl_id).subst(&substs); | 87 | let ty = self.db.impl_self_ty(impl_id).substitute(&Interner, &substs); |
87 | if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { | 88 | if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { |
88 | let ty = self.db.value_ty(struct_id.into()).subst(&substs); | 89 | let ty = self.db.value_ty(struct_id.into()).substitute(&Interner, &substs); |
89 | return Some(ty); | 90 | return Some(ty); |
90 | } else { | 91 | } else { |
91 | // FIXME: diagnostic, invalid Self reference | 92 | // FIXME: diagnostic, invalid Self reference |
@@ -95,16 +96,13 @@ impl<'a> InferenceContext<'a> { | |||
95 | ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)), | 96 | ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)), |
96 | }; | 97 | }; |
97 | 98 | ||
98 | let ty = self.db.value_ty(typable); | 99 | let parent_substs = self_subst.unwrap_or_else(|| Substitution::empty(&Interner)); |
99 | // self_subst is just for the parent | ||
100 | let parent_substs = self_subst.unwrap_or_else(Substitution::empty); | ||
101 | let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver); | 100 | let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver); |
102 | let substs = ctx.substs_from_path(path, typable, true); | 101 | let substs = ctx.substs_from_path(path, typable, true); |
103 | let full_substs = Substitution::builder(substs.len()) | 102 | let ty = TyBuilder::value_ty(self.db, typable) |
104 | .use_parent_substs(&parent_substs) | 103 | .use_parent_substs(&parent_substs) |
105 | .fill(substs.0[parent_substs.len()..].iter().cloned()) | 104 | .fill(substs.interned()[parent_substs.len(&Interner)..].iter().cloned()) |
106 | .build(); | 105 | .build(); |
107 | let ty = ty.subst(&full_substs); | ||
108 | Some(ty) | 106 | Some(ty) |
109 | } | 107 | } |
110 | 108 | ||
@@ -147,7 +145,7 @@ impl<'a> InferenceContext<'a> { | |||
147 | remaining_segments_for_ty, | 145 | remaining_segments_for_ty, |
148 | true, | 146 | true, |
149 | ); | 147 | ); |
150 | if let TyKind::Unknown = ty.interned(&Interner) { | 148 | if let TyKind::Error = ty.kind(&Interner) { |
151 | return None; | 149 | return None; |
152 | } | 150 | } |
153 | 151 | ||
@@ -212,7 +210,7 @@ impl<'a> InferenceContext<'a> { | |||
212 | name: &Name, | 210 | name: &Name, |
213 | id: ExprOrPatId, | 211 | id: ExprOrPatId, |
214 | ) -> Option<(ValueNs, Option<Substitution>)> { | 212 | ) -> Option<(ValueNs, Option<Substitution>)> { |
215 | if let TyKind::Unknown = ty.interned(&Interner) { | 213 | if let TyKind::Error = ty.kind(&Interner) { |
216 | return None; | 214 | return None; |
217 | } | 215 | } |
218 | 216 | ||
@@ -245,27 +243,22 @@ impl<'a> InferenceContext<'a> { | |||
245 | }; | 243 | }; |
246 | let substs = match container { | 244 | let substs = match container { |
247 | AssocContainerId::ImplId(impl_id) => { | 245 | AssocContainerId::ImplId(impl_id) => { |
248 | let impl_substs = Substitution::build_for_def(self.db, impl_id) | 246 | let impl_substs = TyBuilder::subst_for_def(self.db, impl_id) |
249 | .fill(iter::repeat_with(|| self.table.new_type_var())) | 247 | .fill(iter::repeat_with(|| self.table.new_type_var())) |
250 | .build(); | 248 | .build(); |
251 | let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs); | 249 | let impl_self_ty = |
250 | self.db.impl_self_ty(impl_id).substitute(&Interner, &impl_substs); | ||
252 | self.unify(&impl_self_ty, &ty); | 251 | self.unify(&impl_self_ty, &ty); |
253 | Some(impl_substs) | 252 | Some(impl_substs) |
254 | } | 253 | } |
255 | AssocContainerId::TraitId(trait_) => { | 254 | AssocContainerId::TraitId(trait_) => { |
256 | // we're picking this method | 255 | // we're picking this method |
257 | let trait_substs = Substitution::build_for_def(self.db, trait_) | 256 | let trait_ref = TyBuilder::trait_ref(self.db, trait_) |
258 | .push(ty.clone()) | 257 | .push(ty.clone()) |
259 | .fill(std::iter::repeat_with(|| self.table.new_type_var())) | 258 | .fill(std::iter::repeat_with(|| self.table.new_type_var())) |
260 | .build(); | 259 | .build(); |
261 | self.obligations.push( | 260 | self.push_obligation(trait_ref.clone().cast(&Interner)); |
262 | TraitRef { | 261 | Some(trait_ref.substitution) |
263 | trait_id: to_chalk_trait_id(trait_), | ||
264 | substitution: trait_substs.clone(), | ||
265 | } | ||
266 | .cast(&Interner), | ||
267 | ); | ||
268 | Some(trait_substs) | ||
269 | } | 262 | } |
270 | AssocContainerId::ModuleId(_) => None, | 263 | AssocContainerId::ModuleId(_) => None, |
271 | }; | 264 | }; |
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 75250a369..2ea9dd920 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs | |||
@@ -7,8 +7,9 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; | |||
7 | 7 | ||
8 | use super::{DomainGoal, InferenceContext}; | 8 | use super::{DomainGoal, InferenceContext}; |
9 | use crate::{ | 9 | use crate::{ |
10 | AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, | 10 | AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, |
11 | InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, | 11 | InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, TypeWalk, |
12 | WhereClause, | ||
12 | }; | 13 | }; |
13 | 14 | ||
14 | impl<'a> InferenceContext<'a> { | 15 | impl<'a> InferenceContext<'a> { |
@@ -49,9 +50,9 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
49 | 50 | ||
50 | fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T { | 51 | fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T { |
51 | t.fold_binders( | 52 | t.fold_binders( |
52 | &mut |ty, binders| match ty.interned(&Interner) { | 53 | &mut |ty, binders| match ty.kind(&Interner) { |
53 | &TyKind::InferenceVar(var, kind) => { | 54 | &TyKind::InferenceVar(var, kind) => { |
54 | let inner = var.to_inner(); | 55 | let inner = from_inference_var(var); |
55 | if self.var_stack.contains(&inner) { | 56 | if self.var_stack.contains(&inner) { |
56 | // recursive type | 57 | // recursive type |
57 | return self.ctx.table.type_variable_table.fallback_value(var, kind); | 58 | return self.ctx.table.type_variable_table.fallback_value(var, kind); |
@@ -65,7 +66,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
65 | result | 66 | result |
66 | } else { | 67 | } else { |
67 | let root = self.ctx.table.var_unification_table.find(inner); | 68 | let root = self.ctx.table.var_unification_table.find(inner); |
68 | let position = self.add(InferenceVar::from_inner(root), kind); | 69 | let position = self.add(to_inference_var(root), kind); |
69 | TyKind::BoundVar(BoundVar::new(binders, position)).intern(&Interner) | 70 | TyKind::BoundVar(BoundVar::new(binders, position)).intern(&Interner) |
70 | } | 71 | } |
71 | } | 72 | } |
@@ -108,19 +109,22 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
108 | } | 109 | } |
109 | 110 | ||
110 | impl<T> Canonicalized<T> { | 111 | impl<T> Canonicalized<T> { |
111 | pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { | 112 | pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty { |
112 | ty.walk_mut_binders( | 113 | ty.fold_binders( |
113 | &mut |ty, binders| { | 114 | &mut |ty, binders| { |
114 | if let &mut TyKind::BoundVar(bound) = ty.interned_mut() { | 115 | if let TyKind::BoundVar(bound) = ty.kind(&Interner) { |
115 | if bound.debruijn >= binders { | 116 | if bound.debruijn >= binders { |
116 | let (v, k) = self.free_vars[bound.index]; | 117 | let (v, k) = self.free_vars[bound.index]; |
117 | *ty = TyKind::InferenceVar(v, k).intern(&Interner); | 118 | TyKind::InferenceVar(v, k).intern(&Interner) |
119 | } else { | ||
120 | ty | ||
118 | } | 121 | } |
122 | } else { | ||
123 | ty | ||
119 | } | 124 | } |
120 | }, | 125 | }, |
121 | DebruijnIndex::INNERMOST, | 126 | DebruijnIndex::INNERMOST, |
122 | ); | 127 | ) |
123 | ty | ||
124 | } | 128 | } |
125 | 129 | ||
126 | pub(super) fn apply_solution( | 130 | pub(super) fn apply_solution( |
@@ -129,52 +133,56 @@ impl<T> Canonicalized<T> { | |||
129 | solution: Canonical<Substitution>, | 133 | solution: Canonical<Substitution>, |
130 | ) { | 134 | ) { |
131 | // the solution may contain new variables, which we need to convert to new inference vars | 135 | // the solution may contain new variables, which we need to convert to new inference vars |
132 | let new_vars = Substitution( | 136 | let new_vars = Substitution::from_iter( |
133 | solution | 137 | &Interner, |
134 | .binders | 138 | solution.binders.iter(&Interner).map(|k| match k.kind { |
135 | .iter(&Interner) | 139 | VariableKind::Ty(TyVariableKind::General) => ctx.table.new_type_var(), |
136 | .map(|k| match k.kind { | 140 | VariableKind::Ty(TyVariableKind::Integer) => ctx.table.new_integer_var(), |
137 | VariableKind::Ty(TyVariableKind::General) => ctx.table.new_type_var(), | 141 | VariableKind::Ty(TyVariableKind::Float) => ctx.table.new_float_var(), |
138 | VariableKind::Ty(TyVariableKind::Integer) => ctx.table.new_integer_var(), | 142 | // HACK: Chalk can sometimes return new lifetime variables. We |
139 | VariableKind::Ty(TyVariableKind::Float) => ctx.table.new_float_var(), | 143 | // want to just skip them, but to not mess up the indices of |
140 | // HACK: Chalk can sometimes return new lifetime variables. We | 144 | // other variables, we'll just create a new type variable in |
141 | // want to just skip them, but to not mess up the indices of | 145 | // their place instead. This should not matter (we never see the |
142 | // other variables, we'll just create a new type variable in | 146 | // actual *uses* of the lifetime variable). |
143 | // their place instead. This should not matter (we never see the | 147 | VariableKind::Lifetime => ctx.table.new_type_var(), |
144 | // actual *uses* of the lifetime variable). | 148 | _ => panic!("const variable in solution"), |
145 | VariableKind::Lifetime => ctx.table.new_type_var(), | 149 | }), |
146 | _ => panic!("const variable in solution"), | ||
147 | }) | ||
148 | .collect(), | ||
149 | ); | 150 | ); |
150 | for (i, ty) in solution.value.into_iter().enumerate() { | 151 | for (i, ty) in solution.value.iter(&Interner).enumerate() { |
151 | let (v, k) = self.free_vars[i]; | 152 | let (v, k) = self.free_vars[i]; |
152 | // eagerly replace projections in the type; we may be getting types | 153 | // eagerly replace projections in the type; we may be getting types |
153 | // e.g. from where clauses where this hasn't happened yet | 154 | // e.g. from where clauses where this hasn't happened yet |
154 | let ty = ctx.normalize_associated_types_in(ty.clone().subst_bound_vars(&new_vars)); | 155 | let ty = ctx.normalize_associated_types_in( |
156 | new_vars.apply(ty.assert_ty_ref(&Interner).clone(), &Interner), | ||
157 | ); | ||
155 | ctx.table.unify(&TyKind::InferenceVar(v, k).intern(&Interner), &ty); | 158 | ctx.table.unify(&TyKind::InferenceVar(v, k).intern(&Interner), &ty); |
156 | } | 159 | } |
157 | } | 160 | } |
158 | } | 161 | } |
159 | 162 | ||
163 | pub fn could_unify(t1: &Ty, t2: &Ty) -> bool { | ||
164 | InferenceTable::new().unify(t1, t2) | ||
165 | } | ||
166 | |||
160 | pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { | 167 | pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { |
161 | let mut table = InferenceTable::new(); | 168 | let mut table = InferenceTable::new(); |
162 | let vars = Substitution( | 169 | let vars = Substitution::from_iter( |
170 | &Interner, | ||
163 | tys.binders | 171 | tys.binders |
164 | .iter(&Interner) | 172 | .iter(&Interner) |
165 | // we always use type vars here because we want everything to | 173 | // we always use type vars here because we want everything to |
166 | // fallback to Unknown in the end (kind of hacky, as below) | 174 | // fallback to Unknown in the end (kind of hacky, as below) |
167 | .map(|_| table.new_type_var()) | 175 | .map(|_| table.new_type_var()), |
168 | .collect(), | ||
169 | ); | 176 | ); |
170 | let ty1_with_vars = tys.value.0.clone().subst_bound_vars(&vars); | 177 | let ty1_with_vars = vars.apply(tys.value.0.clone(), &Interner); |
171 | let ty2_with_vars = tys.value.1.clone().subst_bound_vars(&vars); | 178 | let ty2_with_vars = vars.apply(tys.value.1.clone(), &Interner); |
172 | if !table.unify(&ty1_with_vars, &ty2_with_vars) { | 179 | if !table.unify(&ty1_with_vars, &ty2_with_vars) { |
173 | return None; | 180 | return None; |
174 | } | 181 | } |
175 | // default any type vars that weren't unified back to their original bound vars | 182 | // default any type vars that weren't unified back to their original bound vars |
176 | // (kind of hacky) | 183 | // (kind of hacky) |
177 | for (i, var) in vars.iter().enumerate() { | 184 | for (i, var) in vars.iter(&Interner).enumerate() { |
185 | let var = var.assert_ty_ref(&Interner); | ||
178 | if &*table.resolve_ty_shallow(var) == var { | 186 | if &*table.resolve_ty_shallow(var) == var { |
179 | table.unify( | 187 | table.unify( |
180 | var, | 188 | var, |
@@ -182,11 +190,11 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { | |||
182 | ); | 190 | ); |
183 | } | 191 | } |
184 | } | 192 | } |
185 | Some( | 193 | Some(Substitution::from_iter( |
186 | Substitution::builder(tys.binders.len(&Interner)) | 194 | &Interner, |
187 | .fill(vars.iter().map(|v| table.resolve_ty_completely(v.clone()))) | 195 | vars.iter(&Interner) |
188 | .build(), | 196 | .map(|v| table.resolve_ty_completely(v.assert_ty_ref(&Interner).clone())), |
189 | ) | 197 | )) |
190 | } | 198 | } |
191 | 199 | ||
192 | #[derive(Clone, Debug)] | 200 | #[derive(Clone, Debug)] |
@@ -200,17 +208,17 @@ impl TypeVariableTable { | |||
200 | } | 208 | } |
201 | 209 | ||
202 | pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) { | 210 | pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) { |
203 | self.inner[iv.to_inner().0 as usize].diverging = diverging; | 211 | self.inner[from_inference_var(iv).0 as usize].diverging = diverging; |
204 | } | 212 | } |
205 | 213 | ||
206 | fn is_diverging(&mut self, iv: InferenceVar) -> bool { | 214 | fn is_diverging(&mut self, iv: InferenceVar) -> bool { |
207 | self.inner[iv.to_inner().0 as usize].diverging | 215 | self.inner[from_inference_var(iv).0 as usize].diverging |
208 | } | 216 | } |
209 | 217 | ||
210 | fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty { | 218 | fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty { |
211 | match kind { | 219 | match kind { |
212 | _ if self.inner[iv.to_inner().0 as usize].diverging => TyKind::Never, | 220 | _ if self.inner[from_inference_var(iv).0 as usize].diverging => TyKind::Never, |
213 | TyVariableKind::General => TyKind::Unknown, | 221 | TyVariableKind::General => TyKind::Error, |
214 | TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)), | 222 | TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)), |
215 | TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)), | 223 | TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)), |
216 | } | 224 | } |
@@ -227,6 +235,7 @@ pub(crate) struct TypeVariableData { | |||
227 | pub(crate) struct InferenceTable { | 235 | pub(crate) struct InferenceTable { |
228 | pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>, | 236 | pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>, |
229 | pub(super) type_variable_table: TypeVariableTable, | 237 | pub(super) type_variable_table: TypeVariableTable, |
238 | pub(super) revision: u32, | ||
230 | } | 239 | } |
231 | 240 | ||
232 | impl InferenceTable { | 241 | impl InferenceTable { |
@@ -234,6 +243,7 @@ impl InferenceTable { | |||
234 | InferenceTable { | 243 | InferenceTable { |
235 | var_unification_table: InPlaceUnificationTable::new(), | 244 | var_unification_table: InPlaceUnificationTable::new(), |
236 | type_variable_table: TypeVariableTable { inner: Vec::new() }, | 245 | type_variable_table: TypeVariableTable { inner: Vec::new() }, |
246 | revision: 0, | ||
237 | } | 247 | } |
238 | } | 248 | } |
239 | 249 | ||
@@ -241,7 +251,7 @@ impl InferenceTable { | |||
241 | self.type_variable_table.push(TypeVariableData { diverging }); | 251 | self.type_variable_table.push(TypeVariableData { diverging }); |
242 | let key = self.var_unification_table.new_key(TypeVarValue::Unknown); | 252 | let key = self.var_unification_table.new_key(TypeVarValue::Unknown); |
243 | assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); | 253 | assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); |
244 | TyKind::InferenceVar(InferenceVar::from_inner(key), kind).intern(&Interner) | 254 | TyKind::InferenceVar(to_inference_var(key), kind).intern(&Interner) |
245 | } | 255 | } |
246 | 256 | ||
247 | pub(crate) fn new_type_var(&mut self) -> Ty { | 257 | pub(crate) fn new_type_var(&mut self) -> Ty { |
@@ -278,7 +288,9 @@ impl InferenceTable { | |||
278 | substs2: &Substitution, | 288 | substs2: &Substitution, |
279 | depth: usize, | 289 | depth: usize, |
280 | ) -> bool { | 290 | ) -> bool { |
281 | substs1.0.iter().zip(substs2.0.iter()).all(|(t1, t2)| self.unify_inner(t1, t2, depth)) | 291 | substs1.iter(&Interner).zip(substs2.iter(&Interner)).all(|(t1, t2)| { |
292 | self.unify_inner(t1.assert_ty_ref(&Interner), t2.assert_ty_ref(&Interner), depth) | ||
293 | }) | ||
282 | } | 294 | } |
283 | 295 | ||
284 | fn unify_inner(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -> bool { | 296 | fn unify_inner(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -> bool { |
@@ -293,12 +305,12 @@ impl InferenceTable { | |||
293 | let ty1 = self.resolve_ty_shallow(ty1); | 305 | let ty1 = self.resolve_ty_shallow(ty1); |
294 | let ty2 = self.resolve_ty_shallow(ty2); | 306 | let ty2 = self.resolve_ty_shallow(ty2); |
295 | if ty1.equals_ctor(&ty2) { | 307 | if ty1.equals_ctor(&ty2) { |
296 | match (ty1.interned(&Interner), ty2.interned(&Interner)) { | 308 | match (ty1.kind(&Interner), ty2.kind(&Interner)) { |
297 | (TyKind::Adt(_, substs1), TyKind::Adt(_, substs2)) | 309 | (TyKind::Adt(_, substs1), TyKind::Adt(_, substs2)) |
298 | | (TyKind::FnDef(_, substs1), TyKind::FnDef(_, substs2)) | 310 | | (TyKind::FnDef(_, substs1), TyKind::FnDef(_, substs2)) |
299 | | ( | 311 | | ( |
300 | TyKind::Function(FnPointer { substs: substs1, .. }), | 312 | TyKind::Function(FnPointer { substitution: FnSubst(substs1), .. }), |
301 | TyKind::Function(FnPointer { substs: substs2, .. }), | 313 | TyKind::Function(FnPointer { substitution: FnSubst(substs2), .. }), |
302 | ) | 314 | ) |
303 | | (TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2)) | 315 | | (TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2)) |
304 | | (TyKind::OpaqueType(_, substs1), TyKind::OpaqueType(_, substs2)) | 316 | | (TyKind::OpaqueType(_, substs1), TyKind::OpaqueType(_, substs2)) |
@@ -306,9 +318,11 @@ impl InferenceTable { | |||
306 | | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => { | 318 | | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => { |
307 | self.unify_substs(substs1, substs2, depth + 1) | 319 | self.unify_substs(substs1, substs2, depth + 1) |
308 | } | 320 | } |
309 | (TyKind::Ref(_, ty1), TyKind::Ref(_, ty2)) | 321 | (TyKind::Array(ty1, c1), TyKind::Array(ty2, c2)) if c1 == c2 => { |
322 | self.unify_inner(ty1, ty2, depth + 1) | ||
323 | } | ||
324 | (TyKind::Ref(_, _, ty1), TyKind::Ref(_, _, ty2)) | ||
310 | | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2)) | 325 | | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2)) |
311 | | (TyKind::Array(ty1), TyKind::Array(ty2)) | ||
312 | | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1), | 326 | | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1), |
313 | _ => true, /* we checked equals_ctor already */ | 327 | _ => true, /* we checked equals_ctor already */ |
314 | } | 328 | } |
@@ -318,8 +332,8 @@ impl InferenceTable { | |||
318 | } | 332 | } |
319 | 333 | ||
320 | pub(super) fn unify_inner_trivial(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -> bool { | 334 | pub(super) fn unify_inner_trivial(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -> bool { |
321 | match (ty1.interned(&Interner), ty2.interned(&Interner)) { | 335 | match (ty1.kind(&Interner), ty2.kind(&Interner)) { |
322 | (TyKind::Unknown, _) | (_, TyKind::Unknown) => true, | 336 | (TyKind::Error, _) | (_, TyKind::Error) => true, |
323 | 337 | ||
324 | (TyKind::Placeholder(p1), TyKind::Placeholder(p2)) if *p1 == *p2 => true, | 338 | (TyKind::Placeholder(p1), TyKind::Placeholder(p2)) if *p1 == *p2 => true, |
325 | 339 | ||
@@ -356,7 +370,14 @@ impl InferenceTable { | |||
356 | == self.type_variable_table.is_diverging(*tv2) => | 370 | == self.type_variable_table.is_diverging(*tv2) => |
357 | { | 371 | { |
358 | // both type vars are unknown since we tried to resolve them | 372 | // both type vars are unknown since we tried to resolve them |
359 | self.var_unification_table.union(tv1.to_inner(), tv2.to_inner()); | 373 | if !self |
374 | .var_unification_table | ||
375 | .unioned(from_inference_var(*tv1), from_inference_var(*tv2)) | ||
376 | { | ||
377 | self.var_unification_table | ||
378 | .union(from_inference_var(*tv1), from_inference_var(*tv2)); | ||
379 | self.revision += 1; | ||
380 | } | ||
360 | true | 381 | true |
361 | } | 382 | } |
362 | 383 | ||
@@ -391,9 +412,10 @@ impl InferenceTable { | |||
391 | ) => { | 412 | ) => { |
392 | // the type var is unknown since we tried to resolve it | 413 | // the type var is unknown since we tried to resolve it |
393 | self.var_unification_table.union_value( | 414 | self.var_unification_table.union_value( |
394 | tv.to_inner(), | 415 | from_inference_var(*tv), |
395 | TypeVarValue::Known(other.clone().intern(&Interner)), | 416 | TypeVarValue::Known(other.clone().intern(&Interner)), |
396 | ); | 417 | ); |
418 | self.revision += 1; | ||
397 | true | 419 | true |
398 | } | 420 | } |
399 | 421 | ||
@@ -443,9 +465,9 @@ impl InferenceTable { | |||
443 | if i > 0 { | 465 | if i > 0 { |
444 | cov_mark::hit!(type_var_resolves_to_int_var); | 466 | cov_mark::hit!(type_var_resolves_to_int_var); |
445 | } | 467 | } |
446 | match ty.interned(&Interner) { | 468 | match ty.kind(&Interner) { |
447 | TyKind::InferenceVar(tv, _) => { | 469 | TyKind::InferenceVar(tv, _) => { |
448 | let inner = tv.to_inner(); | 470 | let inner = from_inference_var(*tv); |
449 | match self.var_unification_table.inlined_probe_value(inner).known() { | 471 | match self.var_unification_table.inlined_probe_value(inner).known() { |
450 | Some(known_ty) => { | 472 | Some(known_ty) => { |
451 | // The known_ty can't be a type var itself | 473 | // The known_ty can't be a type var itself |
@@ -466,9 +488,9 @@ impl InferenceTable { | |||
466 | /// be resolved as far as possible, i.e. contain no type variables with | 488 | /// be resolved as far as possible, i.e. contain no type variables with |
467 | /// known type. | 489 | /// known type. |
468 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 490 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
469 | ty.fold(&mut |ty| match ty.interned(&Interner) { | 491 | ty.fold(&mut |ty| match ty.kind(&Interner) { |
470 | &TyKind::InferenceVar(tv, kind) => { | 492 | &TyKind::InferenceVar(tv, kind) => { |
471 | let inner = tv.to_inner(); | 493 | let inner = from_inference_var(tv); |
472 | if tv_stack.contains(&inner) { | 494 | if tv_stack.contains(&inner) { |
473 | cov_mark::hit!(type_var_cycles_resolve_as_possible); | 495 | cov_mark::hit!(type_var_cycles_resolve_as_possible); |
474 | // recursive type | 496 | // recursive type |
@@ -493,9 +515,9 @@ impl InferenceTable { | |||
493 | /// Resolves the type completely; type variables without known type are | 515 | /// Resolves the type completely; type variables without known type are |
494 | /// replaced by TyKind::Unknown. | 516 | /// replaced by TyKind::Unknown. |
495 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 517 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
496 | ty.fold(&mut |ty| match ty.interned(&Interner) { | 518 | ty.fold(&mut |ty| match ty.kind(&Interner) { |
497 | &TyKind::InferenceVar(tv, kind) => { | 519 | &TyKind::InferenceVar(tv, kind) => { |
498 | let inner = tv.to_inner(); | 520 | let inner = from_inference_var(tv); |
499 | if tv_stack.contains(&inner) { | 521 | if tv_stack.contains(&inner) { |
500 | cov_mark::hit!(type_var_cycles_resolve_completely); | 522 | cov_mark::hit!(type_var_cycles_resolve_completely); |
501 | // recursive type | 523 | // recursive type |
@@ -538,6 +560,14 @@ impl UnifyKey for TypeVarId { | |||
538 | } | 560 | } |
539 | } | 561 | } |
540 | 562 | ||
563 | fn from_inference_var(var: InferenceVar) -> TypeVarId { | ||
564 | TypeVarId(var.index()) | ||
565 | } | ||
566 | |||
567 | fn to_inference_var(TypeVarId(index): TypeVarId) -> InferenceVar { | ||
568 | index.into() | ||
569 | } | ||
570 | |||
541 | /// The value of a type variable: either we already know the type, or we don't | 571 | /// The value of a type variable: either we already know the type, or we don't |
542 | /// know it yet. | 572 | /// know it yet. |
543 | #[derive(Clone, PartialEq, Eq, Debug)] | 573 | #[derive(Clone, PartialEq, Eq, Debug)] |