[clang] [clang] Bail out if the result of function template instantiation is not a function. (PR #69449)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 04:39:43 PDT 2023


https://github.com/VitaNuo created https://github.com/llvm/llvm-project/pull/69449

None

>From 27dc15e7467afa7bc4ee17a907a088e59719ca5d Mon Sep 17 00:00:00 2001
From: Viktoriia Bakalova <bakalova at google.com>
Date: Wed, 18 Oct 2023 10:18:43 +0000
Subject: [PATCH] [clang] Bail out if the result of function template
 instantiation is not a function.

---
 clang/lib/Sema/SemaTemplateInstantiate.cpp        |  4 +++-
 .../function-decl-nested-type-alias.cpp           | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaTemplate/function-decl-nested-type-alias.cpp

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 3b1731edec95237..fd4dbcd2cd80587 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2649,7 +2649,9 @@ TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
   } else {
     Result = Instantiator.TransformType(TLB, TL);
   }
-  if (Result.isNull())
+  // When clang goes into recovery mode, it substitutes the incorrect function
+  // type with a primitive (e.g., int).
+  if (Result.isNull() || !Result->isFunctionType())
     return nullptr;
 
   return TLB.getTypeSourceInfo(Context, Result);
diff --git a/clang/test/SemaTemplate/function-decl-nested-type-alias.cpp b/clang/test/SemaTemplate/function-decl-nested-type-alias.cpp
new file mode 100644
index 000000000000000..45e03b3c6ba850c
--- /dev/null
+++ b/clang/test/SemaTemplate/function-decl-nested-type-alias.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -x c++ -std=c++14 -fsyntax-only -verify %s
+
+template <class A>
+using Type = typename A::NestedType; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
+
+template <typename T>
+void Func() {
+  using MyType = Type<T>(); // expected-note{{in instantiation of template type alias 'Type' requested here}}
+  MyType var;
+}
+
+void Test() {
+  Func<float>(); // expected-note{{in instantiation of function template specialization 'Func<float>' requested here}}
+}
+



More information about the cfe-commits mailing list