r241998 - [Sema] If lvalue to rvalue reference cast is valid don't emit diagnostic.

Davide Italiano davide at freebsd.org
Sun Jul 12 15:10:56 PDT 2015


Author: davide
Date: Sun Jul 12 17:10:56 2015
New Revision: 241998

URL: http://llvm.org/viewvc/llvm-project?rev=241998&view=rev
Log:
[Sema] If lvalue to rvalue reference cast is valid don't emit diagnostic.

In the test, y1 is not reference compatible to y2 and we currently assume
the cast is ill-formed so we emit a diagnostic. Instead, in order to honour
the standard, if y1 it's not reference-compatible to y2 then it can't be
converted using a static_cast, and a reinterpret_cast should be tried instead.
Richard Smith provided the correct interpretation of the standard and
explanation about the subtle difference between "can't be cast" and "the cast
is ill-formed". The former applies in this case.

PR:		23802

Added:
    cfe/trunk/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp
Modified:
    cfe/trunk/lib/Sema/SemaCast.cpp

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=241998&r1=241997&r2=241998&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Sun Jul 12 17:10:56 2015
@@ -975,7 +975,7 @@ static TryCastResult TryStaticCast(Sema
   if (tcr != TC_NotApplicable)
     return tcr;
 
-  // C++0x [expr.static.cast]p3: 
+  // C++11 [expr.static.cast]p3: 
   //   A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2
   //   T2" if "cv2 T2" is reference-compatible with "cv1 T1".
   tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, 
@@ -1133,7 +1133,7 @@ TryCastResult
 TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
                       bool CStyle, CastKind &Kind, CXXCastPath &BasePath, 
                       unsigned &msg) {
-  // C++0x [expr.static.cast]p3:
+  // C++11 [expr.static.cast]p3:
   //   A glvalue of type "cv1 T1" can be cast to type "rvalue reference to 
   //   cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
   const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
@@ -1161,6 +1161,8 @@ TryLValueToRValueCast(Sema &Self, Expr *
                                         DerivedToBase, ObjCConversion,
                                         ObjCLifetimeConversion) 
         < Sema::Ref_Compatible_With_Added_Qualification) {
+    if (CStyle)
+      return TC_NotApplicable;
     msg = diag::err_bad_lvalue_to_rvalue_cast;
     return TC_Failed;
   }

Added: cfe/trunk/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp?rev=241998&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp (added)
+++ cfe/trunk/test/SemaCXX/cast-lvalue-to-rvalue-reference.cpp Sun Jul 12 17:10:56 2015
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
+// expected-no-diagnostics
+
+struct S {};
+int x;
+S&& y1 = (S&&)x;
+S&& y2 = reinterpret_cast<S&&>(x);
+S& z1 = (S&)x;





More information about the cfe-commits mailing list