[clang] ddfbdbf - [clang] Do not crash on template specialization following a fatal error

Adam Czachorowski via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 23 04:40:58 PDT 2021


Author: Adam Czachorowski
Date: 2021-04-23T13:34:05+02:00
New Revision: ddfbdbfefae04ea71391a38ed5e9cb6975f6630b

URL: https://github.com/llvm/llvm-project/commit/ddfbdbfefae04ea71391a38ed5e9cb6975f6630b
DIFF: https://github.com/llvm/llvm-project/commit/ddfbdbfefae04ea71391a38ed5e9cb6975f6630b.diff

LOG: [clang] Do not crash on template specialization following a fatal error

There was a missing isInvalid() check leading to an attempt to
instantiate template with an empty instantiation stack.

Differential Revision: https://reviews.llvm.org/D100675

Added: 
    clang/test/SemaCXX/template-specialization-fatal.cpp

Modified: 
    clang/lib/Sema/SemaTemplateDeduction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index d755696c698c6..7d548e4f8dee1 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -5466,6 +5466,9 @@ static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2,
                                                Deduced.end());
   Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs,
                                    Info);
+  if (Inst.isInvalid())
+    return false;
+
   auto *TST1 = T1->castAs<TemplateSpecializationType>();
   bool AtLeastAsSpecialized;
   S.runWithSufficientStackSpace(Info.getLocation(), [&] {

diff  --git a/clang/test/SemaCXX/template-specialization-fatal.cpp b/clang/test/SemaCXX/template-specialization-fatal.cpp
new file mode 100644
index 0000000000000..2191e54c8f162
--- /dev/null
+++ b/clang/test/SemaCXX/template-specialization-fatal.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s
+// Verify clang doesn't assert()-fail on template specialization happening after
+// fatal error.
+
+#include "not_found.h" // expected-error {{'not_found.h' file not found}}
+
+template <class A, class B, class = void>
+struct foo {};
+
+template <class A, class B>
+struct foo<A, B, decltype(static_cast<void (*)(B)>(0)(static_cast<A (*)()>(0)()))> {};


        


More information about the cfe-commits mailing list