diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-14 20:58:20 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-14 20:58:20 +0000 |
commit | e8e82ce032f8678929b015e6f70ac060bb2cf94c (patch) | |
tree | 9fb158e9f7115bb70cf2b8623b70710c55497ed4 /crates/ra_hir/src/ty/primitive.rs | |
parent | 784ff638e549a27503b719e5c2f0009b40d25364 (diff) | |
parent | 37ba237e6686d94783d1f025d23823ad7c0cb0c8 (diff) |
Merge #485
485: Add type inference for a bunch of primitives r=flodiebold a=marcusklaas
This PR adds inference for `&str`, `&[u8]`, `char`, `bool`, floats and integers. For floats and integers it uses type variables to infer the exact type, i.e. `u32`, from context when it's not annotated explicitly.
I'm not quite happy with the implementation yet, but I think it mostly works now.
Co-authored-by: Marcus Klaas de Vries <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/primitive.rs')
-rw-r--r-- | crates/ra_hir/src/ty/primitive.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 498d42d52..5741ca90d 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs | |||
@@ -2,6 +2,56 @@ use std::fmt; | |||
2 | 2 | ||
3 | use crate::{Name, KnownName}; | 3 | use crate::{Name, KnownName}; |
4 | 4 | ||
5 | #[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] | ||
6 | pub enum UncertainIntTy { | ||
7 | Unknown, | ||
8 | Unsigned(UintTy), | ||
9 | Signed(IntTy), | ||
10 | } | ||
11 | |||
12 | impl UncertainIntTy { | ||
13 | pub fn ty_to_string(&self) -> &'static str { | ||
14 | match *self { | ||
15 | UncertainIntTy::Unknown => "{integer}", | ||
16 | UncertainIntTy::Signed(ty) => ty.ty_to_string(), | ||
17 | UncertainIntTy::Unsigned(ty) => ty.ty_to_string(), | ||
18 | } | ||
19 | } | ||
20 | |||
21 | pub fn from_name(name: &Name) -> Option<UncertainIntTy> { | ||
22 | if let Some(ty) = IntTy::from_name(name) { | ||
23 | Some(UncertainIntTy::Signed(ty)) | ||
24 | } else if let Some(ty) = UintTy::from_name(name) { | ||
25 | Some(UncertainIntTy::Unsigned(ty)) | ||
26 | } else { | ||
27 | None | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | #[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] | ||
33 | pub enum UncertainFloatTy { | ||
34 | Unknown, | ||
35 | Known(FloatTy), | ||
36 | } | ||
37 | |||
38 | impl UncertainFloatTy { | ||
39 | pub fn ty_to_string(&self) -> &'static str { | ||
40 | match *self { | ||
41 | UncertainFloatTy::Unknown => "{float}", | ||
42 | UncertainFloatTy::Known(ty) => ty.ty_to_string(), | ||
43 | } | ||
44 | } | ||
45 | |||
46 | pub fn from_name(name: &Name) -> Option<UncertainFloatTy> { | ||
47 | if let Some(ty) = FloatTy::from_name(name) { | ||
48 | Some(UncertainFloatTy::Known(ty)) | ||
49 | } else { | ||
50 | None | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
5 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | 55 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] |
6 | pub enum IntTy { | 56 | pub enum IntTy { |
7 | Isize, | 57 | Isize, |