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

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 10:50:20 PST 2025


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

The `UnresolvedLookupExpr` doesn't get looked up and resolved again while it is in the non-dependent context. It propagates into the codegen phase and causing the assertion failure.

We attempt to determine if the enclosing function is templated before moving on with the substitution introduced in the https://github.com/llvm/llvm-project/commit/20a05677f9394d4bc9467fe7bc93a4ebd3aeda61. 

This fixes https://github.com/llvm/llvm-project/issues/122892. 

>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] [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



More information about the cfe-commits mailing list