https://github.com/keinflue created https://github.com/llvm/llvm-project/pull/150829
HandleMemberPointerAccess considered whether the lvalue path in a member pointer access matched the bases of the containing class of the member, but neglected to check the same for the containing class of the member itself, thereby ignoring access attempts to members in direct sibling classes. I am new affected code and not 100% sure that the conditions I chose are correct. At least the test cases I added pass. Fixes #150705. Fixes #150709. >From 16b4c1862d773fd6e75d07bb194f42a00d680a0f Mon Sep 17 00:00:00 2001 From: keinflue <80230456+keinf...@users.noreply.github.com> Date: Sun, 27 Jul 2025 04:36:22 +0200 Subject: [PATCH] [clang] Fix constant evaluation of member pointer access to member of sibling class. HandleMemberPointerAccess considered whether the lvalue path in a member pointer access matched the bases of the containing class of the member, but neglected to check the same for the containing class of the member itself, thereby ignoring access attempts to members in direct sibling classes. Fixes #150705. Fixes #150709. --- clang/lib/AST/ExprConstant.cpp | 10 ++++++++++ .../test/SemaCXX/constant-expression-cxx11.cpp | 18 ++++++++++++++++++ .../test/SemaCXX/constant-expression-cxx2a.cpp | 13 +++++++++++++ 3 files changed, 41 insertions(+) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 9808298a1b1d0..83096f2d982b8 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -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(), diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index 5ecb8c607f59a..4584f53aa16f0 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -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 { diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp index ffb7e633c2919..b22a82c57ef06 100644 --- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp @@ -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}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits