[clang] [Clang] Do not create dependent CallExpr having UnresolvedLookupExpr inside non-dependent context (PR #124609)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 19 11:50:52 PDT 2025
https://github.com/TilakChad updated https://github.com/llvm/llvm-project/pull/124609
>From c865ba50fd3c1a5d427069e29e035c1d6e3d21d3 Mon Sep 17 00:00:00 2001
From: Tilak Chad <tilakchad111 at gmail.com>
Date: Fri, 14 Mar 2025 00:44:10 +0545
Subject: [PATCH 1/3] [Clang] Dependent CallExpr having UnresolvedLookupExpr
are not created inside non-dependent context
---
clang/lib/Sema/SemaOverload.cpp | 17 +++++++-
.../SemaCXX/deduced-return-type-cxx14.cpp | 42 +++++++++++++++++++
2 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 6ae9c51c06b31..d99657d4d67b9 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14228,9 +14228,24 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
const FunctionDecl *FDecl = Best->Function;
if (FDecl && FDecl->isTemplateInstantiation() &&
FDecl->getReturnType()->isUndeducedType()) {
+
+ // Creating dependent CallExpr is not okay if the enclosing context itself
+ // is not dependent. This situation notably arises if a non-dependent
+ // member function calls the later-defined overloaded static function.
+ //
+ // For example, in
+ // class A {
+ // void c() { callee(1); }
+ // static auto callee(auto x) { }
+ // };
+ //
+ // Here callee(1) is unresolved at the call site, but is not inside a
+ // dependent context. There will be no further attempt to resolve this
+ // call if it is made dependent.
+
if (const auto *TP =
FDecl->getTemplateInstantiationPattern(/*ForDefinition=*/false);
- TP && TP->willHaveBody()) {
+ TP && TP->willHaveBody() && CurContext->isDependentContext()) {
return CallExpr::Create(Context, Fn, Args, Context.DependentTy,
VK_PRValue, RParenLoc, CurFPFeatureOverrides());
}
diff --git a/clang/test/SemaCXX/deduced-return-type-cxx14.cpp b/clang/test/SemaCXX/deduced-return-type-cxx14.cpp
index c33e07088ba32..aa62c4a57a636 100644
--- a/clang/test/SemaCXX/deduced-return-type-cxx14.cpp
+++ b/clang/test/SemaCXX/deduced-return-type-cxx14.cpp
@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in function prototype}}
return f(1) + 1;
}
+namespace GH122892 {
+ struct NonTemplate {
+ void caller() {
+ c1(int{}); // since-cxx20-error {{cannot be used before it is defined}}
+ c2(int{}); // since-cxx14-error {{cannot be used before it is defined}}
+ }
+
+ static auto c1(auto x) { // since-cxx20-note {{declared here}} // cxx14-error {{'auto' not allowed in function prototype}}
+ }
+
+ template <typename T>
+ static auto c2(T x) { // since-cxx14-note {{declared here}}
+ return x;
+ }
+ };
+
+ struct FunctionTemplateSpecialized {
+ template <typename T>
+ void specialized(){}
+
+ template <>
+ void specialized<int>() {
+ c1(int{}); // since-cxx20-error {{cannot be used before it is defined}}
+ c2(int{}); // since-cxx14-error {{cannot be used before it is defined}}
+ }
+
+ static auto c1(auto x) { // since-cxx20-note {{declared here}} // cxx14-error {{'auto' not allowed in function prototype}}
+ }
+
+ template <typename T>
+ static auto c2(T x) { // since-cxx14-note {{declared here}}
+ return x;
+ }
+ };
+
+ struct MemberInit {
+ int x1 = c1(int{}); // since-cxx20-error {{cannot be used before it is defined}}
+
+ static auto c1(auto x) { return x; } // since-cxx20-note {{declared here}} // cxx14-error {{'auto' not allowed in function prototype}}
+ };
+
+}
}
#if __cplusplus >= 202002L
>From 2c2f30b456e5c4f654660217b1ae88344763dc17 Mon Sep 17 00:00:00 2001
From: Tilak Chad <tilakchad111 at gmail.com>
Date: Tue, 18 Mar 2025 22:34:33 +0545
Subject: [PATCH 2/3] [Clang] Updated the clang release notes
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 031c5d84e49f9..ee352771bd38c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1007,6 +1007,7 @@ Bug Fixes to C++ Support
- Fix type of expression when calling a template which returns an ``__array_rank`` querying a type depending on a
template parameter. Now, such expression can be used with ``static_assert`` and ``constexpr``. (#GH123498)
- Correctly determine the implicit constexprness of lambdas in dependent contexts. (#GH97958) (#GH114234)
+- Fixed a clang regression on c++20 mode where unresolved dependent call expressions were created inside non-dependent context (#GH122892)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
>From 797a385e4cd681ebea8570a9459a7e298435dfba Mon Sep 17 00:00:00 2001
From: TilakChad <49703944+TilakChad at users.noreply.github.com>
Date: Tue, 18 Mar 2025 23:00:06 +0545
Subject: [PATCH 3/3] [Clang] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 97269df111ba2..0d0353633118b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -337,7 +337,7 @@ Bug Fixes to C++ Support
- Fixed an assertion failure affecting code that uses C++23 "deducing this". (#GH130272)
- Clang now properly instantiates destructors for initialized members within non-delegating constructors. (#GH93251)
- Correctly diagnoses if unresolved using declarations shadows template paramters (#GH129411)
-- Fixed a clang regression on c++20 mode where unresolved dependent call expressions were created inside non-dependent context (#GH122892)
+- Fixed a Clang regression in C++20 mode where unresolved dependent call expressions were created inside non-dependent contexts (#GH122892)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list