1604: Fix failing type interference for floating point literal r=matklad a=theotherphil

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/1592

Co-authored-by: Phil Ellison <phil.j.ellison@gmail.com>
This commit is contained in:
bors[bot] 2019-07-29 19:02:39 +00:00
commit a5fe9f7a87
2 changed files with 34 additions and 14 deletions

View file

@ -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]"###
);
}

View file

@ -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"];