Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5051,6 +5051,16 @@ static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
return nullptr;
}
}
// Consider member in a sibling.
const CXXRecordDecl *LastLVDecl =
(PathLengthToMember > LV.Designator.MostDerivedPathLength)
? getAsBaseClass(LV.Designator.Entries[PathLengthToMember - 1])
: LV.Designator.MostDerivedType->getAsCXXRecordDecl();
const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
Info.FFDiag(RHS);
return nullptr;
}

// Truncate the lvalue to the appropriate derived class.
if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(),
Expand Down
18 changes: 18 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,24 @@ namespace MemberPointer {
return (a.*f)();
}
static_assert(apply(A(2), &A::f) == 5, "");

struct C { };
struct D : C {
constexpr int f() const { return 1; };
};
struct E : C { };
struct F : D { };
constexpr C c1, c2[2];
constexpr D d1, d2[2];
constexpr E e1, e2[2];
constexpr F f;
static_assert((c1.*(static_cast<int (C::*)() const>(&D::f)))() == 1, ""); // expected-error {{constant expression}}
static_assert((d1.*(static_cast<int (C::*)() const>(&D::f)))() == 1, "");
static_assert((e1.*(static_cast<int (C::*)() const>(&D::f)))() == 1, ""); // expected-error {{constant expression}}
static_assert((f.*(static_cast<int (C::*)() const>(&D::f)))() == 1, "");
static_assert((c2[0].*(static_cast<int (C::*)() const>(&D::f)))() == 1, ""); // expected-error {{constant expression}}
static_assert((d2[0].*(static_cast<int (C::*)() const>(&D::f)))() == 1, "");
static_assert((e2[0].*(static_cast<int (C::*)() const>(&D::f)))() == 1, ""); // expected-error {{constant expression}}
}

namespace ArrayBaseDerived {
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx2a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,3 +1497,16 @@ namespace GH67317 {
// expected-note {{subobject of type 'const unsigned char' is not initialized}}
__builtin_bit_cast(unsigned char, *new char[3][1]);
};

namespace GH150705 {
struct A { };
struct B : A { };
struct C : A {
constexpr virtual int foo() const { return 0; }
};
constexpr auto p = &C::foo;
constexpr auto q = static_cast<int (A::*)() const>(p);
constexpr B b;
constexpr const A& a = b;
constexpr auto x = (a.*q)(); // expected-error {{constant expression}}
}