mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
don't try to treat arrays and tuples as literals
This commit is contained in:
parent
1574715be5
commit
81bc8e4973
5 changed files with 27 additions and 57 deletions
|
@ -115,8 +115,6 @@ pub enum Literal {
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Int(u64, UncertainIntTy),
|
Int(u64, UncertainIntTy),
|
||||||
Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq
|
Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq
|
||||||
Tuple { values: Vec<ExprId> },
|
|
||||||
Array { values: Vec<ExprId> },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
@ -322,14 +320,7 @@ impl Expr {
|
||||||
f(*expr);
|
f(*expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::Literal(l) => match l {
|
Expr::Literal(_) => {}
|
||||||
Literal::Array { values } | Literal::Tuple { values } => {
|
|
||||||
for &val in values {
|
|
||||||
f(val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -720,14 +711,6 @@ impl ExprCollector {
|
||||||
let text = c.text().to_string();
|
let text = c.text().to_string();
|
||||||
Literal::String(text)
|
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::TRUE_KW => Literal::Bool(true),
|
||||||
SyntaxKind::FALSE_KW => Literal::Bool(false),
|
SyntaxKind::FALSE_KW => Literal::Bool(false),
|
||||||
SyntaxKind::BYTE_STRING => {
|
SyntaxKind::BYTE_STRING => {
|
||||||
|
|
|
@ -107,9 +107,9 @@ impl UnifyValue for TypeVarValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The kinds of placeholders we need during type inference. Currently, we only
|
/// The kinds of placeholders we need during type inference. There's seperate
|
||||||
/// have type variables; in the future, we will probably also need int and float
|
/// values for general types, and for integer and float variables. The latter
|
||||||
/// variables, for inference of literal values (e.g. `100` could be one of
|
/// two are used for inference of literal values (e.g. `100` could be one of
|
||||||
/// several integer types).
|
/// several integer types).
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub enum InferTy {
|
pub enum InferTy {
|
||||||
|
@ -118,6 +118,20 @@ pub enum InferTy {
|
||||||
FloatVar(TypeVarId),
|
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
|
/// When inferring an expression, we propagate downward whatever type hint we
|
||||||
/// are able in the form of an `Expectation`.
|
/// are able in the form of an `Expectation`.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
|
@ -156,8 +170,6 @@ pub enum Ty {
|
||||||
/// A primitive integer type. For example, `i32`.
|
/// A primitive integer type. For example, `i32`.
|
||||||
Int(primitive::UncertainIntTy),
|
Int(primitive::UncertainIntTy),
|
||||||
|
|
||||||
// /// A primitive unsigned integer type. For example, `u32`.
|
|
||||||
// Uint(primitive::UintTy),
|
|
||||||
/// A primitive floating-point type. For example, `f64`.
|
/// A primitive floating-point type. For example, `f64`.
|
||||||
Float(primitive::UncertainFloatTy),
|
Float(primitive::UncertainFloatTy),
|
||||||
|
|
||||||
|
@ -199,8 +211,9 @@ pub enum Ty {
|
||||||
// above function pointer type. Once we implement generics, we will probably
|
// above function pointer type. Once we implement generics, we will probably
|
||||||
// need this as well.
|
// need this as well.
|
||||||
|
|
||||||
// A trait, defined with `dyn trait`.
|
// A trait, defined with `dyn Trait`.
|
||||||
// Dynamic(),
|
// Dynamic(),
|
||||||
|
|
||||||
// The anonymous type of a closure. Used to represent the type of
|
// The anonymous type of a closure. Used to represent the type of
|
||||||
// `|a| a`.
|
// `|a| a`.
|
||||||
// Closure(DefId, ClosureSubsts<'tcx>),
|
// 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
|
// known_ty may contain other variables that are known by now
|
||||||
self.resolve_ty_completely(known_ty.clone())
|
self.resolve_ty_completely(known_ty.clone())
|
||||||
} else {
|
} else {
|
||||||
match i {
|
i.fallback_value()
|
||||||
InferTy::TypeVar(..) => Ty::Unknown,
|
|
||||||
InferTy::IntVar(..) => Ty::Int(primitive::UncertainIntTy::Unknown),
|
|
||||||
InferTy::FloatVar(..) => Ty::Float(primitive::UncertainFloatTy::Unknown),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => ty,
|
_ => ty,
|
||||||
|
@ -1111,24 +1120,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
Ty::Ref(slice_type, Mutability::Shared)
|
Ty::Ref(slice_type, Mutability::Shared)
|
||||||
}
|
}
|
||||||
Literal::Char(..) => Ty::Char,
|
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::Int(_v, ty) => Ty::Int(*ty),
|
||||||
Literal::Float(_v, ty) => Ty::Float(*ty),
|
Literal::Float(_v, ty) => Ty::Float(*ty),
|
||||||
},
|
},
|
||||||
|
|
|
@ -144,9 +144,7 @@ fn test() {
|
||||||
b'b';
|
b'b';
|
||||||
3.14;
|
3.14;
|
||||||
5000;
|
5000;
|
||||||
(0u32, -5isize);
|
|
||||||
false;
|
false;
|
||||||
[true, true, false]
|
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
"literals.txt",
|
"literals.txt",
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
[11; 146) '{ ...lse] }': ()
|
[11; 101) '{ ...lse; }': ()
|
||||||
[17; 21) '5i32': i32
|
[17; 21) '5i32': i32
|
||||||
[27; 34) '"hello"': &str
|
[27; 34) '"hello"': &str
|
||||||
[40; 48) 'b"bytes"': &[u8]
|
[40; 48) 'b"bytes"': &[u8]
|
||||||
[54; 57) ''c'': char
|
[54; 57) ''c'': char
|
||||||
[63; 67) 'b'b'': u8
|
[63; 67) 'b'b'': u8
|
||||||
[73; 77) '3.14': {float}
|
[73; 77) '3.14': f64
|
||||||
[83; 87) '5000': {integer}
|
[83; 87) '5000': i32
|
||||||
[93; 108) '(0u32, -5isize)': [unknown]
|
[93; 98) 'false': bool
|
||||||
[114; 119) 'false': bool
|
|
||||||
[125; 144) '[true,...false]': ()
|
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
[82; 83) 'c': [unknown]
|
[82; 83) 'c': [unknown]
|
||||||
[86; 87) 'C': [unknown]
|
[86; 87) 'C': [unknown]
|
||||||
[86; 90) 'C(1)': [unknown]
|
[86; 90) 'C(1)': [unknown]
|
||||||
[88; 89) '1': {integer}
|
[88; 89) '1': i32
|
||||||
[96; 97) 'B': [unknown]
|
[96; 97) 'B': [unknown]
|
||||||
[107; 108) 'a': A
|
[107; 108) 'a': A
|
||||||
[114; 133) 'A { b:...C(1) }': A
|
[114; 133) 'A { b:...C(1) }': A
|
||||||
[121; 122) 'B': B
|
[121; 122) 'B': B
|
||||||
[127; 128) 'C': [unknown]
|
[127; 128) 'C': [unknown]
|
||||||
[127; 131) 'C(1)': C
|
[127; 131) 'C(1)': C
|
||||||
[129; 130) '1': {integer}
|
[129; 130) '1': i32
|
||||||
[139; 140) 'a': A
|
[139; 140) 'a': A
|
||||||
[139; 142) 'a.b': B
|
[139; 142) 'a.b': B
|
||||||
[148; 149) 'a': A
|
[148; 149) 'a': A
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue