[clang] [clang][AST] Remove HasFirstArg assertion in CallExpr::getBeginLoc() (PR #130725)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 11 13:18:39 PDT 2025


https://github.com/HighCommander4 updated https://github.com/llvm/llvm-project/pull/130725

>From d6c4f884c221fffca5d686ed18a9f607450a8fb5 Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Tue, 11 Mar 2025 02:34:51 -0400
Subject: [PATCH] [clang][AST] Remove HasFirstArg assertion in
 CallExpr::getBeginLoc()

There are cases where the assertion legitimately does not hold
(e.g. CallExpr::CreateTemporary()), and there's no readily available
way to tell such cases apart.

Fixes https://github.com/llvm/llvm-project/issues/130272
---
 clang/docs/ReleaseNotes.rst                     | 1 +
 clang/lib/AST/Expr.cpp                          | 8 +++++---
 clang/test/AST/ast-dump-cxx2b-deducing-this.cpp | 9 +++++++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7d2eea9a7e25f..5a6e314f0dbf4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -304,6 +304,7 @@ Bug Fixes to C++ Support
 - Correctly diagnoses template template paramters which have a pack parameter
   not in the last position.
 - Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
+- Fixed an assertion failure affecting code that uses C++23 "deducing this". (#GH130272)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index ccfec7fda0cbc..1dde64f193dbd 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1656,9 +1656,11 @@ SourceLocation CallExpr::getBeginLoc() const {
     if (const auto *Method =
             dyn_cast_if_present<const CXXMethodDecl>(getCalleeDecl());
         Method && Method->isExplicitObjectMemberFunction()) {
-      bool HasFirstArg = getNumArgs() > 0 && getArg(0);
-      assert(HasFirstArg);
-      if (HasFirstArg)
+      // Note: while we typically expect the call to have a first argument
+      // here, we can't assert it because in some cases it does not, e.g.
+      // calls created with CallExpr::CreateTemporary() during overload
+      // resolution.
+      if (getNumArgs() > 0 && getArg(0))
         return getArg(0)->getBeginLoc();
     }
   }
diff --git a/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp b/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp
index abe9d6a5b5bc6..fc86aeb3e5ec3 100644
--- a/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp
+++ b/clang/test/AST/ast-dump-cxx2b-deducing-this.cpp
@@ -26,3 +26,12 @@ struct S {
   // CHECK-NEXT:   `-DeclRefExpr 0x{{[^ ]*}} <col:5> 'S<T>' lvalue ParmVar 0x{{[^ ]*}} 's' 'S<T>'
 };
 }
+
+namespace GH130272 {
+struct A {};
+struct B {
+  operator A(this B);
+};
+A a = A(B{});
+// CHECK: CallExpr 0x{{[^ ]*}} <col:9, col:11> 'A':'GH130272::A'
+}



More information about the cfe-commits mailing list