Skip to content

Commit a5fe9f7

Browse files
Merge #1604
1604: Fix failing type interference for floating point literal r=matklad a=theotherphil Fixes #1592 Co-authored-by: Phil Ellison <phil.j.ellison@gmail.com>
2 parents 359b337 + d79dc38 commit a5fe9f7

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

crates/ra_hir/src/ty/tests.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ fn infer_literals() {
334334
infer(r##"
335335
fn test() {
336336
5i32;
337+
5f32;
338+
5f64;
337339
"hello";
338340
b"bytes";
339341
'c';
@@ -351,18 +353,20 @@ fn test() {
351353
}
352354
"##),
353355
@r###"
354-
[11; 201) '{ ...o"#; }': ()
356+
[11; 221) '{ ...o"#; }': ()
355357
[17; 21) '5i32': i32
356-
[27; 34) '"hello"': &str
357-
[40; 48) 'b"bytes"': &[u8]
358-
[54; 57) ''c'': char
359-
[63; 67) 'b'b'': u8
360-
[73; 77) '3.14': f64
361-
[83; 87) '5000': i32
362-
[93; 98) 'false': bool
363-
[104; 108) 'true': bool
364-
[114; 182) 'r#" ... "#': &str
365-
[188; 198) 'br#"yolo"#': &[u8]"###
358+
[27; 31) '5f32': f32
359+
[37; 41) '5f64': f64
360+
[47; 54) '"hello"': &str
361+
[60; 68) 'b"bytes"': &[u8]
362+
[74; 77) ''c'': char
363+
[83; 87) 'b'b'': u8
364+
[93; 97) '3.14': f64
365+
[103; 107) '5000': i32
366+
[113; 118) 'false': bool
367+
[124; 128) 'true': bool
368+
[134; 202) 'r#" ... "#': &str
369+
[208; 218) 'br#"yolo"#': &[u8]"###
366370
);
367371
}
368372

crates/ra_syntax/src/ast/expr_extensions.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,32 @@ 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 = ["f32", "f64"];
251+
246252
let text = self.token().text().to_string();
247-
let suffix = allowed_suffix_list
253+
254+
let float_suffix = float_suffix_list
248255
.iter()
249256
.find(|&s| text.ends_with(s))
250257
.map(|&suf| SmolStr::new(suf));
251-
LiteralKind::IntNumber { suffix }
258+
259+
if float_suffix.is_some() {
260+
LiteralKind::FloatNumber { suffix: float_suffix }
261+
} else {
262+
let suffix = int_suffix_list
263+
.iter()
264+
.find(|&s| text.ends_with(s))
265+
.map(|&suf| SmolStr::new(suf));
266+
LiteralKind::IntNumber { suffix }
267+
}
252268
}
253269
FLOAT_NUMBER => {
254270
let allowed_suffix_list = ["f64", "f32"];

0 commit comments

Comments
 (0)