From 81bc8e4973fefd0ff31d08206c374fb58aa8b6e0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Fri, 11 Jan 2019 09:47:31 +0100 Subject: don't try to treat arrays and tuples as literals --- crates/ra_hir/src/expr.rs | 19 +---------- crates/ra_hir/src/ty.rs | 49 ++++++++++++---------------- crates/ra_hir/src/ty/tests.rs | 2 -- crates/ra_hir/src/ty/tests/data/literals.txt | 10 +++--- crates/ra_hir/src/ty/tests/data/struct.txt | 4 +-- 5 files changed, 27 insertions(+), 57 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 2798937a6..52af2af45 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -115,8 +115,6 @@ pub enum Literal { Bool(bool), Int(u64, UncertainIntTy), Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq - Tuple { values: Vec }, - Array { values: Vec }, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -322,14 +320,7 @@ impl Expr { f(*expr); } } - Expr::Literal(l) => match l { - Literal::Array { values } | Literal::Tuple { values } => { - for &val in values { - f(val); - } - } - _ => {} - }, + Expr::Literal(_) => {} } } } @@ -720,14 +711,6 @@ impl ExprCollector { let text = c.text().to_string(); Literal::String(text) } - SyntaxKind::ARRAY_EXPR => { - // TODO: recursively call to self - Literal::Array { values: vec![] } - } - SyntaxKind::PAREN_EXPR => { - // TODO: recursively call to self - Literal::Tuple { values: vec![] } - } SyntaxKind::TRUE_KW => Literal::Bool(true), SyntaxKind::FALSE_KW => Literal::Bool(false), SyntaxKind::BYTE_STRING => { 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 { } } -/// The kinds of placeholders we need during type inference. Currently, we only -/// have type variables; in the future, we will probably also need int and float -/// variables, for inference of literal values (e.g. `100` could be one of +/// The kinds of placeholders we need during type inference. There's seperate +/// values for general types, and for integer and float variables. The latter +/// two are used for inference of literal values (e.g. `100` could be one of /// several integer types). #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub enum InferTy { @@ -118,6 +118,20 @@ pub enum InferTy { FloatVar(TypeVarId), } +impl InferTy { + fn fallback_value(self) -> Ty { + match self { + InferTy::TypeVar(..) => Ty::Unknown, + InferTy::IntVar(..) => { + Ty::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32)) + } + InferTy::FloatVar(..) => { + Ty::Float(primitive::UncertainFloatTy::Known(primitive::FloatTy::F64)) + } + } + } +} + /// When inferring an expression, we propagate downward whatever type hint we /// are able in the form of an `Expectation`. #[derive(Clone, PartialEq, Eq, Debug)] @@ -156,8 +170,6 @@ pub enum Ty { /// A primitive integer type. For example, `i32`. Int(primitive::UncertainIntTy), - // /// A primitive unsigned integer type. For example, `u32`. - // Uint(primitive::UintTy), /// A primitive floating-point type. For example, `f64`. Float(primitive::UncertainFloatTy), @@ -199,8 +211,9 @@ pub enum Ty { // above function pointer type. Once we implement generics, we will probably // need this as well. - // A trait, defined with `dyn trait`. + // A trait, defined with `dyn Trait`. // Dynamic(), + // The anonymous type of a closure. Used to represent the type of // `|a| a`. // Closure(DefId, ClosureSubsts<'tcx>), @@ -824,11 +837,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // known_ty may contain other variables that are known by now self.resolve_ty_completely(known_ty.clone()) } else { - match i { - InferTy::TypeVar(..) => Ty::Unknown, - InferTy::IntVar(..) => Ty::Int(primitive::UncertainIntTy::Unknown), - InferTy::FloatVar(..) => Ty::Float(primitive::UncertainFloatTy::Unknown), - } + i.fallback_value() } } _ => ty, @@ -1111,24 +1120,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Ty::Ref(slice_type, Mutability::Shared) } Literal::Char(..) => Ty::Char, - Literal::Tuple { values } => { - let mut inner_tys = Vec::new(); - for &expr in values { - let inner_ty = self.infer_expr(expr, &Expectation::none())?; - inner_tys.push(inner_ty); - } - Ty::Tuple(Arc::from(inner_tys)) - } - Literal::Array { values } => { - // simply take the type of the first element for now - let inner_ty = match values.get(0) { - Some(&expr) => self.infer_expr(expr, &Expectation::none())?, - None => Ty::Unknown, - }; - // TODO: we should return a Ty::Array when it becomes - // available - Ty::Slice(Arc::new(inner_ty)) - } Literal::Int(_v, ty) => Ty::Int(*ty), Literal::Float(_v, ty) => Ty::Float(*ty), }, diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 0c43415a6..53ea99874 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -144,9 +144,7 @@ fn test() { b'b'; 3.14; 5000; - (0u32, -5isize); false; - [true, true, false] } "#, "literals.txt", diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index df435edd7..8d6079c98 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,11 +1,9 @@ -[11; 146) '{ ...lse] }': () +[11; 101) '{ ...lse; }': () [17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] [54; 57) ''c'': char [63; 67) 'b'b'': u8 -[73; 77) '3.14': {float} -[83; 87) '5000': {integer} -[93; 108) '(0u32, -5isize)': [unknown] -[114; 119) 'false': bool -[125; 144) '[true,...false]': () +[73; 77) '3.14': f64 +[83; 87) '5000': i32 +[93; 98) 'false': bool diff --git a/crates/ra_hir/src/ty/tests/data/struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt index dcdf61363..be9e12d02 100644 --- a/crates/ra_hir/src/ty/tests/data/struct.txt +++ b/crates/ra_hir/src/ty/tests/data/struct.txt @@ -2,14 +2,14 @@ [82; 83) 'c': [unknown] [86; 87) 'C': [unknown] [86; 90) 'C(1)': [unknown] -[88; 89) '1': {integer} +[88; 89) '1': i32 [96; 97) 'B': [unknown] [107; 108) 'a': A [114; 133) 'A { b:...C(1) }': A [121; 122) 'B': B [127; 128) 'C': [unknown] [127; 131) 'C(1)': C -[129; 130) '1': {integer} +[129; 130) '1': i32 [139; 140) 'a': A [139; 142) 'a.b': B [148; 149) 'a': A -- cgit v1.2.3