Index: include/llvm/ADT/StringRef.h =================================================================== --- include/llvm/ADT/StringRef.h +++ include/llvm/ADT/StringRef.h @@ -215,6 +215,15 @@ return Data[Index]; } + /// Disallow accidental assignment from a temporary std::string. + /// + /// The declaration here is extra complicated so that `stringRef = {}` + /// and `stringRef = "abc"` continue to select the move assignment operator. + template + typename std::enable_if::value, + StringRef>::type & + operator=(T &&Str) = delete; + /// @} /// @name Type Conversions /// @{ Index: unittests/ADT/StringRefTest.cpp =================================================================== --- unittests/ADT/StringRefTest.cpp +++ unittests/ADT/StringRefTest.cpp @@ -32,6 +32,34 @@ } +// Check that we can't accidentally assign a temporary std::string to a +// StringRef. (Unfortunately we can't make use of the same thing with +// constructors.) +// +// Disable this check under MSVC; even MSVC 2015 isn't consistent between +// std::is_assignable and actually writing such an assignment. +#if !defined(_MSC_VER) +static_assert( + !std::is_assignable::value, + "Assigning from prvalue std::string"); +static_assert( + !std::is_assignable::value, + "Assigning from xvalue std::string"); +static_assert( + std::is_assignable::value, + "Assigning from lvalue std::string"); +static_assert( + std::is_assignable::value, + "Assigning from prvalue C string"); +static_assert( + std::is_assignable::value, + "Assigning from xvalue C string"); +static_assert( + std::is_assignable::value, + "Assigning from lvalue C string"); +#endif + + namespace { TEST(StringRefTest, Construction) { EXPECT_EQ("", StringRef()); @@ -40,6 +68,14 @@ EXPECT_EQ("hello", StringRef(std::string("hello"))); } +TEST(StringRefTest, EmptyInitializerList) { + StringRef S = {}; + EXPECT_TRUE(S.empty()); + + S = {}; + EXPECT_TRUE(S.empty()); +} + TEST(StringRefTest, Iteration) { StringRef S("hello"); const char *p = "hello";