[PATCH] D120255: [Concepts] Check constraints for explicit template instantiations

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 3 06:33:58 PST 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5aeaabf35ead: [Concepts] Check constraints for explicit template instantiations (authored by royjacobson, committed by erichkeane).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D120255

Files:
  clang/lib/Sema/SemaTemplateInstantiate.cpp
  clang/test/AST/constraints-explicit-instantiation.cpp


Index: clang/test/AST/constraints-explicit-instantiation.cpp
===================================================================
--- /dev/null
+++ clang/test/AST/constraints-explicit-instantiation.cpp
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1 -std=c++20 -ast-dump %s | FileCheck %s
+
+namespace PR46029 {
+
+template <int N>
+void canary1();
+template <int N>
+void canary2();
+
+template <int N>
+struct A {
+  void f() requires(N == 1) {
+    static_assert(N == 1);
+    canary1<N>();
+  }
+  void f() requires(N == 2) {
+    static_assert(N == 2);
+    canary2<N>();
+  }
+};
+
+// This checks that `canary1<1>` and `canaray2<2>` are instantiated, thus
+// indirectly validating that the correct candidates of `A::f` were really
+// instantiated each time. 
+// The `static_assert`s validate we don't instantiate wrong candidates.
+
+// CHECK:{{.*}}FunctionTemplateDecl {{.*}} canary1
+// CHECK:      {{.*}}TemplateArgument integral
+// CHECK-SAME: {{1$}}
+template struct A<1>;
+
+// CHECK:      {{.*}}FunctionTemplateDecl {{.*}} canary2
+// CHECK:      {{.*}}TemplateArgument integral
+// CHECK-SAME: {{2$}}
+template struct A<2>;
+
+template struct A<3>;
+}
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -3230,6 +3230,14 @@
       if (FunctionDecl *Pattern =
               Function->getInstantiatedFromMemberFunction()) {
 
+        if (Function->getTrailingRequiresClause()) {
+          ConstraintSatisfaction Satisfaction;
+          if (CheckFunctionConstraints(Function, Satisfaction) ||
+              !Satisfaction.IsSatisfied) {
+            continue;
+          }
+        }
+
         if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>())
           continue;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120255.412700.patch
Type: text/x-patch
Size: 1852 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220303/70604f81/attachment.bin>


More information about the cfe-commits mailing list