Skip to content

Commit 2ec38ab

Browse files
zyn0217tru
authored andcommitted
[Clang] Fix a partial ordering bug involving CTAD injected template arguments (#149782)
The synthesized deduction guides use injected template arguments for distinguishment of explicit and implicit deduction guides. In partial ordering, we may substitute into these injected types when checking consistency. Properly substituting them needs the instantiated class template specializations which isn't the case at that point. So instead, we check their template specialization types. No release note because I think we want a backport, after baking it for a couple of days. Fixes #134613 (cherry picked from commit 07faafe)
1 parent a3d182b commit 2ec38ab

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

clang/lib/Sema/SemaTemplateDeduction.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5525,6 +5525,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
55255525
// FIXME: A substitution can be incomplete on a non-structural part of the
55265526
// type. Use the canonical type for now, until the TemplateInstantiator can
55275527
// deal with that.
5528+
5529+
// Workaround: Implicit deduction guides use InjectedClassNameTypes, whereas
5530+
// the explicit guides don't. The substitution doesn't transform these types,
5531+
// so let it transform their specializations instead.
5532+
bool IsDeductionGuide = isa<CXXDeductionGuideDecl>(FTD->getTemplatedDecl());
5533+
if (IsDeductionGuide) {
5534+
if (auto *Injected = P->getAs<InjectedClassNameType>())
5535+
P = Injected->getInjectedSpecializationType();
5536+
}
55285537
QualType InstP = S.SubstType(P.getCanonicalType(), MLTAL, FTD->getLocation(),
55295538
FTD->getDeclName(), &IsIncompleteSubstitution);
55305539
if (InstP.isNull() && !IsIncompleteSubstitution)
@@ -5539,9 +5548,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
55395548
if (auto *PA = dyn_cast<PackExpansionType>(A);
55405549
PA && !isa<PackExpansionType>(InstP))
55415550
A = PA->getPattern();
5542-
if (!S.Context.hasSameType(
5543-
S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType()),
5544-
S.Context.getUnqualifiedArrayType(A.getNonReferenceType())))
5551+
auto T1 = S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType());
5552+
auto T2 = S.Context.getUnqualifiedArrayType(A.getNonReferenceType());
5553+
if (IsDeductionGuide) {
5554+
if (auto *Injected = T1->getAs<InjectedClassNameType>())
5555+
T1 = Injected->getInjectedSpecializationType();
5556+
if (auto *Injected = T2->getAs<InjectedClassNameType>())
5557+
T2 = Injected->getInjectedSpecializationType();
5558+
}
5559+
if (!S.Context.hasSameType(T1, T2))
55455560
return TemplateDeductionResult::NonDeducedMismatch;
55465561
return TemplateDeductionResult::Success;
55475562
}

clang/test/SemaTemplate/deduction-guide.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,3 +966,19 @@ Expand<Type, Invocable<>> _{};
966966
// CHECK-NEXT: | `-ParmVarDecl {{.+}} 'T...' pack
967967

968968
}
969+
970+
namespace GH134613 {
971+
template <typename R> struct Foo {
972+
using value_type = R;
973+
974+
Foo() = default;
975+
Foo(Foo<Foo<R>> &&rhs) {}
976+
};
977+
978+
void main() {
979+
auto r1 = Foo(Foo<Foo<int>>{});
980+
981+
static_assert(__is_same(decltype(r1)::value_type, int));
982+
}
983+
984+
}

0 commit comments

Comments
 (0)