[PATCH] D136545: [Clang] use non-template function declaration for constraints partial ordering

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 22 21:30:21 PDT 2022


ychen created this revision.
ychen added reviewers: erichkeane, royjacobson, mizvekov.
Herald added a project: All.
ychen requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Per wordings in

- https://eel.is/c++draft/over.match#best.general-2.6
- https://eel.is/c++draft/temp.constr.order
- https://eel.is/c++draft/temp.constr#atomic-1

constraints partial ordering should use the unsubstituted template
parameters of the constrained entity, not the instantiated entity.

Fix #56154


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136545

Files:
  clang/lib/Sema/SemaOverload.cpp
  clang/test/CXX/over/over.match/over.match.best/p2.cpp
  clang/test/CXX/temp/temp.constr/temp.constr.order/non-template-functions.cpp


Index: clang/test/CXX/temp/temp.constr/temp.constr.order/non-template-functions.cpp
===================================================================
--- /dev/null
+++ clang/test/CXX/temp/temp.constr/temp.constr.order/non-template-functions.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -std=c++2a -x c++ -verify %s
+// expected-no-diagnostics
+
+namespace PR56154 {
+  template <int N> concept C0 = (N == 0);
+  template <int N, int N2> concept C0x = C0<N>;
+  template <int N1, int N2> concept C00 = C0x<N1, N2> && C0<N2>;
+
+  template<int N1, int N2>
+  struct A {
+     void f() requires C00<N1, N2>;
+     void f() requires C0x<N1, N2>;
+  };
+  void h(A<0, 0> a) {
+   a.f();
+  }
+}
Index: clang/test/CXX/over/over.match/over.match.best/p2.cpp
===================================================================
--- clang/test/CXX/over/over.match/over.match.best/p2.cpp
+++ clang/test/CXX/over/over.match/over.match.best/p2.cpp
@@ -7,8 +7,7 @@
     bool operator<(const A&) const & requires X<T>; // #1
     int operator<=>(const A&) const & requires X<T> && X<int> = delete; // #2
   };
-  bool k1 = A<int>() < A<int>(); // not ordered by constraints: prefer non-rewritten form
-  bool k2 = A<float>() < A<float>(); // prefer more-constrained 'operator<=>'
+  bool k = A<float>() < A<float>(); // prefer more-constrained 'operator<=>'
   // expected-error at -1 {{deleted}}
   // expected-note@#1 {{candidate}}
   // expected-note@#2 {{candidate function has been explicitly deleted}}
Index: clang/lib/Sema/SemaOverload.cpp
===================================================================
--- clang/lib/Sema/SemaOverload.cpp
+++ clang/lib/Sema/SemaOverload.cpp
@@ -10033,13 +10033,20 @@
   //      parameter-type-lists, and F1 is more constrained than F2 [...],
   if (!Cand1IsSpecialization && !Cand2IsSpecialization &&
       sameFunctionParameterTypeLists(S, Cand1, Cand2)) {
-    Expr *RC1 = Cand1.Function->getTrailingRequiresClause();
-    Expr *RC2 = Cand2.Function->getTrailingRequiresClause();
+    FunctionDecl *Function1 = Cand1.Function;
+    FunctionDecl *Function2 = Cand2.Function;
+    if (FunctionDecl *MF = Function1->getInstantiatedFromMemberFunction())
+      Function1 = MF;
+    if (FunctionDecl *MF = Function2->getInstantiatedFromMemberFunction())
+      Function2 = MF;
+
+    Expr *RC1 = Function1->getTrailingRequiresClause();
+    Expr *RC2 = Function2->getTrailingRequiresClause();
     if (RC1 && RC2) {
       bool AtLeastAsConstrained1, AtLeastAsConstrained2;
-      if (S.IsAtLeastAsConstrained(Cand1.Function, {RC1}, Cand2.Function, {RC2},
+      if (S.IsAtLeastAsConstrained(Function1, {RC1}, Function2, {RC2},
                                    AtLeastAsConstrained1) ||
-          S.IsAtLeastAsConstrained(Cand2.Function, {RC2}, Cand1.Function, {RC1},
+          S.IsAtLeastAsConstrained(Function2, {RC2}, Function1, {RC1},
                                    AtLeastAsConstrained2))
         return false;
       if (AtLeastAsConstrained1 != AtLeastAsConstrained2)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136545.469958.patch
Type: text/x-patch
Size: 3010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221023/7274014d/attachment.bin>


More information about the cfe-commits mailing list