[clang] [Clang] Fix handling of overloads differing only by constraints and ref-qualifiers (PR #192018)
Corentin Jabot via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 14 01:11:21 PDT 2026
https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/192018
We should only error about inconsistent qualifiers if the functions are actually overloads.
Fixes #120812
>From bb3201d099130ab8eabc62d9de07fccaaedef10f Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 14 Apr 2026 09:57:57 +0200
Subject: [PATCH] [Clang] Fix handling of overloads differing only by
constraints and ref-qualifiers.
We ashould only error about inconsistent qualifiers
if the functions are actually overloads.
Fixes #120812
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaOverload.cpp | 18 ++++++++++--------
clang/test/CXX/drs/cwg24xx.cpp | 9 +++++++++
3 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3e2d287d1eb1f..9e4699d642332 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -434,6 +434,7 @@ Bug Fixes to C++ Support
- We no longer caches invalid variable specializations. (#GH132592)
- Fixed an incorrect template argument deduction when matching packs of template
template parameters when one of its parameters is also a pack. (#GH181166)
+- Clang no longer errors on overloads with different ref-qualifiers and constraints. (#GH120812)
- Fixed a crash when a default argument is passed to an explicit object parameter. (#GH176639)
- Fixed an alias template CTAD crash.
- Fixed a crash when diagnosing an invalid static member function with an explicit object parameter (#GH177741)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 11e771bc240f1..5892a813c0f00 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -1521,6 +1521,8 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
return false;
};
+ bool ShouldDiagnoseInconsistentRefQualifiers = false;
+
if (OldMethod && OldMethod->isExplicitObjectMemberFunction())
OldParamsOffset++;
if (NewMethod && NewMethod->isExplicitObjectMemberFunction())
@@ -1557,8 +1559,7 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
}(OldMethod, NewMethod);
if (!HaveCorrespondingObjectParameters) {
- if (DiagnoseInconsistentRefQualifiers())
- return true;
+ ShouldDiagnoseInconsistentRefQualifiers = true;
// CWG2554
// and, if at least one is an explicit object member function, ignoring
// object parameters
@@ -1568,6 +1569,10 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
}
}
+ if (NewMethod && OldMethod && OldMethod->isImplicitObjectMemberFunction() &&
+ NewMethod->isImplicitObjectMemberFunction())
+ ShouldDiagnoseInconsistentRefQualifiers = true;
+
if (!UseOverrideRules &&
New->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
AssociatedConstraint NewRC = New->getTrailingRequiresClause(),
@@ -1582,12 +1587,6 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
return true;
}
- if (NewMethod && OldMethod && OldMethod->isImplicitObjectMemberFunction() &&
- NewMethod->isImplicitObjectMemberFunction()) {
- if (DiagnoseInconsistentRefQualifiers())
- return true;
- }
-
// Though pass_object_size is placed on parameters and takes an argument, we
// consider it to be a function-level modifier for the sake of function
// identity. Either the function has one or more parameters with
@@ -1612,6 +1611,9 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
return true;
}
+ if(ShouldDiagnoseInconsistentRefQualifiers && DiagnoseInconsistentRefQualifiers())
+ return true;
+
// At this point, it is known that the two functions have the same signature.
if (SemaRef.getLangOpts().CUDA && ConsiderCudaAttrs) {
// Don't allow overloading of destructors. (In theory we could, but it
diff --git a/clang/test/CXX/drs/cwg24xx.cpp b/clang/test/CXX/drs/cwg24xx.cpp
index b27c6b823940c..0cbf2cdfdb966 100644
--- a/clang/test/CXX/drs/cwg24xx.cpp
+++ b/clang/test/CXX/drs/cwg24xx.cpp
@@ -212,5 +212,14 @@ struct T : S {
virtual void k() &;
virtual void l() &&;
};
+#if __cplusplus >= 202002L
+
+// We should not error on inconsistent ref-qualifiers if the functions
+// have different constraints.
+template <unsigned R> struct type {
+ void func() const requires(R == 0);
+ void func() & requires(R == 1);
+};
+
#endif
}
More information about the cfe-commits
mailing list