[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:55 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: TilakChad (TilakChad)
<details>
<summary>Changes</summary>
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 <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
``````````
</details>
https://github.com/llvm/llvm-project/pull/124609
More information about the cfe-commits
mailing list