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

2025-03-13 Thread Erich Keane via cfe-commits

https://github.com/erichkeane approved this pull request.

I'm going to give a conditional approval.  My comfort level is reasonable right 
now, but I want time to think about it.  So if I haven't responded by ~EOD 
Monday (or if no one else did other comments), feel free to merge.

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread via cfe-commits

TilakChad wrote:

I've incorporated your feedback and updated the PR. 

Let me know if I have to change anything. 
Thanks for taking your time to review this PR. 

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread via cfe-commits

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 
Date: Fri, 14 Mar 2025 00:44:10 +0545
Subject: [PATCH] [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 
+static auto c2(T x) { // since-cxx14-note {{declared here}}
+return x;
+}
+  };
+
+  struct FunctionTemplateSpecialized {
+template 
+void specialized(){}
+
+template <>
+void specialized() {
+  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 
+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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Younan Zhang via cfe-commits


@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in 
function prototype}}
   return f(1) + 1;
 }
 
+namespace GH122892 {

zyn0217 wrote:

Oh so it is the case - looks like we now have errors for the case in question.

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits


@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in 
function prototype}}
   return f(1) + 1;
 }
 
+namespace GH122892 {

erichkeane wrote:

Yeah, so this patch now prevents an `UnresolvedLookupExpr` from being put into 
the AST for a non-dependent function.  The symptom of that (besides the 
AST-dump) was that CodeGen didn't know how to handle that for obvious reasons.

This patch is now preventing that `UnresolvedLookupExpr` from being put into 
the AST for non-dependent functions, and falls-thru to emitting an error.

I Think.

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Younan Zhang via cfe-commits


@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in 
function prototype}}
   return f(1) + 1;
 }
 
+namespace GH122892 {

zyn0217 wrote:

I don't know, but the original issue doesn't manifest with `-fsyntax-only` 
https://godbolt.org/z/MdMzjEd5q

So if this patch changed the behavior in that mode (e.g. diagnose out errors 
earlier) then it probably makes sense to have the test live in SemaCXX, just my 
thoughts :P

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits


@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in 
function prototype}}
   return f(1) + 1;
 }
 
+namespace GH122892 {

erichkeane wrote:

I don't think so?  Since this is replacing the condition that previously 
crashed in codegen with a sema-error, right?

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Younan Zhang via cfe-commits


@@ -703,6 +703,48 @@ auto f(auto x) { // cxx14-error {{'auto' not allowed in 
function prototype}}
   return f(1) + 1;
 }
 
+namespace GH122892 {

zyn0217 wrote:

I think the test should live in CodeGen/ rather than SemaCXX/, which mostly 
contains '-fsyntax-only' tests, while the issue arises in the codegen stage?

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits

https://github.com/erichkeane edited 
https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits


@@ -14228,9 +14228,15 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, 
Expr *Fn,
 const FunctionDecl *FDecl = Best->Function;
 if (FDecl && FDecl->isTemplateInstantiation() &&
 FDecl->getReturnType()->isUndeducedType()) {
+
+  // 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();

erichkeane wrote:

`Resolvable` isn't a good name here, it sorta implies the opposite of what we 
mean, but not to the point of `NotResolvable` being a good idea.  I think just 
putting `&& CurContext->isDependentContext()` below is actually more readable.  
Also, you don't really have to check the `DeclContext`, having a null one I 
think requires that you be outside of a `TranslationUnitDecl`, which isn't 
possible. 

I see that we are just going through `FinishOverloadedCallExpr` in the event 
that we are going to instantiate this again, which I guess makes sense, since 
this is creating a call with a `DependentTy`, but this comment doesn't make it 
clear what i   I think I'd be happy with a better commit message explaining the 
whole situation and why this works.  I've debugged a while and think I have a 
good hold on it though, so just a better description I think would help

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits

https://github.com/erichkeane commented:

I have minor suggestions, and am not quite comfortable with this yet, so please 
improve the commit message and make the change I requested (re `Resolvable`), 
and I'll revisit this afterwards.

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread Erich Keane via cfe-commits

erichkeane wrote:

> Hi @erichkeane Is this resolution still not good enough? I should probably 
> close the PR if thats the case.

I apparently didn't see your last change since your last comment.  Looking now.

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-03-13 Thread via cfe-commits

TilakChad wrote:

Hi @erichkeane 

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-01-29 Thread via cfe-commits

https://github.com/TilakChad edited 
https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-01-28 Thread via cfe-commits

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 
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 
+static auto c2(T x) { // since-cxx14-note {{declared here}}
+return x;
+}
+  };
+
+  struct FunctionTemplateSpecialized {
+template 
+void specialized(){}
+
+template <>
+void specialized() {
+  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 
+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 
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);

___
cfe-commits mailing list
cfe-co

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

2025-01-27 Thread Erich Keane via cfe-commits

erichkeane wrote:

This doesn't quite seem right to me... First, I would expect us to check the 
declaration context here rather than whether it is in a function template.  

Second, I find myself thinking the `UnresolvedLookupExpr` in a non-dependent 
context is the actual problem, and we need to figure out how we got there, as 
that is the big problem.  

https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-01-27 Thread via cfe-commits

https://github.com/TilakChad edited 
https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-01-27 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: TilakChad (TilakChad)


Changes

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. 

---
Full diff: https://github.com/llvm/llvm-project/pull/124609.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaOverload.cpp (+9-1) 
- (modified) clang/test/SemaCXX/deduced-return-type-cxx14.cpp (+42) 


``diff
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 
+static auto c2(T x) { // since-cxx14-note {{declared here}}
+return x;
+}
+  };
+
+  struct FunctionTemplateSpecialized {
+template 
+void specialized(){}
+
+template <>
+void specialized() {
+  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 
+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

``




https://github.com/llvm/llvm-project/pull/124609
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-01-27 Thread via cfe-commits

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 
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 
+static auto c2(T x) { // since-cxx14-note {{declared here}}
+return x;
+}
+  };
+
+  struct FunctionTemplateSpecialized {
+template 
+void specialized(){}
+
+template <>
+void specialized() {
+  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 
+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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits