diff options
| author | Eric Fiselier <eric@efcs.ca> | 2016-11-03 02:13:17 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2016-11-03 02:13:17 +0000 |
| commit | e4e9e281a1fbc915b6567a003fd21cc5492cde65 (patch) | |
| tree | d4141b30616dd31be8d6803014f23bda7ce9ed56 /clang/lib/Sema | |
| parent | 0515fb8d4b2dbfce4fc960abb08a8d9d2f48fe2b (diff) | |
| download | bcm5719-llvm-e4e9e281a1fbc915b6567a003fd21cc5492cde65.tar.gz bcm5719-llvm-e4e9e281a1fbc915b6567a003fd21cc5492cde65.zip | |
[Sema] Allow static_cast<T&&>(e) to check explicit conversions for non-reference-related types.
Summary:
[expr.cast.static] states:
> 3. 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”. The result refers to the object or the specified base class subobject thereof. If T2 is
> an inaccessible or ambiguous base class of T1, a program that necessitates such a cast is
> ill-formed.
>
> 4. Otherwise, an expression e can be explicitly converted to a type T using a static_cast of the form static_-
> cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t. [...]
Currently when checking p3 Clang will diagnose `static_cast<T&&>(e)` as invalid if the argument is not reference compatible with `T`. However I believe the correct behavior is to also check p4 in those cases. For example:
```
double y = 42;
static_cast<int&&>(y); // this should be OK. 'int&& t(y)' is well formed
```
Note that we still don't check p4 for non-reference-compatible types which are reference-related since `T&& t(e);` should never be well formed in those cases.
Reviewers: rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D26231
llvm-svn: 285872
Diffstat (limited to 'clang/lib/Sema')
| -rw-r--r-- | clang/lib/Sema/SemaCast.cpp | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index 43fdf590424..1821009346b 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -983,7 +983,7 @@ static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, // 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, + tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind, BasePath, msg); if (tcr != TC_NotApplicable) return tcr; @@ -1134,12 +1134,12 @@ static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr, } /// Tests whether a conversion according to N2844 is valid. -TryCastResult -TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, - bool CStyle, CastKind &Kind, CXXCastPath &BasePath, - unsigned &msg) { +TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, + QualType DestType, bool CStyle, + CastKind &Kind, CXXCastPath &BasePath, + unsigned &msg) { // C++11 [expr.static.cast]p3: - // A glvalue of type "cv1 T1" can be cast to type "rvalue reference to + // 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>(); if (!R) @@ -1160,15 +1160,18 @@ TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, FromType = FromType.getUnqualifiedType(); ToType = ToType.getUnqualifiedType(); } - - if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(), - ToType, FromType, - DerivedToBase, ObjCConversion, - ObjCLifetimeConversion) - != Sema::Ref_Compatible) { - if (CStyle) + + Sema::ReferenceCompareResult RefResult = Self.CompareReferenceRelationship( + SrcExpr->getLocStart(), ToType, FromType, DerivedToBase, ObjCConversion, + ObjCLifetimeConversion); + if (RefResult != Sema::Ref_Compatible) { + if (CStyle || RefResult == Sema::Ref_Incompatible) return TC_NotApplicable; - msg = diag::err_bad_lvalue_to_rvalue_cast; + // Diagnose types which are reference-related but not compatible here since + // we can provide better diagnostics. In these cases forwarding to + // [expr.static.cast]p4 should never result in a well-formed cast. + msg = SrcExpr->isLValue() ? diag::err_bad_lvalue_to_rvalue_cast + : diag::err_bad_rvalue_to_rvalue_cast; return TC_Failed; } |

