Skip to content

[Clang][Sema] ASTContext::getUnconstrainedType propagates dependence #92425

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

Merged
merged 3 commits into from
May 16, 2024

Conversation

sdkrystian
Copy link
Member

@sdkrystian sdkrystian commented May 16, 2024

When the argument passed to ASTContext::getUnconstrainedType is an unconstrained AutoType, will return the argument unchanged. However, when called with a constrained AutoType, an unconstrained, non-dependent AutoType will be returned even if the argument was dependent. Consider the following:

template<typename T>
concept C = sizeof(T) == sizeof(int);

template<auto N>
struct A;

template<C auto N>
struct A<N>; // error: class template partial specialization is not more specialized than the primary template

When comparing the template parameters for equivalence, ASTContext::getUnconstrainedType is used to remove the constraints per [temp.over.link] p6 sentence 2. For the template parameter N of the class template, it returns a dependent AutoType. For the template parameter N of the class template partial specialization, it returns a non-dependent AutoType. We subsequently compare the adjusted types and find they are not equivalent, thus we consider the partial specialization to not be more specialized than the primary template per [temp.func.order] p6.2.2.

This patch changes ASTContext::getUnconstrainedType such that the dependence of a constrained AutoType will propagate to the returned unconstrained AutoType. This causes the above example to be correctly accepted, fixing #77377.

@sdkrystian sdkrystian requested a review from erichkeane May 16, 2024 16:16
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 16, 2024
@sdkrystian sdkrystian requested review from zyn0217 and jcsxky May 16, 2024 16:16
@llvmbot
Copy link
Member

llvmbot commented May 16, 2024

@llvm/pr-subscribers-clang

Author: Krystian Stasiowski (sdkrystian)

Changes

When the argument passed to ASTContext::getUnconstrainedType is an unconstrained AutoType, will return the argument unchanged. However, when called with a constrained AutoType, an unconstrained, non-dependent AutoType will be returned even if the argument was dependent. Consider the following:

template&lt;typename T&gt;
concept C = sizeof(T) == sizeof(int);

template&lt;auto N&gt;
struct A;

template&lt;C auto N&gt;
struct A&lt;N&gt; { }; // error: class template partial specialization is not more specialized than the primary template

When comparing the template parameters for equivalence, ASTContext::getUnconstrainedType is used to remove the constraints per [[temp.over.link] p6 sentence 2](http://eel.is/c++draft/temp.over.link#6.sentence-2). For the template parameter N of the class template, it returns a dependent AutoType. For the template parameter N of the class template partial specialization, it returns a non-dependent AutoType. We subsequently compare the adjusted types and find they are not equivalent, thus we consider the partial specialization to not be more specialized than the primary template per [[temp.func.order] p6.2.2](http://eel.is/c++draft/temp.func.order#6.2.2).

This patch changes ASTContext::getUnconstrainedType such that the dependence of a constrained AutoType will propagate to the returned unconstrained AutoType. This causes the above example to be correctly accepted, fixing #77377.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/AST/ASTContext.cpp (+2-1)
  • (added) clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p2.cpp (+11)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index be4cded276321..21f273cf8f54e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -723,6 +723,7 @@ Bug Fixes to C++ Support
 - Clang now ignores template parameters only used within the exception specification of candidate function
   templates during partial ordering when deducing template arguments from a function declaration or when
   taking the address of a function template.
+- Fix a bug with checking constrained non-type template parameters for equivalence.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4475f399a120b..8fc2bb8c401c2 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -5910,7 +5910,8 @@ QualType ASTContext::getUnconstrainedType(QualType T) const {
   if (auto *AT = CanonT->getAs<AutoType>()) {
     if (!AT->isConstrained())
       return T;
-    return getQualifiedType(getAutoType(QualType(), AT->getKeyword(), false,
+    return getQualifiedType(getAutoType(QualType(), AT->getKeyword(),
+                                        AT->isDependentType(),
                                         AT->containsUnexpandedParameterPack()),
                             T.getQualifiers());
   }
diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p2.cpp b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p2.cpp
new file mode 100644
index 0000000000000..5c908d789d511
--- /dev/null
+++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p2.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+// expected-no-diagnostics
+
+template<typename T>
+concept C = sizeof(T) == sizeof(int);
+
+template<auto N>
+struct A;
+
+template<C auto N>
+struct A<N> { };

@sdkrystian sdkrystian force-pushed the partial-spec-dependent-auto branch from 7905c01 to c26365e Compare May 16, 2024 16:17
Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

LGTM

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.

5 participants