From 6850fafd27ba804d4d4ca8af404beed5574e3749 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sun, 29 Apr 2012 08:24:44 +0000 Subject: [PATCH] PR9546, DR1268: A prvalue cannot be reinterpret_cast to an rvalue reference type. But a glvalue can be reinterpret_cast to either flavor of reference. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155789 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaCast.cpp | 7 +++---- test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Sema/SemaCast.cpp b/lib/Sema/SemaCast.cpp index 54683e127e..a67d1dec59 100644 --- a/lib/Sema/SemaCast.cpp +++ b/lib/Sema/SemaCast.cpp @@ -1504,10 +1504,9 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, } if (const ReferenceType *DestTypeTmp = DestType->getAs()) { - bool LValue = DestTypeTmp->isLValueReferenceType(); - if (LValue && !SrcExpr.get()->isLValue()) { - // Cannot cast non-lvalue to lvalue reference type. See the similar - // comment in const_cast. + if (!SrcExpr.get()->isGLValue()) { + // Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the + // similar comment in const_cast. msg = diag::err_bad_cxx_cast_rvalue; return TC_NotApplicable; } diff --git a/test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp b/test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp index 22892a63ab..212adc8c2b 100644 --- a/test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp +++ b/test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp @@ -10,7 +10,10 @@ template T&& xvalue(); void test_classification(char *ptr) { int (&fr0)(int) = reinterpret_cast(f); int &&ir0 = reinterpret_cast(*ptr); - int &&ir1 = reinterpret_cast(0); - int &&ir2 = reinterpret_cast('a'); + int &&ir1 = reinterpret_cast(0); // expected-error {{rvalue to reference type}} + int &&ir2 = reinterpret_cast('a'); // expected-error {{rvalue to reference type}} int &&ir3 = reinterpret_cast(xvalue()); + // Per DR1268, reinterpret_cast can convert between lvalues and xvalues. + int &ir4 = reinterpret_cast(xvalue()); + int &&ir5 = reinterpret_cast(*ptr); } -- 2.40.0