[clang] [clang] Fix a crash when default argument is passed to an explicit object parameter (PR #177534)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 31 05:41:11 PST 2026
https://github.com/hax0kartik updated https://github.com/llvm/llvm-project/pull/177534
>From 108e02a34bea10b1902c336a8d032a4d7a46e061 Mon Sep 17 00:00:00 2001
From: hax0kartik <agarwala.kartik at gmail.com>
Date: Fri, 23 Jan 2026 12:05:46 +0530
Subject: [PATCH 1/2] [clang] Fix a crash when default argument is passed to an
explicit object parameter
This is an invalid usecase and we should mark it as such after
outputting a Diagnostic.
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDeclCXX.cpp | 1 +
clang/test/SemaCXX/cxx2b-deducing-this.cpp | 22 ++++++++++++++++++++++
3 files changed, 24 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2e7a9fff3161b..fe573afc99422 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -232,6 +232,7 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed a crash when instantiating ``requires`` expressions involving substitution failures in C++ concepts. (#GH176402)
+- Fixed a crash when a default argument is passed to an explicit object parameter. (#GH176639)
- Fixed a crash when diagnosing an invalid static member function with an explicit object parameter (#GH177741)
Bug Fixes to AST Handling
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c96189172a010..5837ecd6b9163 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11600,6 +11600,7 @@ void Sema::CheckExplicitObjectMemberFunction(Declarator &D,
Diag(ExplicitObjectParam->getLocation(),
diag::err_explicit_object_default_arg)
<< ExplicitObjectParam->getSourceRange();
+ D.setInvalidType();
}
if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 0d472a608fcca..96e8f48fb7497 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -1431,6 +1431,28 @@ namespace ConstexprBacktrace {
// expected-note {{in call to}}
}
+namespace GH176639 {
+
+struct S {
+ void operator()(this S =) // expected-error {{the explicit object parameter cannot have a default argument}}
+ // expected-error at -1 {{expected ';' at end of declaration list}}
+ // expected-error at -2 {{expected expression}}
+};
+
+void foo() {
+ S s{};
+ s(0); // expected-error {{no matching function for call to object of type 'S'}}
+}
+
+struct S2 {
+ void operator()(this S2 = S2 {}){} // expected-error {{the explicit object parameter cannot have a default argument}}
+};
+
+void foo2() {
+ S2 s{};
+ s(0); // expected-error {{no matching function for call to object of type 'S2'}}
+}
+
namespace GH177741 {
struct S {
>From c53cbe3ba9a38c9ead36731114cbeccb498bc99c Mon Sep 17 00:00:00 2001
From: hax0kartik <agarwala.kartik at gmail.com>
Date: Sat, 31 Jan 2026 19:06:48 +0530
Subject: [PATCH 2/2] Fix tests
---
clang/test/SemaCXX/cxx2b-deducing-this.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 96e8f48fb7497..995397f65b20c 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -1453,6 +1453,8 @@ void foo2() {
s(0); // expected-error {{no matching function for call to object of type 'S2'}}
}
+}
+
namespace GH177741 {
struct S {
More information about the cfe-commits
mailing list