From 31d075e0bf1051eea03221db54da4e9bcc988a5e Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Fri, 4 Nov 2016 15:42:12 -0700 Subject: [PATCH] Disallow StringRef assignment from temporary std::strings. Similar to r283798, this prevents accidentally referring to temporary storage that goes out of scope by the end of the statement: someStringRef = getStringByValue(); someStringRef = (Twine("-") + otherString).str(); Note that once again the constructor still has this problem: StringRef someStringRef = getStringByValue(); because once again we occasionally rely on this in calls: takesStringRef(getStringByValue()); takesStringRef(Twine("-") + otherString); Still, it's a step. --- include/llvm/ADT/StringRef.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 17a428cfd1d..17c5e403cc0 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -215,6 +215,15 @@ namespace llvm { 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 /// @{