Skip to content

Commit 7276a6a

Browse files
committed
Auto merge of #90297 - dtolnay:dotzero, r=petrochenkov
Append .0 to unsuffixed float if it would otherwise become int token Previously the unsuffixed f32/f64 constructors of `proc_macro::Literal` would create literal tokens that are definitely not a float: ```rust Literal::f32_unsuffixed(10.0) // 10 Literal::f32_suffixed(10.0) // 10f32 Literal::f64_unsuffixed(10.0) // 10 Literal::f64_suffixed(10.0) // 10f64 ``` Notice that the `10` are actually integer tokens if you were to reparse them, not float tokens. This diff updates `Literal::f32_unsuffixed` and `Literal::f64_unsuffixed` to produce tokens that unambiguously parse as a float. This matches longstanding behavior of the proc-macro2 crate's implementation of these APIs dating back at least 3.5 years, so it's likely an unobjectionable behavior. ```rust Literal::f32_unsuffixed(10.0) // 10.0 Literal::f32_suffixed(10.0) // 10f32 Literal::f64_unsuffixed(10.0) // 10.0 Literal::f64_suffixed(10.0) // 10f64 ``` Fixes dtolnay/syn#1085.
2 parents 9d39f6a + 1f98077 commit 7276a6a

File tree

2 files changed

+20
-2
lines changed
  • library/proc_macro/src
  • src/test/ui/proc-macro/auxiliary/api

2 files changed

+20
-2
lines changed

library/proc_macro/src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,11 @@ impl Literal {
10741074
if !n.is_finite() {
10751075
panic!("Invalid float literal {}", n);
10761076
}
1077-
Literal(bridge::client::Literal::float(&n.to_string()))
1077+
let mut repr = n.to_string();
1078+
if !repr.contains('.') {
1079+
repr.push_str(".0");
1080+
}
1081+
Literal(bridge::client::Literal::float(&repr))
10781082
}
10791083

10801084
/// Creates a new suffixed floating-point literal.
@@ -1115,7 +1119,11 @@ impl Literal {
11151119
if !n.is_finite() {
11161120
panic!("Invalid float literal {}", n);
11171121
}
1118-
Literal(bridge::client::Literal::float(&n.to_string()))
1122+
let mut repr = n.to_string();
1123+
if !repr.contains('.') {
1124+
repr.push_str(".0");
1125+
}
1126+
Literal(bridge::client::Literal::float(&repr))
11191127
}
11201128

11211129
/// Creates a new suffixed floating-point literal.

src/test/ui/proc-macro/auxiliary/api/parse.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-linelength
2+
13
use proc_macro::Literal;
24

35
pub fn test() {
@@ -8,6 +10,14 @@ pub fn test() {
810
fn test_display_literal() {
911
assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "-10");
1012
assert_eq!(Literal::isize_suffixed(-10).to_string(), "-10isize");
13+
assert_eq!(Literal::f32_unsuffixed(-10.0).to_string(), "-10.0");
14+
assert_eq!(Literal::f32_suffixed(-10.0).to_string(), "-10f32");
15+
assert_eq!(Literal::f64_unsuffixed(-10.0).to_string(), "-10.0");
16+
assert_eq!(Literal::f64_suffixed(-10.0).to_string(), "-10f64");
17+
assert_eq!(
18+
Literal::f64_unsuffixed(1e100).to_string(),
19+
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0",
20+
);
1121
}
1222

1323
fn test_parse_literal() {

0 commit comments

Comments
 (0)