-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[Clang] Fix deduction of explicit object member functions #140030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+43
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When taking the address of an overload set containing an explicit object member, we should not take the explicit object parameter into account.
@llvm/pr-subscribers-clang Author: cor3ntin (cor3ntin) ChangesWhen taking the address of an overload set containing an explicit object member, we should not take the Full diff: https://github.com/llvm/llvm-project/pull/140030.diff 5 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 31c517338c21f..1f17bc6e5c8e3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -710,6 +710,7 @@ Bug Fixes to C++ Support
- Clang now correctly parses arbitrary order of ``[[]]``, ``__attribute__`` and ``alignas`` attributes for declarations (#GH133107)
- Fixed a crash when forming an invalid function type in a dependent context. (#GH138657) (#GH115725) (#GH68852)
- Clang no longer segfaults when there is a configuration mismatch between modules and their users (http://crbug.com/400353616).
+- Fix an incorrect deduction when calling an explicit object member function template through an overload set address.
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 6ea7ee281e14d..5ec67087aeea4 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -12576,6 +12576,7 @@ class Sema final : public SemaBase {
bool PartialOverloading, bool AggregateDeductionCandidate,
bool PartialOrdering, QualType ObjectType,
Expr::Classification ObjectClassification,
+ bool ForOverloadSetAddressResolution,
llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent);
/// Deduce template arguments when taking the address of a function
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index e20a41c10ccaa..23304e12f8c31 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -7846,6 +7846,8 @@ static void AddMethodTemplateCandidateImmediately(
MethodTmpl, ExplicitTemplateArgs, Args, Specialization, Info,
PartialOverloading, /*AggregateDeductionCandidate=*/false,
/*PartialOrdering=*/false, ObjectType, ObjectClassification,
+ CandidateSet.getKind() ==
+ clang::OverloadCandidateSet::CSK_AddressOfOverloadSet,
[&](ArrayRef<QualType> ParamTypes) {
return S.CheckNonDependentConversions(
MethodTmpl, ParamTypes, Args, CandidateSet, Conversions,
@@ -7960,6 +7962,8 @@ static void AddTemplateOverloadCandidateImmediately(
/*PartialOrdering=*/false,
/*ObjectType=*/QualType(),
/*ObjectClassification=*/Expr::Classification(),
+ CandidateSet.getKind() ==
+ OverloadCandidateSet::CSK_AddressOfOverloadSet,
[&](ArrayRef<QualType> ParamTypes) {
return S.CheckNonDependentConversions(
FunctionTemplate, ParamTypes, Args, CandidateSet, Conversions,
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 5dc06ebc2a235..217d57d67f067 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -4432,6 +4432,7 @@ TemplateDeductionResult Sema::DeduceTemplateArguments(
bool PartialOverloading, bool AggregateDeductionCandidate,
bool PartialOrdering, QualType ObjectType,
Expr::Classification ObjectClassification,
+ bool ForOverloadSetAddressResolution,
llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) {
if (FunctionTemplate->isInvalidDecl())
return TemplateDeductionResult::Invalid;
@@ -4440,7 +4441,15 @@ TemplateDeductionResult Sema::DeduceTemplateArguments(
unsigned NumParams = Function->getNumParams();
bool HasExplicitObject = false;
int ExplicitObjectOffset = 0;
- if (Function->hasCXXExplicitFunctionObjectParameter()) {
+
+ // [C++26] [over.call.func]p3
+ // If the primary-expression is the address of an overload set,
+ // the argument list is the same as the expression-list in the call.
+ // Otherwise, the argument list is the expression-list in the call augmented
+ // by the addition of an implied object argument as in a qualified function
+ // call.
+ if (!ForOverloadSetAddressResolution &&
+ Function->hasCXXExplicitFunctionObjectParameter()) {
HasExplicitObject = true;
ExplicitObjectOffset = 1;
}
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 7e392213710a4..2286da8d1c0e5 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -926,6 +926,33 @@ struct C {
(&fref)();
}
};
+
+struct CTpl {
+ template <typename T>
+ constexpr int c(this const CTpl&, T) { // #P2797-ctpl-1
+ return 42;
+ }
+
+ template <typename T>
+ void c(T)&; // #P2797-ctpl-2
+
+ template <typename T>
+ static void c(T = 0, T = 0); // #P2797-ctpl-3
+
+ void d() {
+ c(0); // expected-error {{call to member function 'c' is ambiguous}}
+ // expected-note@#P2797-ctpl-1{{candidate}}
+ // expected-note@#P2797-ctpl-2{{candidate}}
+ // expected-note@#P2797-ctpl-3{{candidate}}
+ (CTpl::c)(0); // expected-error {{call to member function 'c' is ambiguous}}
+ // expected-note@#P2797-ctpl-1{{candidate}}
+ // expected-note@#P2797-ctpl-2{{candidate}}
+ // expected-note@#P2797-ctpl-3{{candidate}}
+
+ static_assert((&CTpl::c)(CTpl{}, 0) == 42); // selects #1
+ }
+};
+
}
namespace GH85992 {
|
erichkeane
approved these changes
May 15, 2025
TIFitis
pushed a commit
to TIFitis/llvm-project
that referenced
this pull request
May 19, 2025
When taking the address of an overload set containing an explicit object member, we should not take the explicit object parameter into account.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When taking the address of an overload set containing an explicit object member, we should not take the
explicit object parameter into account.