aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Ellison <[email protected]>2019-07-28 20:47:44 +0100
committerPhil Ellison <[email protected]>2019-07-28 20:47:44 +0100
commit4fd7ad908b6e7cee0ee7853fcf29fb5a38a19aa2 (patch)
tree30574ad27b93b2eecbda661547ee952e6b32d0fd
parent578bc05ca41e095c61c64bdef255760c26736a11 (diff)
Add special case for f32 and f43 suffices on Literal.kind
-rw-r--r--crates/ra_assists/src/add_explicit_type.rs5
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs24
-rw-r--r--crates/ra_syntax/src/parsing/lexer.rs14
3 files changed, 26 insertions, 17 deletions
diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs
index 7c6080640..2ed03e5e9 100644
--- a/crates/ra_assists/src/add_explicit_type.rs
+++ b/crates/ra_assists/src/add_explicit_type.rs
@@ -77,6 +77,11 @@ mod tests {
77 "fn f() { let a<|> = 42f64; }", 77 "fn f() { let a<|> = 42f64; }",
78 "fn f() { let a<|>: f64 = 42f64; }", 78 "fn f() { let a<|>: f64 = 42f64; }",
79 ); 79 );
80 check_assist(
81 add_explicit_type,
82 "fn f() { let a<|> = 42f32; }",
83 "fn f() { let a<|>: f32 = 42f32; }",
84 );
80 } 85 }
81 86
82 #[test] 87 #[test]
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs
index f9190d877..745dece98 100644
--- a/crates/ra_syntax/src/ast/expr_extensions.rs
+++ b/crates/ra_syntax/src/ast/expr_extensions.rs
@@ -239,16 +239,34 @@ impl ast::Literal {
239 pub fn kind(&self) -> LiteralKind { 239 pub fn kind(&self) -> LiteralKind {
240 match self.token().kind() { 240 match self.token().kind() {
241 INT_NUMBER => { 241 INT_NUMBER => {
242 let allowed_suffix_list = [ 242 let int_suffix_list = [
243 "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", 243 "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32",
244 "u16", "u8", 244 "u16", "u8",
245 ]; 245 ];
246
247 // The lexer treats e.g. `1f64` as an integer literal. See
248 // https://github.com/rust-analyzer/rust-analyzer/issues/1592
249 // and the comments on the linked PR.
250 let float_suffix_list = [
251 "f32", "f64"
252 ];
253
246 let text = self.token().text().to_string(); 254 let text = self.token().text().to_string();
247 let suffix = allowed_suffix_list 255
256 let float_suffix = float_suffix_list
248 .iter() 257 .iter()
249 .find(|&s| text.ends_with(s)) 258 .find(|&s| text.ends_with(s))
250 .map(|&suf| SmolStr::new(suf)); 259 .map(|&suf| SmolStr::new(suf));
251 LiteralKind::IntNumber { suffix } 260
261 if float_suffix.is_some() {
262 LiteralKind::FloatNumber { suffix: float_suffix }
263 } else {
264 let suffix = int_suffix_list
265 .iter()
266 .find(|&s| text.ends_with(s))
267 .map(|&suf| SmolStr::new(suf));
268 LiteralKind::IntNumber { suffix }
269 }
252 } 270 }
253 FLOAT_NUMBER => { 271 FLOAT_NUMBER => {
254 let allowed_suffix_list = ["f64", "f32"]; 272 let allowed_suffix_list = ["f64", "f32"];
diff --git a/crates/ra_syntax/src/parsing/lexer.rs b/crates/ra_syntax/src/parsing/lexer.rs
index 45ef88ac0..2a4343b0a 100644
--- a/crates/ra_syntax/src/parsing/lexer.rs
+++ b/crates/ra_syntax/src/parsing/lexer.rs
@@ -145,17 +145,3 @@ pub fn classify_literal(text: &str) -> Option<Token> {
145 }; 145 };
146 Some(Token { kind, len: TextUnit::from_usize(t.len) }) 146 Some(Token { kind, len: TextUnit::from_usize(t.len) })
147} 147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 // https://github.com/rust-analyzer/rust-analyzer/issues/1592
154 #[test]
155 fn lex_float_literal() {
156 assert_eq!(
157 tokenize("42f64")[0],
158 Token { kind: FLOAT_NUMBER, len: TextUnit::from_usize(5)}
159 );
160 }
161}