Add special case for f32 and f43 suffices on Literal.kind

This commit is contained in:
Phil Ellison 2019-07-28 20:47:44 +01:00
parent 578bc05ca4
commit 4fd7ad908b
3 changed files with 26 additions and 17 deletions

View file

@ -77,6 +77,11 @@ mod tests {
"fn f() { let a<|> = 42f64; }",
"fn f() { let a<|>: f64 = 42f64; }",
);
check_assist(
add_explicit_type,
"fn f() { let a<|> = 42f32; }",
"fn f() { let a<|>: f32 = 42f32; }",
);
}
#[test]

View file

@ -239,17 +239,35 @@ 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));
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"];
let text = self.token().text().to_string();

View file

@ -145,17 +145,3 @@ pub fn classify_literal(text: &str) -> Option<Token> {
};
Some(Token { kind, len: TextUnit::from_usize(t.len) })
}
#[cfg(test)]
mod tests {
use super::*;
// https://github.com/rust-analyzer/rust-analyzer/issues/1592
#[test]
fn lex_float_literal() {
assert_eq!(
tokenize("42f64")[0],
Token { kind: FLOAT_NUMBER, len: TextUnit::from_usize(5)}
);
}
}