[compiler-rt] [lld] [clang-tools-extra] [libcxxabi] [mlir] [libunwind] [flang] [libcxx] [llvm] [libc] [lldb] [openmp] [clang] [clang] static operators should evaluate object argument (PR #68485)

Tianlan Zhou via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 11 22:15:01 PST 2024


================
@@ -5678,10 +5678,15 @@ static ImplicitConversionSequence TryObjectArgumentInitialization(
   assert(FromType->isRecordType());
 
   QualType ClassType = S.Context.getTypeDeclType(ActingContext);
-  // [class.dtor]p2: A destructor can be invoked for a const, volatile or
-  //                 const volatile object.
+  // C++98 [class.dtor]p2:
+  //   A destructor can be invoked for a const, volatile or const volatile
+  //   object.
+  // C++98 [over.match.funcs]p4:
+  //   For static member functions, the implicit object parameter is considered
+  //   to match any object (since if the function is selected, the object is
+  //   discarded).
   Qualifiers Quals = Method->getMethodQualifiers();
-  if (isa<CXXDestructorDecl>(Method)) {
+  if (isa<CXXDestructorDecl>(Method) || Method->isStatic()) {
----------------
SuperSodaSea wrote:

The following code won't compile without this change:

```c++
struct Foo {
    static int operator()(int a, int b) { return a + b; }
};

void f() {
    const Foo foo;
    foo(1, 2); // 'this' argument to member function 'operator()' has type 'const Foo', but function is not marked const
}
```

This issue was founded in [libc++ test suite](https://buildkite.com/llvm-project/clang-ci/builds/9164#018cd53e-cd20-4251-8d04-ddf463079deb/6-17), which calls static `operator()` on a const object. Test for this situation is added in [`clang/test/SemaCXX/cxx2b-static-operator.cpp`](https://github.com/llvm/llvm-project/blob/fdfa350fc07aaa06e6d30402b0b265ba3591fa51/clang/test/SemaCXX/cxx2b-static-operator.cpp).

https://github.com/llvm/llvm-project/pull/68485


More information about the cfe-commits mailing list