Skip to content

Commit 4fd7ad9

Browse files
committed
Add special case for f32 and f43 suffices on Literal.kind
1 parent 578bc05 commit 4fd7ad9

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

crates/ra_assists/src/add_explicit_type.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ mod tests {
7777
"fn f() { let a<|> = 42f64; }",
7878
"fn f() { let a<|>: f64 = 42f64; }",
7979
);
80+
check_assist(
81+
add_explicit_type,
82+
"fn f() { let a<|> = 42f32; }",
83+
"fn f() { let a<|>: f32 = 42f32; }",
84+
);
8085
}
8186

8287
#[test]

crates/ra_syntax/src/ast/expr_extensions.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,34 @@ impl ast::Literal {
239239
pub fn kind(&self) -> LiteralKind {
240240
match self.token().kind() {
241241
INT_NUMBER => {
242-
let allowed_suffix_list = [
242+
let int_suffix_list = [
243243
"isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32",
244244
"u16", "u8",
245245
];
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+
246254
let text = self.token().text().to_string();
247-
let suffix = allowed_suffix_list
255+
256+
let float_suffix = float_suffix_list
248257
.iter()
249258
.find(|&s| text.ends_with(s))
250259
.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+
}
252270
}
253271
FLOAT_NUMBER => {
254272
let allowed_suffix_list = ["f64", "f32"];

crates/ra_syntax/src/parsing/lexer.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,3 @@ pub fn classify_literal(text: &str) -> Option<Token> {
145145
};
146146
Some(Token { kind, len: TextUnit::from_usize(t.len) })
147147
}
148-
149-
#[cfg(test)]
150-
mod 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-
}

0 commit comments

Comments
 (0)