Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Fix IsOverload for function templates #77323

Merged
merged 1 commit into from
Jan 8, 2024

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Jan 8, 2024

Functions which correspond but have different template parameter lists are not redeclarations.

Fixes a regression introduced by af4751

(The patch just moves the template parameters check above if the signature check)

Fixes #76358

Functions which correspond but have different template parameter
lists are not redeclarations.

Fixes a regression introduced by af4751

(The patch just moves the template parameters check above if the
signature check)

Fixes llvm#76358
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Functions which correspond but have different template parameter lists are not redeclarations.

Fixes a regression introduced by af4751

(The patch just moves the template parameters check above if the signature check)

Fixes #76358


Full diff: https://github.com/llvm/llvm-project/pull/77323.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaOverload.cpp (+37-37)
  • (modified) clang/test/CXX/over/over.load/p2-0x.cpp (+5)
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 07da5cb150b467..e6c267bb79e60b 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -1259,6 +1259,43 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
   if ((OldTemplate == nullptr) != (NewTemplate == nullptr))
     return true;
 
+  if (NewTemplate) {
+    // C++ [temp.over.link]p4:
+    //   The signature of a function template consists of its function
+    //   signature, its return type and its template parameter list. The names
+    //   of the template parameters are significant only for establishing the
+    //   relationship between the template parameters and the rest of the
+    //   signature.
+    //
+    // We check the return type and template parameter lists for function
+    // templates first; the remaining checks follow.
+    bool SameTemplateParameterList = SemaRef.TemplateParameterListsAreEqual(
+        NewTemplate, NewTemplate->getTemplateParameters(), OldTemplate,
+        OldTemplate->getTemplateParameters(), false, Sema::TPL_TemplateMatch);
+    bool SameReturnType = SemaRef.Context.hasSameType(
+        Old->getDeclaredReturnType(), New->getDeclaredReturnType());
+    // FIXME(GH58571): Match template parameter list even for non-constrained
+    // template heads. This currently ensures that the code prior to C++20 is
+    // not newly broken.
+    bool ConstraintsInTemplateHead =
+        NewTemplate->getTemplateParameters()->hasAssociatedConstraints() ||
+        OldTemplate->getTemplateParameters()->hasAssociatedConstraints();
+    // C++ [namespace.udecl]p11:
+    //   The set of declarations named by a using-declarator that inhabits a
+    //   class C does not include member functions and member function
+    //   templates of a base class that "correspond" to (and thus would
+    //   conflict with) a declaration of a function or function template in
+    //   C.
+    // Comparing return types is not required for the "correspond" check to
+    // decide whether a member introduced by a shadow declaration is hidden.
+    if (UseMemberUsingDeclRules && ConstraintsInTemplateHead &&
+        !SameTemplateParameterList)
+      return true;
+    if (!UseMemberUsingDeclRules &&
+        (!SameTemplateParameterList || !SameReturnType))
+      return true;
+  }
+
   // Is the function New an overload of the function Old?
   QualType OldQType = SemaRef.Context.getCanonicalType(Old->getType());
   QualType NewQType = SemaRef.Context.getCanonicalType(New->getType());
@@ -1410,43 +1447,6 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
     }
   }
 
-  if (NewTemplate) {
-    // C++ [temp.over.link]p4:
-    //   The signature of a function template consists of its function
-    //   signature, its return type and its template parameter list. The names
-    //   of the template parameters are significant only for establishing the
-    //   relationship between the template parameters and the rest of the
-    //   signature.
-    //
-    // We check the return type and template parameter lists for function
-    // templates first; the remaining checks follow.
-    bool SameTemplateParameterList = SemaRef.TemplateParameterListsAreEqual(
-        NewTemplate, NewTemplate->getTemplateParameters(), OldTemplate,
-        OldTemplate->getTemplateParameters(), false, Sema::TPL_TemplateMatch);
-    bool SameReturnType = SemaRef.Context.hasSameType(
-        Old->getDeclaredReturnType(), New->getDeclaredReturnType());
-    // FIXME(GH58571): Match template parameter list even for non-constrained
-    // template heads. This currently ensures that the code prior to C++20 is
-    // not newly broken.
-    bool ConstraintsInTemplateHead =
-        NewTemplate->getTemplateParameters()->hasAssociatedConstraints() ||
-        OldTemplate->getTemplateParameters()->hasAssociatedConstraints();
-    // C++ [namespace.udecl]p11:
-    //   The set of declarations named by a using-declarator that inhabits a
-    //   class C does not include member functions and member function
-    //   templates of a base class that "correspond" to (and thus would
-    //   conflict with) a declaration of a function or function template in
-    //   C.
-    // Comparing return types is not required for the "correspond" check to
-    // decide whether a member introduced by a shadow declaration is hidden.
-    if (UseMemberUsingDeclRules && ConstraintsInTemplateHead &&
-        !SameTemplateParameterList)
-      return true;
-    if (!UseMemberUsingDeclRules &&
-        (!SameTemplateParameterList || !SameReturnType))
-      return true;
-  }
-
   if (!UseOverrideRules) {
     Expr *NewRC = New->getTrailingRequiresClause(),
          *OldRC = Old->getTrailingRequiresClause();
diff --git a/clang/test/CXX/over/over.load/p2-0x.cpp b/clang/test/CXX/over/over.load/p2-0x.cpp
index 183f3cb322af7e..8fd9a1ce1e87ab 100644
--- a/clang/test/CXX/over/over.load/p2-0x.cpp
+++ b/clang/test/CXX/over/over.load/p2-0x.cpp
@@ -24,6 +24,11 @@ class Y {
   void k() &&; // expected-error{{cannot overload a member function with ref-qualifier '&&' with a member function without a ref-qualifier}}
 };
 
+struct GH76358 {
+    template<int> void f() && {}
+    template<typename T> void f() const {}
+};
+
 
 #if __cplusplus >= 202002L
 namespace GH58962 {

@cor3ntin
Copy link
Contributor Author

cor3ntin commented Jan 8, 2024

@erichkeane

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, I don't see any reason that anything in the middle would cause a problem, so moving this is fine.

@cor3ntin cor3ntin merged commit 69066ab into llvm:main Jan 8, 2024
6 checks passed
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Functions which correspond but have different template parameter lists
are not redeclarations.

Fixes a regression introduced by af4751

(The patch just moves the template parameters check above if the
signature check)

Fixes llvm#76358
Xazax-hun pushed a commit to swiftlang/llvm-project that referenced this pull request Nov 7, 2024
After Swift PR llvm#77323, we import C++ source locations for imported code.
This improves some diagnostics but also breaks some of the heuristics
LLDB have to select the right frame on traps. This PR marks the test
XFAIL until we figure out a better heuristic to stop at the right frame.
This is not a trivial question as we cannot just mark all C++ imported
source locations artifical, some of that code is user written, some is
not (coming from frameworks, STL). Moreover, some users might prefer to
stop in the C++ code and some users might prefer to stop in Swift code.
Xazax-hun pushed a commit to swiftlang/llvm-project that referenced this pull request Nov 7, 2024
After Swift PR llvm#77323, we import C++ source locations for imported code.
This improves some diagnostics but also breaks some of the heuristics
LLDB have to select the right frame on traps. This PR marks the test
XFAIL until we figure out a better heuristic to stop at the right frame.
This is not a trivial question as we cannot just mark all C++ imported
source locations artifical, some of that code is user written, some is
not (coming from frameworks, STL). Moreover, some users might prefer to
stop in the C++ code and some users might prefer to stop in Swift code.
Xazax-hun pushed a commit to swiftlang/llvm-project that referenced this pull request Nov 8, 2024
After Swift PR llvm#77323, we import C++ source locations for imported code.
This improves some diagnostics but also breaks some of the heuristics
LLDB have to select the right frame on traps. This PR marks the test
XFAIL until we figure out a better heuristic to stop at the right frame.
This is not a trivial question as we cannot just mark all C++ imported
source locations artifical, some of that code is user written, some is
not (coming from frameworks, STL). Moreover, some users might prefer to
stop in the C++ code and some users might prefer to stop in Swift code.

(cherry picked from commit 1c0bdf9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang trunk does not compile overloaded member functions with ref-qualifier
3 participants