From c5025f0e4e46569bdd31743434a9d2dd602e95f2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 25 Oct 2021 20:25:30 -0700 Subject: [PATCH 1/2] Append .0 to unsuffixed float if it would otherwise become int token --- library/proc_macro/src/lib.rs | 12 ++++++++++-- src/test/ui/proc-macro/auxiliary/api/parse.rs | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 3a06cd04ab146..9d673d6968719 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -1074,7 +1074,11 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal(bridge::client::Literal::float(&n.to_string())) + let mut repr = n.to_string(); + if !repr.contains('.') { + repr.push_str(".0"); + } + Literal(bridge::client::Literal::float(&repr)) } /// Creates a new suffixed floating-point literal. @@ -1115,7 +1119,11 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal(bridge::client::Literal::float(&n.to_string())) + let mut repr = n.to_string(); + if !repr.contains('.') { + repr.push_str(".0"); + } + Literal(bridge::client::Literal::float(&repr)) } /// Creates a new suffixed floating-point literal. diff --git a/src/test/ui/proc-macro/auxiliary/api/parse.rs b/src/test/ui/proc-macro/auxiliary/api/parse.rs index 93551ebaf82d5..ef777fc033368 100644 --- a/src/test/ui/proc-macro/auxiliary/api/parse.rs +++ b/src/test/ui/proc-macro/auxiliary/api/parse.rs @@ -8,6 +8,10 @@ pub fn test() { fn test_display_literal() { assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "-10"); assert_eq!(Literal::isize_suffixed(-10).to_string(), "-10isize"); + assert_eq!(Literal::f32_unsuffixed(-10.0).to_string(), "-10.0"); + assert_eq!(Literal::f32_suffixed(-10.0).to_string(), "-10f32"); + assert_eq!(Literal::f64_unsuffixed(-10.0).to_string(), "-10.0"); + assert_eq!(Literal::f64_suffixed(-10.0).to_string(), "-10f64"); } fn test_parse_literal() { From 1f9807799c5f70c3a215698ad6e8eb8a03725969 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 5 Nov 2021 11:48:28 -0700 Subject: [PATCH 2/2] Add test to confirm fnn_unsuffixed does not emit exponent notation --- src/test/ui/proc-macro/auxiliary/api/parse.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/ui/proc-macro/auxiliary/api/parse.rs b/src/test/ui/proc-macro/auxiliary/api/parse.rs index ef777fc033368..6186b941ef69f 100644 --- a/src/test/ui/proc-macro/auxiliary/api/parse.rs +++ b/src/test/ui/proc-macro/auxiliary/api/parse.rs @@ -1,3 +1,5 @@ +// ignore-tidy-linelength + use proc_macro::Literal; pub fn test() { @@ -12,6 +14,10 @@ fn test_display_literal() { assert_eq!(Literal::f32_suffixed(-10.0).to_string(), "-10f32"); assert_eq!(Literal::f64_unsuffixed(-10.0).to_string(), "-10.0"); assert_eq!(Literal::f64_suffixed(-10.0).to_string(), "-10f64"); + assert_eq!( + Literal::f64_unsuffixed(1e100).to_string(), + "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0", + ); } fn test_parse_literal() {