]> granicus.if.org Git - llvm/commitdiff
[Testing/Support] Remove the const_cast in TakeExpected
authorPavel Labath <labath@google.com>
Thu, 22 Jun 2017 13:11:50 +0000 (13:11 +0000)
committerPavel Labath <labath@google.com>
Thu, 22 Jun 2017 13:11:50 +0000 (13:11 +0000)
Summary:
The const_cast in the "const" version of TakeExpected was quite
dangerous, as the function does indeed modify the apparently const
argument.

I assume the reason the const overload was added was to make the
function bind to xvalues(temporaries). That can be also achieved with
rvalue references, so I use that instead.

Using the ASSERT macros on const Expected objects will now become
illegal, but I believe that is correct, as it is not actually possible
to inspect the error stored in an Expected object without modifying it.

Reviewers: zturner, chandlerc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34405

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306001 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Testing/Support/Error.h

index d527529015933cb13fbe408556607ce3233aa7fd..f23d289266adc1be92a877562eb5aacf75b950a8 100644 (file)
@@ -30,8 +30,8 @@ template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
   return Result;
 }
 
-template <typename T> ExpectedHolder<T> TakeExpected(const Expected<T> &Exp) {
-  return TakeExpected(const_cast<Expected<T> &>(Exp));
+template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
+  return TakeExpected(Exp);
 }
 } // namespace detail