diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index c1970f064cef5..d2348d2825651 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -2360,8 +2360,12 @@ template ::T> bool CastPointerIntegral(InterpState &S, CodePtr OpPC) { const Pointer &Ptr = S.Stk.pop(); + S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast) + << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret + << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC); + if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth())) - return false; + return Invalid(S, OpPC); S.Stk.push(T::from(Ptr.getIntegerRepresentation())); return true; diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp index 18806801bdd33..88e195c36b583 100644 --- a/clang/test/AST/ByteCode/cxx11.cpp +++ b/clang/test/AST/ByteCode/cxx11.cpp @@ -221,3 +221,15 @@ namespace PseudoDtor { static_assert(f() == 0, ""); // both-error {{constant expression}} \ // expected-note {{in call to}} } + +namespace IntToPtrCast { + typedef __INTPTR_TYPE__ intptr_t; + + constexpr intptr_t f(intptr_t x) { + return (((x) >> 21) * 8); + } + + extern "C" int foo; + constexpr intptr_t i = f((intptr_t)&foo - 10); // both-error{{constexpr variable 'i' must be initialized by a constant expression}} \ + // both-note{{reinterpret_cast}} +}