diff options
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index b4b338874..de5ec5b46 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -107,9 +107,9 @@ impl UnifyValue for TypeVarValue { | |||
107 | } | 107 | } |
108 | } | 108 | } |
109 | 109 | ||
110 | /// The kinds of placeholders we need during type inference. Currently, we only | 110 | /// The kinds of placeholders we need during type inference. There's seperate |
111 | /// have type variables; in the future, we will probably also need int and float | 111 | /// values for general types, and for integer and float variables. The latter |
112 | /// variables, for inference of literal values (e.g. `100` could be one of | 112 | /// two are used for inference of literal values (e.g. `100` could be one of |
113 | /// several integer types). | 113 | /// several integer types). |
114 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] | 114 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] |
115 | pub enum InferTy { | 115 | pub enum InferTy { |
@@ -118,6 +118,20 @@ pub enum InferTy { | |||
118 | FloatVar(TypeVarId), | 118 | FloatVar(TypeVarId), |
119 | } | 119 | } |
120 | 120 | ||
121 | impl InferTy { | ||
122 | fn fallback_value(self) -> Ty { | ||
123 | match self { | ||
124 | InferTy::TypeVar(..) => Ty::Unknown, | ||
125 | InferTy::IntVar(..) => { | ||
126 | Ty::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32)) | ||
127 | } | ||
128 | InferTy::FloatVar(..) => { | ||
129 | Ty::Float(primitive::UncertainFloatTy::Known(primitive::FloatTy::F64)) | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | |||
121 | /// When inferring an expression, we propagate downward whatever type hint we | 135 | /// When inferring an expression, we propagate downward whatever type hint we |
122 | /// are able in the form of an `Expectation`. | 136 | /// are able in the form of an `Expectation`. |
123 | #[derive(Clone, PartialEq, Eq, Debug)] | 137 | #[derive(Clone, PartialEq, Eq, Debug)] |
@@ -156,8 +170,6 @@ pub enum Ty { | |||
156 | /// A primitive integer type. For example, `i32`. | 170 | /// A primitive integer type. For example, `i32`. |
157 | Int(primitive::UncertainIntTy), | 171 | Int(primitive::UncertainIntTy), |
158 | 172 | ||
159 | // /// A primitive unsigned integer type. For example, `u32`. | ||
160 | // Uint(primitive::UintTy), | ||
161 | /// A primitive floating-point type. For example, `f64`. | 173 | /// A primitive floating-point type. For example, `f64`. |
162 | Float(primitive::UncertainFloatTy), | 174 | Float(primitive::UncertainFloatTy), |
163 | 175 | ||
@@ -199,8 +211,9 @@ pub enum Ty { | |||
199 | // above function pointer type. Once we implement generics, we will probably | 211 | // above function pointer type. Once we implement generics, we will probably |
200 | // need this as well. | 212 | // need this as well. |
201 | 213 | ||
202 | // A trait, defined with `dyn trait`. | 214 | // A trait, defined with `dyn Trait`. |
203 | // Dynamic(), | 215 | // Dynamic(), |
216 | |||
204 | // The anonymous type of a closure. Used to represent the type of | 217 | // The anonymous type of a closure. Used to represent the type of |
205 | // `|a| a`. | 218 | // `|a| a`. |
206 | // Closure(DefId, ClosureSubsts<'tcx>), | 219 | // Closure(DefId, ClosureSubsts<'tcx>), |
@@ -824,11 +837,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
824 | // known_ty may contain other variables that are known by now | 837 | // known_ty may contain other variables that are known by now |
825 | self.resolve_ty_completely(known_ty.clone()) | 838 | self.resolve_ty_completely(known_ty.clone()) |
826 | } else { | 839 | } else { |
827 | match i { | 840 | i.fallback_value() |
828 | InferTy::TypeVar(..) => Ty::Unknown, | ||
829 | InferTy::IntVar(..) => Ty::Int(primitive::UncertainIntTy::Unknown), | ||
830 | InferTy::FloatVar(..) => Ty::Float(primitive::UncertainFloatTy::Unknown), | ||
831 | } | ||
832 | } | 841 | } |
833 | } | 842 | } |
834 | _ => ty, | 843 | _ => ty, |
@@ -1111,24 +1120,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1111 | Ty::Ref(slice_type, Mutability::Shared) | 1120 | Ty::Ref(slice_type, Mutability::Shared) |
1112 | } | 1121 | } |
1113 | Literal::Char(..) => Ty::Char, | 1122 | Literal::Char(..) => Ty::Char, |
1114 | Literal::Tuple { values } => { | ||
1115 | let mut inner_tys = Vec::new(); | ||
1116 | for &expr in values { | ||
1117 | let inner_ty = self.infer_expr(expr, &Expectation::none())?; | ||
1118 | inner_tys.push(inner_ty); | ||
1119 | } | ||
1120 | Ty::Tuple(Arc::from(inner_tys)) | ||
1121 | } | ||
1122 | Literal::Array { values } => { | ||
1123 | // simply take the type of the first element for now | ||
1124 | let inner_ty = match values.get(0) { | ||
1125 | Some(&expr) => self.infer_expr(expr, &Expectation::none())?, | ||
1126 | None => Ty::Unknown, | ||
1127 | }; | ||
1128 | // TODO: we should return a Ty::Array when it becomes | ||
1129 | // available | ||
1130 | Ty::Slice(Arc::new(inner_ty)) | ||
1131 | } | ||
1132 | Literal::Int(_v, ty) => Ty::Int(*ty), | 1123 | Literal::Int(_v, ty) => Ty::Int(*ty), |
1133 | Literal::Float(_v, ty) => Ty::Float(*ty), | 1124 | Literal::Float(_v, ty) => Ty::Float(*ty), |
1134 | }, | 1125 | }, |