diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-04-06 20:11:41 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-04-06 20:11:41 +0100 |
commit | 1643d1534b92eef1cf55631aba3ca9289c3cea72 (patch) | |
tree | 8db339ad4a14cde9a63235d7616f648d4e560e56 | |
parent | 5df65e5066a534cfb1e0d5ff494b1cd775d5dd90 (diff) | |
parent | 768ee3e47a140e354575154f431421375b0b5203 (diff) |
Merge #8385
8385: Align `InferenceVar` to Chalk r=flodiebold a=flodiebold
Co-authored-by: Florian Diebold <[email protected]>
-rw-r--r-- | crates/hir_ty/src/infer.rs | 19 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/unify.rs | 36 | ||||
-rw-r--r-- | crates/hir_ty/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 106 | ||||
-rw-r--r-- | crates/hir_ty/src/types.rs | 25 |
5 files changed, 85 insertions, 103 deletions
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index efe9198cc..75d633c96 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs | |||
@@ -683,25 +683,6 @@ impl<'a> InferenceContext<'a> { | |||
683 | } | 683 | } |
684 | } | 684 | } |
685 | 685 | ||
686 | /// The kinds of placeholders we need during type inference. There's separate | ||
687 | /// values for general types, and for integer and float variables. The latter | ||
688 | /// two are used for inference of literal values (e.g. `100` could be one of | ||
689 | /// several integer types). | ||
690 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] | ||
691 | pub struct InferenceVar { | ||
692 | index: u32, | ||
693 | } | ||
694 | |||
695 | impl InferenceVar { | ||
696 | fn to_inner(self) -> unify::TypeVarId { | ||
697 | unify::TypeVarId(self.index) | ||
698 | } | ||
699 | |||
700 | fn from_inner(unify::TypeVarId(index): unify::TypeVarId) -> Self { | ||
701 | InferenceVar { index } | ||
702 | } | ||
703 | } | ||
704 | |||
705 | /// When inferring an expression, we propagate downward whatever type hint we | 686 | /// When inferring an expression, we propagate downward whatever type hint we |
706 | /// are able in the form of an `Expectation`. | 687 | /// are able in the form of an `Expectation`. |
707 | #[derive(Clone, PartialEq, Eq, Debug)] | 688 | #[derive(Clone, PartialEq, Eq, Debug)] |
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 7d76cda68..d717e3375 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs | |||
@@ -51,7 +51,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
51 | t.fold_binders( | 51 | t.fold_binders( |
52 | &mut |ty, binders| match ty.kind(&Interner) { | 52 | &mut |ty, binders| match ty.kind(&Interner) { |
53 | &TyKind::InferenceVar(var, kind) => { | 53 | &TyKind::InferenceVar(var, kind) => { |
54 | let inner = var.to_inner(); | 54 | let inner = from_inference_var(var); |
55 | if self.var_stack.contains(&inner) { | 55 | if self.var_stack.contains(&inner) { |
56 | // recursive type | 56 | // recursive type |
57 | return self.ctx.table.type_variable_table.fallback_value(var, kind); | 57 | return self.ctx.table.type_variable_table.fallback_value(var, kind); |
@@ -65,7 +65,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
65 | result | 65 | result |
66 | } else { | 66 | } else { |
67 | let root = self.ctx.table.var_unification_table.find(inner); | 67 | let root = self.ctx.table.var_unification_table.find(inner); |
68 | let position = self.add(InferenceVar::from_inner(root), kind); | 68 | let position = self.add(to_inference_var(root), kind); |
69 | TyKind::BoundVar(BoundVar::new(binders, position)).intern(&Interner) | 69 | TyKind::BoundVar(BoundVar::new(binders, position)).intern(&Interner) |
70 | } | 70 | } |
71 | } | 71 | } |
@@ -207,16 +207,16 @@ impl TypeVariableTable { | |||
207 | } | 207 | } |
208 | 208 | ||
209 | pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) { | 209 | pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) { |
210 | self.inner[iv.to_inner().0 as usize].diverging = diverging; | 210 | self.inner[from_inference_var(iv).0 as usize].diverging = diverging; |
211 | } | 211 | } |
212 | 212 | ||
213 | fn is_diverging(&mut self, iv: InferenceVar) -> bool { | 213 | fn is_diverging(&mut self, iv: InferenceVar) -> bool { |
214 | self.inner[iv.to_inner().0 as usize].diverging | 214 | self.inner[from_inference_var(iv).0 as usize].diverging |
215 | } | 215 | } |
216 | 216 | ||
217 | fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty { | 217 | fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty { |
218 | match kind { | 218 | match kind { |
219 | _ if self.inner[iv.to_inner().0 as usize].diverging => TyKind::Never, | 219 | _ if self.inner[from_inference_var(iv).0 as usize].diverging => TyKind::Never, |
220 | TyVariableKind::General => TyKind::Error, | 220 | TyVariableKind::General => TyKind::Error, |
221 | TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)), | 221 | TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)), |
222 | TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)), | 222 | TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)), |
@@ -250,7 +250,7 @@ impl InferenceTable { | |||
250 | self.type_variable_table.push(TypeVariableData { diverging }); | 250 | self.type_variable_table.push(TypeVariableData { diverging }); |
251 | let key = self.var_unification_table.new_key(TypeVarValue::Unknown); | 251 | let key = self.var_unification_table.new_key(TypeVarValue::Unknown); |
252 | assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); | 252 | assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); |
253 | TyKind::InferenceVar(InferenceVar::from_inner(key), kind).intern(&Interner) | 253 | TyKind::InferenceVar(to_inference_var(key), kind).intern(&Interner) |
254 | } | 254 | } |
255 | 255 | ||
256 | pub(crate) fn new_type_var(&mut self) -> Ty { | 256 | pub(crate) fn new_type_var(&mut self) -> Ty { |
@@ -369,8 +369,12 @@ impl InferenceTable { | |||
369 | == self.type_variable_table.is_diverging(*tv2) => | 369 | == self.type_variable_table.is_diverging(*tv2) => |
370 | { | 370 | { |
371 | // both type vars are unknown since we tried to resolve them | 371 | // both type vars are unknown since we tried to resolve them |
372 | if !self.var_unification_table.unioned(tv1.to_inner(), tv2.to_inner()) { | 372 | if !self |
373 | self.var_unification_table.union(tv1.to_inner(), tv2.to_inner()); | 373 | .var_unification_table |
374 | .unioned(from_inference_var(*tv1), from_inference_var(*tv2)) | ||
375 | { | ||
376 | self.var_unification_table | ||
377 | .union(from_inference_var(*tv1), from_inference_var(*tv2)); | ||
374 | self.revision += 1; | 378 | self.revision += 1; |
375 | } | 379 | } |
376 | true | 380 | true |
@@ -407,7 +411,7 @@ impl InferenceTable { | |||
407 | ) => { | 411 | ) => { |
408 | // the type var is unknown since we tried to resolve it | 412 | // the type var is unknown since we tried to resolve it |
409 | self.var_unification_table.union_value( | 413 | self.var_unification_table.union_value( |
410 | tv.to_inner(), | 414 | from_inference_var(*tv), |
411 | TypeVarValue::Known(other.clone().intern(&Interner)), | 415 | TypeVarValue::Known(other.clone().intern(&Interner)), |
412 | ); | 416 | ); |
413 | self.revision += 1; | 417 | self.revision += 1; |
@@ -462,7 +466,7 @@ impl InferenceTable { | |||
462 | } | 466 | } |
463 | match ty.kind(&Interner) { | 467 | match ty.kind(&Interner) { |
464 | TyKind::InferenceVar(tv, _) => { | 468 | TyKind::InferenceVar(tv, _) => { |
465 | let inner = tv.to_inner(); | 469 | let inner = from_inference_var(*tv); |
466 | match self.var_unification_table.inlined_probe_value(inner).known() { | 470 | match self.var_unification_table.inlined_probe_value(inner).known() { |
467 | Some(known_ty) => { | 471 | Some(known_ty) => { |
468 | // The known_ty can't be a type var itself | 472 | // The known_ty can't be a type var itself |
@@ -485,7 +489,7 @@ impl InferenceTable { | |||
485 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 489 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
486 | ty.fold(&mut |ty| match ty.kind(&Interner) { | 490 | ty.fold(&mut |ty| match ty.kind(&Interner) { |
487 | &TyKind::InferenceVar(tv, kind) => { | 491 | &TyKind::InferenceVar(tv, kind) => { |
488 | let inner = tv.to_inner(); | 492 | let inner = from_inference_var(tv); |
489 | if tv_stack.contains(&inner) { | 493 | if tv_stack.contains(&inner) { |
490 | cov_mark::hit!(type_var_cycles_resolve_as_possible); | 494 | cov_mark::hit!(type_var_cycles_resolve_as_possible); |
491 | // recursive type | 495 | // recursive type |
@@ -512,7 +516,7 @@ impl InferenceTable { | |||
512 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 516 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
513 | ty.fold(&mut |ty| match ty.kind(&Interner) { | 517 | ty.fold(&mut |ty| match ty.kind(&Interner) { |
514 | &TyKind::InferenceVar(tv, kind) => { | 518 | &TyKind::InferenceVar(tv, kind) => { |
515 | let inner = tv.to_inner(); | 519 | let inner = from_inference_var(tv); |
516 | if tv_stack.contains(&inner) { | 520 | if tv_stack.contains(&inner) { |
517 | cov_mark::hit!(type_var_cycles_resolve_completely); | 521 | cov_mark::hit!(type_var_cycles_resolve_completely); |
518 | // recursive type | 522 | // recursive type |
@@ -555,6 +559,14 @@ impl UnifyKey for TypeVarId { | |||
555 | } | 559 | } |
556 | } | 560 | } |
557 | 561 | ||
562 | fn from_inference_var(var: InferenceVar) -> TypeVarId { | ||
563 | TypeVarId(var.index()) | ||
564 | } | ||
565 | |||
566 | fn to_inference_var(TypeVarId(index): TypeVarId) -> InferenceVar { | ||
567 | index.into() | ||
568 | } | ||
569 | |||
558 | /// The value of a type variable: either we already know the type, or we don't | 570 | /// The value of a type variable: either we already know the type, or we don't |
559 | /// know it yet. | 571 | /// know it yet. |
560 | #[derive(Clone, PartialEq, Eq, Debug)] | 572 | #[derive(Clone, PartialEq, Eq, Debug)] |
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index f5b658cba..5c83a508d 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -42,7 +42,7 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; | |||
42 | pub use autoderef::autoderef; | 42 | pub use autoderef::autoderef; |
43 | pub use builder::TyBuilder; | 43 | pub use builder::TyBuilder; |
44 | pub use chalk_ext::{ProjectionTyExt, TyExt}; | 44 | pub use chalk_ext::{ProjectionTyExt, TyExt}; |
45 | pub use infer::{could_unify, InferenceResult, InferenceVar}; | 45 | pub use infer::{could_unify, InferenceResult}; |
46 | pub use lower::{ | 46 | pub use lower::{ |
47 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 47 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
48 | TyDefId, TyLoweringContext, ValueTyDefId, | 48 | TyDefId, TyLoweringContext, ValueTyDefId, |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index cf73cb078..0536b934e 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -10,10 +10,9 @@ use base_db::salsa::InternKey; | |||
10 | use hir_def::{GenericDefId, TypeAliasId}; | 10 | use hir_def::{GenericDefId, TypeAliasId}; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | chalk_ext::ProjectionTyExt, db::HirDatabase, dummy_usize_const, static_lifetime, AliasTy, | 13 | chalk_ext::ProjectionTyExt, db::HirDatabase, static_lifetime, AliasTy, CallableDefId, |
14 | CallableDefId, Canonical, Const, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, | 14 | Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, |
15 | OpaqueTy, ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, | 15 | QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause, |
16 | WhereClause, | ||
17 | }; | 16 | }; |
18 | 17 | ||
19 | use super::interner::*; | 18 | use super::interner::*; |
@@ -23,16 +22,16 @@ impl ToChalk for Ty { | |||
23 | type Chalk = chalk_ir::Ty<Interner>; | 22 | type Chalk = chalk_ir::Ty<Interner>; |
24 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 23 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
25 | match self.into_inner() { | 24 | match self.into_inner() { |
26 | TyKind::Ref(m, lt, ty) => ref_to_chalk(db, m, lt, ty), | 25 | TyKind::Ref(m, lt, ty) => { |
27 | TyKind::Array(ty, size) => array_to_chalk(db, ty, size), | 26 | chalk_ir::TyKind::Ref(m, lt, ty.to_chalk(db)).intern(&Interner) |
28 | TyKind::Function(FnPointer { sig, substitution: substs, .. }) => { | 27 | } |
28 | TyKind::Array(ty, size) => { | ||
29 | chalk_ir::TyKind::Array(ty.to_chalk(db), size).intern(&Interner) | ||
30 | } | ||
31 | TyKind::Function(FnPointer { sig, substitution: substs, num_binders }) => { | ||
29 | let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db)); | 32 | let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db)); |
30 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 33 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, sig, substitution }) |
31 | num_binders: 0, | 34 | .intern(&Interner) |
32 | sig, | ||
33 | substitution, | ||
34 | }) | ||
35 | .intern(&Interner) | ||
36 | } | 35 | } |
37 | TyKind::AssociatedType(assoc_type_id, substs) => { | 36 | TyKind::AssociatedType(assoc_type_id, substs) => { |
38 | let substitution = substs.to_chalk(db); | 37 | let substitution = substs.to_chalk(db); |
@@ -74,22 +73,13 @@ impl ToChalk for Ty { | |||
74 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) | 73 | chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) |
75 | } | 74 | } |
76 | TyKind::Alias(AliasTy::Projection(proj_ty)) => { | 75 | TyKind::Alias(AliasTy::Projection(proj_ty)) => { |
77 | let associated_ty_id = proj_ty.associated_ty_id; | 76 | chalk_ir::AliasTy::Projection(proj_ty.to_chalk(db)) |
78 | let substitution = proj_ty.substitution.to_chalk(db); | ||
79 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | ||
80 | associated_ty_id, | ||
81 | substitution, | ||
82 | }) | ||
83 | .cast(&Interner) | ||
84 | .intern(&Interner) | ||
85 | } | ||
86 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
87 | let opaque_ty_id = opaque_ty.opaque_ty_id; | ||
88 | let substitution = opaque_ty.substitution.to_chalk(db); | ||
89 | chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution }) | ||
90 | .cast(&Interner) | 77 | .cast(&Interner) |
91 | .intern(&Interner) | 78 | .intern(&Interner) |
92 | } | 79 | } |
80 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
81 | chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)).cast(&Interner).intern(&Interner) | ||
82 | } | ||
93 | TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner), | 83 | TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner), |
94 | TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), | 84 | TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), |
95 | TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), | 85 | TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), |
@@ -101,7 +91,7 @@ impl ToChalk for Ty { | |||
101 | ); | 91 | ); |
102 | let bounded_ty = chalk_ir::DynTy { | 92 | let bounded_ty = chalk_ir::DynTy { |
103 | bounds: chalk_ir::Binders::new(binders, where_clauses), | 93 | bounds: chalk_ir::Binders::new(binders, where_clauses), |
104 | lifetime: static_lifetime(), | 94 | lifetime: dyn_ty.lifetime, |
105 | }; | 95 | }; |
106 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) | 96 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) |
107 | } | 97 | } |
@@ -114,17 +104,10 @@ impl ToChalk for Ty { | |||
114 | chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size), | 104 | chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size), |
115 | chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), | 105 | chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), |
116 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { | 106 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { |
117 | let associated_ty = proj.associated_ty_id; | 107 | TyKind::Alias(AliasTy::Projection(from_chalk(db, proj))) |
118 | let parameters = from_chalk(db, proj.substitution); | ||
119 | TyKind::Alias(AliasTy::Projection(ProjectionTy { | ||
120 | associated_ty_id: associated_ty, | ||
121 | substitution: parameters, | ||
122 | })) | ||
123 | } | 108 | } |
124 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { | 109 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { |
125 | let opaque_ty_id = opaque_ty.opaque_ty_id; | 110 | TyKind::Alias(AliasTy::Opaque(from_chalk(db, opaque_ty))) |
126 | let parameters = from_chalk(db, opaque_ty.substitution); | ||
127 | TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters })) | ||
128 | } | 111 | } |
129 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 112 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
130 | num_binders, | 113 | num_binders, |
@@ -138,18 +121,19 @@ impl ToChalk for Ty { | |||
138 | } | 121 | } |
139 | chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), | 122 | chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), |
140 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error, | 123 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error, |
141 | chalk_ir::TyKind::Dyn(where_clauses) => { | 124 | chalk_ir::TyKind::Dyn(dyn_ty) => { |
142 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | 125 | assert_eq!(dyn_ty.bounds.binders.len(&Interner), 1); |
143 | let bounds = where_clauses | 126 | let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders(); |
144 | .bounds | 127 | let where_clauses = crate::QuantifiedWhereClauses::from_iter( |
145 | .skip_binders() | 128 | &Interner, |
146 | .iter(&Interner) | 129 | bounds.interned().iter().cloned().map(|p| from_chalk(db, p)), |
147 | .map(|c| from_chalk(db, c.clone())); | 130 | ); |
148 | TyKind::Dyn(crate::DynTy { | 131 | TyKind::Dyn(crate::DynTy { |
149 | bounds: crate::Binders::new( | 132 | bounds: crate::Binders::new(binders, where_clauses), |
150 | where_clauses.bounds.binders.clone(), | 133 | // HACK: we sometimes get lifetime variables back in solutions |
151 | crate::QuantifiedWhereClauses::from_iter(&Interner, bounds), | 134 | // from Chalk, and don't have the infrastructure to substitute |
152 | ), | 135 | // them yet. So for now we just turn them into 'static right |
136 | // when we get them | ||
153 | lifetime: static_lifetime(), | 137 | lifetime: static_lifetime(), |
154 | }) | 138 | }) |
155 | } | 139 | } |
@@ -169,8 +153,12 @@ impl ToChalk for Ty { | |||
169 | } | 153 | } |
170 | chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), | 154 | chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), |
171 | chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), | 155 | chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), |
172 | chalk_ir::TyKind::Ref(mutability, lifetime, ty) => { | 156 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { |
173 | TyKind::Ref(mutability, lifetime, from_chalk(db, ty)) | 157 | // HACK: we sometimes get lifetime variables back in solutions |
158 | // from Chalk, and don't have the infrastructure to substitute | ||
159 | // them yet. So for now we just turn them into 'static right | ||
160 | // when we get them | ||
161 | TyKind::Ref(mutability, static_lifetime(), from_chalk(db, ty)) | ||
174 | } | 162 | } |
175 | chalk_ir::TyKind::Str => TyKind::Str, | 163 | chalk_ir::TyKind::Str => TyKind::Str, |
176 | chalk_ir::TyKind::Never => TyKind::Never, | 164 | chalk_ir::TyKind::Never => TyKind::Never, |
@@ -189,26 +177,6 @@ impl ToChalk for Ty { | |||
189 | } | 177 | } |
190 | } | 178 | } |
191 | 179 | ||
192 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | ||
193 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. | ||
194 | fn ref_to_chalk( | ||
195 | db: &dyn HirDatabase, | ||
196 | mutability: chalk_ir::Mutability, | ||
197 | _lifetime: Lifetime, | ||
198 | ty: Ty, | ||
199 | ) -> chalk_ir::Ty<Interner> { | ||
200 | let arg = ty.to_chalk(db); | ||
201 | let lifetime = static_lifetime(); | ||
202 | chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) | ||
203 | } | ||
204 | |||
205 | /// We currently don't model constants, but Chalk does. So, we have to insert a | ||
206 | /// fake constant here, because Chalks built-in logic may expect it to be there. | ||
207 | fn array_to_chalk(db: &dyn HirDatabase, ty: Ty, _: Const) -> chalk_ir::Ty<Interner> { | ||
208 | let arg = ty.to_chalk(db); | ||
209 | chalk_ir::TyKind::Array(arg, dummy_usize_const()).intern(&Interner) | ||
210 | } | ||
211 | |||
212 | impl ToChalk for GenericArg { | 180 | impl ToChalk for GenericArg { |
213 | type Chalk = chalk_ir::GenericArg<Interner>; | 181 | type Chalk = chalk_ir::GenericArg<Interner>; |
214 | 182 | ||
diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index 89c0ddd1a..eac1b7900 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs | |||
@@ -11,8 +11,7 @@ use smallvec::SmallVec; | |||
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, Const, FnDefId, FnSig, ForeignDefId, | 13 | AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, Const, FnDefId, FnSig, ForeignDefId, |
14 | InferenceVar, Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, | 14 | Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, VariableKinds, |
15 | VariableKinds, | ||
16 | }; | 15 | }; |
17 | 16 | ||
18 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 17 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
@@ -524,3 +523,25 @@ pub enum Guidance { | |||
524 | /// There's no useful information to feed back to type inference | 523 | /// There's no useful information to feed back to type inference |
525 | Unknown, | 524 | Unknown, |
526 | } | 525 | } |
526 | |||
527 | /// The kinds of placeholders we need during type inference. There's separate | ||
528 | /// values for general types, and for integer and float variables. The latter | ||
529 | /// two are used for inference of literal values (e.g. `100` could be one of | ||
530 | /// several integer types). | ||
531 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] | ||
532 | pub struct InferenceVar { | ||
533 | index: u32, | ||
534 | } | ||
535 | |||
536 | impl From<u32> for InferenceVar { | ||
537 | fn from(index: u32) -> InferenceVar { | ||
538 | InferenceVar { index } | ||
539 | } | ||
540 | } | ||
541 | |||
542 | impl InferenceVar { | ||
543 | /// Gets the underlying index value. | ||
544 | pub fn index(self) -> u32 { | ||
545 | self.index | ||
546 | } | ||
547 | } | ||