[PATCH] D38700: [Sema][Crash] Correctly handle an non-dependent noexcept expr in function template

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 9 11:39:16 PDT 2017


erichkeane created this revision.

It seems that all of the other templated cases are handled correctly,
however the function template case was not correctly handled.  This
patch recovers from this condition by setting the function to noexcept
after diagnosing.  Previously it simply set NoexceptExpr to null,
which caused an Assert when this was evaluated during substitution.


https://reviews.llvm.org/D38700

Files:
  lib/Sema/SemaDeclCXX.cpp
  test/CXX/except/except.spec/p1.cpp


Index: test/CXX/except/except.spec/p1.cpp
===================================================================
--- test/CXX/except/except.spec/p1.cpp
+++ test/CXX/except/except.spec/p1.cpp
@@ -86,3 +86,12 @@
     f<0>(); // expected-note{{in instantiation of function template specialization}}
   }
 }
+
+namespace FuncTmplNoexceptError {
+  int a = 0;
+  // expected-error at +1{{argument to noexcept specifier must be a constant expression}}
+  template <class T> T f() noexcept(a++){ return {};}
+  void g(){
+    f<int>();
+  }
+};
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp
+++ lib/Sema/SemaDeclCXX.cpp
@@ -14858,10 +14858,16 @@
         return;
       }
 
-      if (!NoexceptExpr->isValueDependent())
-        NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, nullptr,
-                         diag::err_noexcept_needs_constant_expression,
-                         /*AllowFold*/ false).get();
+      if (!NoexceptExpr->isValueDependent()) {
+        ExprResult Result = VerifyIntegerConstantExpression(
+            NoexceptExpr, nullptr, diag::err_noexcept_needs_constant_expression,
+            /*AllowFold*/ false);
+        if (Result.isInvalid()) {
+          ESI.Type = EST_BasicNoexcept;
+          return;
+        }
+        NoexceptExpr = Result.get();
+      }
       ESI.NoexceptExpr = NoexceptExpr;
     }
     return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38700.118245.patch
Type: text/x-patch
Size: 1435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171009/7335374a/attachment-0001.bin>


More information about the cfe-commits mailing list