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_syntax/src/ast.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_syntax/src/ast.rs')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8bf439b60..211ba31e5 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -609,6 +609,52 @@ impl SelfParam { | |||
609 | } | 609 | } |
610 | } | 610 | } |
611 | 611 | ||
612 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
613 | pub enum LiteralFlavor { | ||
614 | String, | ||
615 | ByteString, | ||
616 | Char, | ||
617 | Byte, | ||
618 | IntNumber { suffix: Option<SmolStr> }, | ||
619 | FloatNumber { suffix: Option<SmolStr> }, | ||
620 | Bool, | ||
621 | } | ||
622 | |||
623 | impl LiteralExpr { | ||
624 | pub fn flavor(&self) -> LiteralFlavor { | ||
625 | let syntax = self.syntax(); | ||
626 | match syntax.kind() { | ||
627 | INT_NUMBER => { | ||
628 | let allowed_suffix_list = [ | ||
629 | "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", | ||
630 | "u16", "u8", | ||
631 | ]; | ||
632 | let text = syntax.text().to_string(); | ||
633 | let suffix = allowed_suffix_list | ||
634 | .iter() | ||
635 | .find(|&s| text.ends_with(s)) | ||
636 | .map(|&suf| SmolStr::new(suf)); | ||
637 | LiteralFlavor::IntNumber { suffix: suffix } | ||
638 | } | ||
639 | FLOAT_NUMBER => { | ||
640 | let allowed_suffix_list = ["f64", "f32"]; | ||
641 | let text = syntax.text().to_string(); | ||
642 | let suffix = allowed_suffix_list | ||
643 | .iter() | ||
644 | .find(|&s| text.ends_with(s)) | ||
645 | .map(|&suf| SmolStr::new(suf)); | ||
646 | LiteralFlavor::FloatNumber { suffix: suffix } | ||
647 | } | ||
648 | STRING | RAW_STRING => LiteralFlavor::String, | ||
649 | TRUE_KW | FALSE_KW => LiteralFlavor::Bool, | ||
650 | BYTE_STRING | RAW_BYTE_STRING => LiteralFlavor::ByteString, | ||
651 | CHAR => LiteralFlavor::Char, | ||
652 | BYTE => LiteralFlavor::Byte, | ||
653 | _ => unreachable!(), | ||
654 | } | ||
655 | } | ||
656 | } | ||
657 | |||
612 | #[test] | 658 | #[test] |
613 | fn test_doc_comment_of_items() { | 659 | fn test_doc_comment_of_items() { |
614 | let file = SourceFile::parse( | 660 | let file = SourceFile::parse( |