don't try to treat arrays and tuples as literals

This commit is contained in:
Marcus Klaas de Vries 2019-01-11 09:47:31 +01:00
parent 1574715be5
commit 81bc8e4973
5 changed files with 27 additions and 57 deletions

View file

@ -115,8 +115,6 @@ pub enum Literal {
Bool(bool),
Int(u64, UncertainIntTy),
Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq
Tuple { values: Vec<ExprId> },
Array { values: Vec<ExprId> },
}
#[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 => {

View file

@ -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),
},

View file

@ -144,9 +144,7 @@ fn test() {
b'b';
3.14;
5000;
(0u32, -5isize);
false;
[true, true, false]
}
"#,
"literals.txt",

View file

@ -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

View file

@ -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