diff --git a/include/fmt/core.h b/include/fmt/core.h index 2625d86bf37b..77ff089be97c 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -629,8 +629,9 @@ inline typename std::enable_if< template inline typename std::enable_if< !convert_to_int::value && - !std::is_convertible>::value && - !std::is_convertible>::value, + !std::is_convertible>::value, + // Implicit conversion to std::string is not handled here because it's + // unsafe: https://github.com/fmtlib/fmt/issues/729 typed_value>::type make_value(const T &val) { return val; } diff --git a/test/compile-test/CMakeLists.txt b/test/compile-test/CMakeLists.txt index 38038ede056b..dda1fb96468e 100644 --- a/test/compile-test/CMakeLists.txt +++ b/test/compile-test/CMakeLists.txt @@ -9,8 +9,7 @@ set(CMAKE_REQUIRED_FLAGS ${CPP14_FLAG}) function (generate_source result fragment) set(${result} " #define FMT_HEADER_ONLY 1 - #include \"fmt/posix.h\" - #include \"fmt/ostream.h\" + #include \"fmt/format.h\" int main() { ${fragment} } @@ -58,6 +57,14 @@ expect_compile_error("fmt::format(\"{}\", L\"foo\");") # mixing UTF-8 with UTF-16/32 can result in an invalid output. expect_compile_error("fmt::format(L\"{}\", \"foo\");") +# Formatting a wide string with a narrow format string is forbidden. +expect_compile_error(" + struct S { + operator std::string() const { return std::string(); } + }; + fmt::format(\"{}\", S()); +") + # Make sure that compiler features detected in the header # match the features detected in CMake. if (SUPPORTS_USER_DEFINED_LITERALS) diff --git a/test/format-test.cc b/test/format-test.cc index 4a6533f5f66c..d9a20919837c 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1072,16 +1072,6 @@ TEST(FormatterTest, FormatStdStringView) { } #endif -struct ConvertibleToString { - std::string s; - ConvertibleToString() : s("foo") {} - operator const std::string &() const { return s; } -}; - -TEST(FormatterTest, FormatConvertibleToString) { - EXPECT_EQ("foo", format("{}", ConvertibleToString())); -} - struct ConvertibleToStringView { operator fmt::string_view() const { return "foo"; } }; diff --git a/test/util-test.cc b/test/util-test.cc index 544d933d06fc..5bba773632b9 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -402,7 +402,7 @@ TEST(UtilTest, BitCast) { uint32_t u[2]; }; auto s = fmt::internal::bit_cast(uint64_t(42)); - EXPECT_EQ(fmt::internal::bit_cast(s), 42); + EXPECT_EQ(fmt::internal::bit_cast(s), 42u); s = fmt::internal::bit_cast(uint64_t(~0ull)); EXPECT_EQ(fmt::internal::bit_cast(s), ~0ull); }