[PATCH] D128119: [clang] Enforce instantiation of constexpr template functions during non-constexpr evaluation

serge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 8 10:02:43 PDT 2022


serge-sans-paille updated this revision to Diff 443276.
serge-sans-paille added a comment.

+ release note
+ take review into account


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128119/new/

https://reviews.llvm.org/D128119

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
  clang/test/SemaCXX/constexpr-late-instantiation.cpp


Index: clang/test/SemaCXX/constexpr-late-instantiation.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-late-instantiation.cpp
@@ -0,0 +1,15 @@
+// Make sure foo is instantiated and we don't get a link error
+// RUN: not %clang_cc1 %s -fsyntax-only -verify
+
+template <typename T>
+constexpr T foo(T a);
+
+int main() {
+  int k = foo<int>(5);           // Ok
+  constexpr int j = foo<int>(5); // expected-error {{constexpr variable 'j' must be initialized by a constant expression}}
+}
+
+template <typename T>
+constexpr T foo(T a) {
+  return a;
+}
Index: clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/constexpr-late-instantiation.cpp
@@ -0,0 +1,17 @@
+// Make sure foo is instantiated and we don't get a link error
+// RUN: %clang_cc1 -S -emit-llvm %s -o- | FileCheck %s
+
+template <typename T>
+constexpr T foo(T a);
+
+// CHECK-LABEL: define {{.*}} @main
+int main() {
+  // CHECK: call {{.*}} @_Z3fooIiET_S0_
+  int k = foo<int>(5);
+}
+// CHECK: }
+
+template <typename T>
+constexpr T foo(T a) {
+  return a;
+}
Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4840,7 +4840,8 @@
                                      /*Complain*/DefinitionRequired)) {
     if (DefinitionRequired)
       Function->setInvalidDecl();
-    else if (TSK == TSK_ExplicitInstantiationDefinition) {
+    else if (TSK == TSK_ExplicitInstantiationDefinition ||
+             (Function->isConstexpr() && !Recursive)) {
       // Try again at the end of the translation unit (at which point a
       // definition will be required).
       assert(!Recursive);
@@ -4855,7 +4856,7 @@
         Diag(PatternDecl->getLocation(), diag::note_forward_template_decl);
         if (getLangOpts().CPlusPlus11)
           Diag(PointOfInstantiation, diag::note_inst_declaration_hint)
-            << Function;
+              << Function;
       }
     }
 
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -181,6 +181,8 @@
   emitted as a dynamic initializer. Previously the variable would
   incorrectly be zero-initialized. In contexts where a dynamic
   initializer is not allowed this is now diagnosed as an error.
+- Clang now correctly emit symbols for implicitly instanciated constexpr
+  template function. Fixes `Issue 55560 <https://github.com/llvm/llvm-project/issues/55560>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128119.443276.patch
Type: text/x-patch
Size: 2822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220708/6591ef73/attachment.bin>


More information about the cfe-commits mailing list