[clang] [Clang] Fixed UnresolvedLookupExpr propagating into the codegen phase (PR #124609)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 28 09:09:56 PST 2025


https://github.com/TilakChad updated https://github.com/llvm/llvm-project/pull/124609

>From e97f2215394198b75fd6ebf6ef2cf5d63e7ea444 Mon Sep 17 00:00:00 2001
From: Tilak Chad <tilakchad111 at gmail.com>
Date: Tue, 28 Jan 2025 00:09:33 +0545
Subject: [PATCH 1/2] [Clang] Fixed UnresolvedLookupExpr propagating into the
 codegen phase

---
 clang/lib/Sema/SemaOverload.cpp               | 10 ++++-
 .../SemaCXX/deduced-return-type-cxx14.cpp     | 42 +++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 6ae9c51c06b315..b6cb358eb71c8b 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14228,9 +14228,17 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
     const FunctionDecl *FDecl = Best->Function;
     if (FDecl && FDecl->isTemplateInstantiation() &&
         FDecl->getReturnType()->isUndeducedType()) {
+
+      // UnresolvedLookupExpr will not be resolved again inside non-dependent
+      // function (i.e non-templated function in this case).
+      const FunctionDecl *EnclosingFn = getCurFunctionDecl();
+      const bool Resolvable =
+          EnclosingFn && EnclosingFn->getTemplatedKind() ==
+                             FunctionDecl::TemplatedKind::TK_FunctionTemplate;
+
       if (const auto *TP =
               FDecl->getTemplateInstantiationPattern(/*ForDefinition=*/false);
-          TP && TP->willHaveBody()) {
+          TP && TP->willHaveBody() && Resolvable) {
         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 c33e07088ba32f..aa62c4a57a6366 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 2204b252f3c0cb596fe52bd83f14fede5040dceb Mon Sep 17 00:00:00 2001
From: Tilak Chad <tilakchad111 at gmail.com>
Date: Tue, 28 Jan 2025 22:54:08 +0545
Subject: [PATCH 2/2] [Clang] Consider current DeclContext instead of enclosing
 function for checking dependence of UnresolvedLookupExpr

---
 clang/lib/Sema/SemaOverload.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index b6cb358eb71c8b..6fb68f186293e5 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14229,12 +14229,10 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
     if (FDecl && FDecl->isTemplateInstantiation() &&
         FDecl->getReturnType()->isUndeducedType()) {
 
-      // UnresolvedLookupExpr will not be resolved again inside non-dependent
-      // function (i.e non-templated function in this case).
-      const FunctionDecl *EnclosingFn = getCurFunctionDecl();
-      const bool Resolvable =
-          EnclosingFn && EnclosingFn->getTemplatedKind() ==
-                             FunctionDecl::TemplatedKind::TK_FunctionTemplate;
+      // As there'll be no attempt to resolve UnresolvedLookupExpr again inside
+      // non-dependent context, skip considering it as type-dependent.
+      const DeclContext *DC = CurContext;
+      const bool Resolvable = DC && DC->isDependentContext();
 
       if (const auto *TP =
               FDecl->getTemplateInstantiationPattern(/*ForDefinition=*/false);



More information about the cfe-commits mailing list