From db6989f4a880e9ee693a3112cadb1504947e2967 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 10:14:37 +0000 Subject: [PATCH 1/5] Print non-finite floating-point constants --- cpp2rust/converter/converter_lib.cpp | 19 ++++++++++++++---- tests/unit/nttp_distinct_specializations.cpp | 21 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 74f4520448..aa586ed071 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -231,10 +231,21 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) { } std::string GetNumAsString(llvm::APFloat value) { + auto non_finite = [&](std::string_view ty) -> std::optional { + if (value.isNaN()) { + return std::format("{}::from_bits({:#x})", ty, + value.bitcastToAPInt().getZExtValue()); + } + if (value.isInfinity()) { + return std::format("{}::{}INFINITY", ty, + value.isNegative() ? "NEG_" : ""); + } + return std::nullopt; + }; switch (llvm::APFloat::SemanticsToEnum(value.getSemantics())) { case llvm::APFloat::S_IEEEsingle: - return value.isInfinity() ? "f32::INFINITY" - : std::format("{}_f32", value.convertToFloat()); + return non_finite("f32").value_or( + std::format("{}_f32", value.convertToFloat())); case llvm::APFloat::S_x87DoubleExtended: { bool loses_info = false; value.convert(llvm::APFloat::IEEEdouble(), @@ -242,8 +253,8 @@ std::string GetNumAsString(llvm::APFloat value) { } [[fallthrough]]; case llvm::APFloat::S_IEEEdouble: - return value.isInfinity() ? "f64::INFINITY" - : std::format("{}_f64", value.convertToDouble()); + return non_finite("f64").value_or( + std::format("{}_f64", value.convertToDouble())); default: assert(false && "unsupported floating-point semantics"); return {}; diff --git a/tests/unit/nttp_distinct_specializations.cpp b/tests/unit/nttp_distinct_specializations.cpp index 5bb406211f..69e91f282b 100644 --- a/tests/unit/nttp_distinct_specializations.cpp +++ b/tests/unit/nttp_distinct_specializations.cpp @@ -1,3 +1,5 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++20 + #include template struct S { @@ -15,6 +17,12 @@ template int scale(int x) { return x * N; } template <> int scale<2>(int x) { return x - 2; } +template float get_float() { return V; } + +template double get_double() { return V; } + +template long double get_long_double() { return V; } + int main() { S<1> s1{5}; S<2> s2{5, 1}; @@ -26,5 +34,18 @@ int main() { assert(scale<1>(7) == 7); assert(scale<2>(7) == 5); assert(scale<3>(7) == 21); + + float f_nan = get_float<__builtin_nanf("")>(); + float f_inf = get_float<-__builtin_inff()>(); + assert(f_nan != f_nan); + assert(f_inf < 0); + double d_nan = get_double<__builtin_nan("0x5")>(); + double d_inf = get_double<-__builtin_inf()>(); + assert(d_nan != d_nan); + assert(d_inf < 0); + long double ld_nan = get_long_double<__builtin_nanl("")>(); + long double ld_inf = get_long_double<-__builtin_infl()>(); + assert(ld_nan != ld_nan); + assert(ld_inf < 0); return 0; } From 18a44e0401e6ef0e99e179c089d0c0c86bbb5b31 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 10:48:18 +0000 Subject: [PATCH 2/5] Simplify non-finite float formatting --- cpp2rust/converter/converter_lib.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index aa586ed071..780f6bf88a 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -231,7 +231,7 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) { } std::string GetNumAsString(llvm::APFloat value) { - auto non_finite = [&](std::string_view ty) -> std::optional { + auto format = [&](std::string_view ty, std::string finite) { if (value.isNaN()) { return std::format("{}::from_bits({:#x})", ty, value.bitcastToAPInt().getZExtValue()); @@ -240,12 +240,11 @@ std::string GetNumAsString(llvm::APFloat value) { return std::format("{}::{}INFINITY", ty, value.isNegative() ? "NEG_" : ""); } - return std::nullopt; + return finite; }; switch (llvm::APFloat::SemanticsToEnum(value.getSemantics())) { case llvm::APFloat::S_IEEEsingle: - return non_finite("f32").value_or( - std::format("{}_f32", value.convertToFloat())); + return format("f32", std::format("{}_f32", value.convertToFloat())); case llvm::APFloat::S_x87DoubleExtended: { bool loses_info = false; value.convert(llvm::APFloat::IEEEdouble(), @@ -253,8 +252,7 @@ std::string GetNumAsString(llvm::APFloat value) { } [[fallthrough]]; case llvm::APFloat::S_IEEEdouble: - return non_finite("f64").value_or( - std::format("{}_f64", value.convertToDouble())); + return format("f64", std::format("{}_f64", value.convertToDouble())); default: assert(false && "unsupported floating-point semantics"); return {}; From 0e093b43b9c9a34572fe426ac826ed5e3ca38f46 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 11:11:24 +0000 Subject: [PATCH 3/5] Update tests --- .../refcount/nttp_distinct_specializations.rs | 30 +++++++++++++++++++ .../unsafe/nttp_distinct_specializations.rs | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/tests/unit/out/refcount/nttp_distinct_specializations.rs b/tests/unit/out/refcount/nttp_distinct_specializations.rs index c96a166f84..4610788ed7 100644 --- a/tests/unit/out/refcount/nttp_distinct_specializations.rs +++ b/tests/unit/out/refcount/nttp_distinct_specializations.rs @@ -35,6 +35,24 @@ pub fn scale_1(mut x: i32) -> i32 { pub fn scale_2(mut x: i32) -> i32 { return (x - 2); } +pub fn get_float_3() -> f32 { + return f32::from_bits(0x7fc00000); +} +pub fn get_float_4() -> f32 { + return f32::NEG_INFINITY; +} +pub fn get_double_5() -> f64 { + return f64::from_bits(0x7ff8000000000005); +} +pub fn get_double_6() -> f64 { + return f64::NEG_INFINITY; +} +pub fn get_long_double_7() -> f64 { + return f64::from_bits(0x7ff8000000000000); +} +pub fn get_long_double_8() -> f64 { + return f64::NEG_INFINITY; +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -49,6 +67,18 @@ fn main_0() -> i32 { assert!((({ scale_0(7,) }) == 7)); assert!((({ scale_2(7,) }) == 5)); assert!((({ scale_1(7,) }) == 21)); + let mut f_nan: f32 = ({ get_float_3() }); + let mut f_inf: f32 = ({ get_float_4() }); + assert!((f_nan != f_nan)); + assert!((f_inf < 0_f32)); + let mut d_nan: f64 = ({ get_double_5() }); + let mut d_inf: f64 = ({ get_double_6() }); + assert!((d_nan != d_nan)); + assert!((d_inf < 0_f64)); + let mut ld_nan: f64 = ({ get_long_double_7() }); + let mut ld_inf: f64 = ({ get_long_double_8() }); + assert!((ld_nan != ld_nan)); + assert!((ld_inf < 0_f64)); return 0; } pub trait S_1_Impl { diff --git a/tests/unit/out/unsafe/nttp_distinct_specializations.rs b/tests/unit/out/unsafe/nttp_distinct_specializations.rs index a7dccd6097..c71cbd2524 100644 --- a/tests/unit/out/unsafe/nttp_distinct_specializations.rs +++ b/tests/unit/out/unsafe/nttp_distinct_specializations.rs @@ -46,6 +46,24 @@ pub unsafe fn scale_1(mut x: i32) -> i32 { pub unsafe fn scale_2(mut x: i32) -> i32 { return ((x) - (2)); } +pub unsafe fn get_float_3() -> f32 { + return f32::from_bits(0x7fc00000); +} +pub unsafe fn get_float_4() -> f32 { + return f32::NEG_INFINITY; +} +pub unsafe fn get_double_5() -> f64 { + return f64::from_bits(0x7ff8000000000005); +} +pub unsafe fn get_double_6() -> f64 { + return f64::NEG_INFINITY; +} +pub unsafe fn get_long_double_7() -> f64 { + return f64::from_bits(0x7ff8000000000000); +} +pub unsafe fn get_long_double_8() -> f64 { + return f64::NEG_INFINITY; +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -62,6 +80,18 @@ unsafe fn main_0() -> i32 { assert!(((unsafe { scale_0(7,) }) == (7))); assert!(((unsafe { scale_2(7,) }) == (5))); assert!(((unsafe { scale_1(7,) }) == (21))); + let mut f_nan: f32 = (unsafe { get_float_3() }); + let mut f_inf: f32 = (unsafe { get_float_4() }); + assert!(((f_nan) != (f_nan))); + assert!(((f_inf) < (0_f32))); + let mut d_nan: f64 = (unsafe { get_double_5() }); + let mut d_inf: f64 = (unsafe { get_double_6() }); + assert!(((d_nan) != (d_nan))); + assert!(((d_inf) < (0_f64))); + let mut ld_nan: f64 = (unsafe { get_long_double_7() }); + let mut ld_inf: f64 = (unsafe { get_long_double_8() }); + assert!(((ld_nan) != (ld_nan))); + assert!(((ld_inf) < (0_f64))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} From 5fbe9badd00da4431ab43b7a8cbb5ed0d585a677 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 13:25:41 +0000 Subject: [PATCH 4/5] Print NaN as the NAN constant --- cpp2rust/converter/converter_lib.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 780f6bf88a..548e697185 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -233,8 +233,10 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) { std::string GetNumAsString(llvm::APFloat value) { auto format = [&](std::string_view ty, std::string finite) { if (value.isNaN()) { - return std::format("{}::from_bits({:#x})", ty, - value.bitcastToAPInt().getZExtValue()); + if (value.isNegative()) { + return std::format("(-{}::NAN)", ty); + } + return std::format("{}::NAN", ty); } if (value.isInfinity()) { return std::format("{}::{}INFINITY", ty, From ab082616facd408993aebc3d47b1f6140153fc7c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 13:25:41 +0000 Subject: [PATCH 5/5] Update tests --- .../refcount/nttp_distinct_specializations.rs | 30 +++++++++++++++++++ .../unsafe/nttp_distinct_specializations.rs | 6 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/unit/out/refcount/nttp_distinct_specializations.rs b/tests/unit/out/refcount/nttp_distinct_specializations.rs index 6ccd6873c7..26ad9123e3 100644 --- a/tests/unit/out/refcount/nttp_distinct_specializations.rs +++ b/tests/unit/out/refcount/nttp_distinct_specializations.rs @@ -35,6 +35,24 @@ pub fn scale_1(mut x: i32) -> i32 { pub fn scale_2(mut x: i32) -> i32 { return (x - 2); } +pub fn get_float_3() -> f32 { + return f32::NAN; +} +pub fn get_float_4() -> f32 { + return f32::NEG_INFINITY; +} +pub fn get_double_5() -> f64 { + return f64::NAN; +} +pub fn get_double_6() -> f64 { + return f64::NEG_INFINITY; +} +pub fn get_long_double_7() -> f64 { + return f64::NAN; +} +pub fn get_long_double_8() -> f64 { + return f64::NEG_INFINITY; +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -49,6 +67,18 @@ fn main_0() -> i32 { assert!((({ scale_0(7,) }) == 7)); assert!((({ scale_2(7,) }) == 5)); assert!((({ scale_1(7,) }) == 21)); + let mut f_nan: f32 = ({ get_float_3() }); + let mut f_inf: f32 = ({ get_float_4() }); + assert!((f_nan != f_nan)); + assert!((f_inf < 0_f32)); + let mut d_nan: f64 = ({ get_double_5() }); + let mut d_inf: f64 = ({ get_double_6() }); + assert!((d_nan != d_nan)); + assert!((d_inf < 0_f64)); + let mut ld_nan: f64 = ({ get_long_double_7() }); + let mut ld_inf: f64 = ({ get_long_double_8() }); + assert!((ld_nan != ld_nan)); + assert!((ld_inf < 0_f64)); return 0; } pub trait S_1_Impl { diff --git a/tests/unit/out/unsafe/nttp_distinct_specializations.rs b/tests/unit/out/unsafe/nttp_distinct_specializations.rs index c71cbd2524..5c3029accb 100644 --- a/tests/unit/out/unsafe/nttp_distinct_specializations.rs +++ b/tests/unit/out/unsafe/nttp_distinct_specializations.rs @@ -47,19 +47,19 @@ pub unsafe fn scale_2(mut x: i32) -> i32 { return ((x) - (2)); } pub unsafe fn get_float_3() -> f32 { - return f32::from_bits(0x7fc00000); + return f32::NAN; } pub unsafe fn get_float_4() -> f32 { return f32::NEG_INFINITY; } pub unsafe fn get_double_5() -> f64 { - return f64::from_bits(0x7ff8000000000005); + return f64::NAN; } pub unsafe fn get_double_6() -> f64 { return f64::NEG_INFINITY; } pub unsafe fn get_long_double_7() -> f64 { - return f64::from_bits(0x7ff8000000000000); + return f64::NAN; } pub unsafe fn get_long_double_8() -> f64 { return f64::NEG_INFINITY;