Skip to content

Commit da928c6

Browse files
authored
[Clang][Sema] ASTContext::getUnconstrainedType propagates dependence (#92425)
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.
1 parent 23f1047 commit da928c6

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ Bug Fixes to C++ Support
723723
- Clang now ignores template parameters only used within the exception specification of candidate function
724724
templates during partial ordering when deducing template arguments from a function declaration or when
725725
taking the address of a function template.
726+
- Fix a bug with checking constrained non-type template parameters for equivalence. Fixes (#GH77377).
726727

727728
Bug Fixes to AST Handling
728729
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5910,7 +5910,8 @@ QualType ASTContext::getUnconstrainedType(QualType T) const {
59105910
if (auto *AT = CanonT->getAs<AutoType>()) {
59115911
if (!AT->isConstrained())
59125912
return T;
5913-
return getQualifiedType(getAutoType(QualType(), AT->getKeyword(), false,
5913+
return getQualifiedType(getAutoType(QualType(), AT->getKeyword(),
5914+
AT->isDependentType(),
59145915
AT->containsUnexpandedParameterPack()),
59155916
T.getQualifiers());
59165917
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
2+
// expected-no-diagnostics
3+
4+
namespace GH77377 {
5+
template<typename T>
6+
concept C = sizeof(T) == sizeof(int);
7+
8+
template<auto N>
9+
struct A;
10+
11+
template<C auto N>
12+
struct A<N>;
13+
}

0 commit comments

Comments
 (0)