diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 676711d0adbd..36dea17a30b3 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -334,6 +334,8 @@ fn infer_literals() { infer(r##" fn test() { 5i32; + 5f32; + 5f64; "hello"; b"bytes"; 'c'; @@ -351,18 +353,20 @@ fn test() { } "##), @r###" -[11; 201) '{ ...o"#; }': () +[11; 221) '{ ...o"#; }': () [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': f64 -[83; 87) '5000': i32 -[93; 98) 'false': bool -[104; 108) 'true': bool -[114; 182) 'r#" ... "#': &str -[188; 198) 'br#"yolo"#': &[u8]"### +[27; 31) '5f32': f32 +[37; 41) '5f64': f64 +[47; 54) '"hello"': &str +[60; 68) 'b"bytes"': &[u8] +[74; 77) ''c'': char +[83; 87) 'b'b'': u8 +[93; 97) '3.14': f64 +[103; 107) '5000': i32 +[113; 118) 'false': bool +[124; 128) 'true': bool +[134; 202) 'r#" ... "#': &str +[208; 218) 'br#"yolo"#': &[u8]"### ); } diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index f9190d877914..8284f1b25f89 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -239,16 +239,32 @@ impl ast::Literal { pub fn kind(&self) -> LiteralKind { match self.token().kind() { INT_NUMBER => { - let allowed_suffix_list = [ + let int_suffix_list = [ "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", "u16", "u8", ]; + + // The lexer treats e.g. `1f64` as an integer literal. See + // https://github.com/rust-analyzer/rust-analyzer/issues/1592 + // and the comments on the linked PR. + let float_suffix_list = ["f32", "f64"]; + let text = self.token().text().to_string(); - let suffix = allowed_suffix_list + + let float_suffix = float_suffix_list .iter() .find(|&s| text.ends_with(s)) .map(|&suf| SmolStr::new(suf)); - LiteralKind::IntNumber { suffix } + + if float_suffix.is_some() { + LiteralKind::FloatNumber { suffix: float_suffix } + } else { + let suffix = int_suffix_list + .iter() + .find(|&s| text.ends_with(s)) + .map(|&suf| SmolStr::new(suf)); + LiteralKind::IntNumber { suffix } + } } FLOAT_NUMBER => { let allowed_suffix_list = ["f64", "f32"];