[clang] [Clang] Fix confusing diagnostic with explicit 'this' parameters. (PR #99824)

Braden Helmer via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 23 04:10:00 PDT 2024


https://github.com/bradenhelmer updated https://github.com/llvm/llvm-project/pull/99824

>From e4c3701ea6be894b1094f45d0590c61e5aa44897 Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Sun, 21 Jul 2024 14:10:17 -0400
Subject: [PATCH 1/3] Fix diag mismatch

---
 clang/lib/Sema/SemaOverload.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 5ea6b06121c7c..102cb5f7baef2 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11152,7 +11152,8 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand,
   // non-constructor method.  Note that 'I' corresponds the
   // conversion-slot index.
   bool isObjectArgument = false;
-  if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) {
+  if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn) &&
+      !Fn->hasCXXExplicitFunctionObjectParameter()) {
     if (I == 0)
       isObjectArgument = true;
     else

>From 7f15d3d1a58f11c0ec2b6e646131c0fc0b7a9d5b Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Sun, 21 Jul 2024 15:37:09 -0400
Subject: [PATCH 2/3] Small change and add test

---
 clang/lib/Sema/SemaOverload.cpp            | 5 ++---
 clang/test/SemaCXX/cxx2b-deducing-this.cpp | 8 ++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 1ef43b2103a9b..ac90ccd5643c1 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -11146,11 +11146,10 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand,
   // non-constructor method.  Note that 'I' corresponds the
   // conversion-slot index.
   bool isObjectArgument = false;
-  if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn) &&
-      !Fn->hasCXXExplicitFunctionObjectParameter()) {
+  if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) {
     if (I == 0)
       isObjectArgument = true;
-    else
+    else if (!Fn->hasCXXExplicitFunctionObjectParameter())
       I--;
   }
 
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 5cbc1f735383b..6ad54b9b2c63d 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -959,3 +959,11 @@ void f();
 };
 void a::f(this auto) {} // expected-error {{an explicit object parameter cannot appear in a non-member function}}
 }
+
+struct R {
+  void f(this auto &&self, int &&r_value_ref) {} // expected-note {{candidate function template not viable: expects an rvalue for 2nd argument}}
+  void g(int &&r_value_ref) {
+	f(r_value_ref); // expected-error {{no matching member function for call to 'f'}}
+  }
+};
+

>From 19c05ce1b9b30a75ca72de1393d15b4e07e4c5e1 Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Tue, 23 Jul 2024 07:09:50 -0400
Subject: [PATCH 3/3] Add release note

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4638b91b48f95..481b6313974ea 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -746,6 +746,8 @@ Improvements to Clang's diagnostics
 - Clang now diagnoses dangling assignments for pointer-like objects (annotated with `[[gsl::Pointer]]`) under `-Wdangling-assignment-gsl` (off by default)
   Fixes #GH63310.
 
+- Clang now has improved diagnostics for functions with explicit 'this' parameters. Fixes #GH97878
+
 Improvements to Clang's time-trace
 ----------------------------------
 



More information about the cfe-commits mailing list