Skip to content

Commit 07faafe

Browse files
authored
[Clang] Fix a partial ordering bug involving CTAD injected template arguments (llvm#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 llvm#134613
1 parent eb43b79 commit 07faafe

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
@@ -5523,6 +5523,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
55235523
// FIXME: A substitution can be incomplete on a non-structural part of the
55245524
// type. Use the canonical type for now, until the TemplateInstantiator can
55255525
// deal with that.
5526+
5527+
// Workaround: Implicit deduction guides use InjectedClassNameTypes, whereas
5528+
// the explicit guides don't. The substitution doesn't transform these types,
5529+
// so let it transform their specializations instead.
5530+
bool IsDeductionGuide = isa<CXXDeductionGuideDecl>(FTD->getTemplatedDecl());
5531+
if (IsDeductionGuide) {
5532+
if (auto *Injected = P->getAs<InjectedClassNameType>())
5533+
P = Injected->getInjectedSpecializationType();
5534+
}
55265535
QualType InstP = S.SubstType(P.getCanonicalType(), MLTAL, FTD->getLocation(),
55275536
FTD->getDeclName(), &IsIncompleteSubstitution);
55285537
if (InstP.isNull() && !IsIncompleteSubstitution)
@@ -5537,9 +5546,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
55375546
if (auto *PA = dyn_cast<PackExpansionType>(A);
55385547
PA && !isa<PackExpansionType>(InstP))
55395548
A = PA->getPattern();
5540-
if (!S.Context.hasSameType(
5541-
S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType()),
5542-
S.Context.getUnqualifiedArrayType(A.getNonReferenceType())))
5549+
auto T1 = S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType());
5550+
auto T2 = S.Context.getUnqualifiedArrayType(A.getNonReferenceType());
5551+
if (IsDeductionGuide) {
5552+
if (auto *Injected = T1->getAs<InjectedClassNameType>())
5553+
T1 = Injected->getInjectedSpecializationType();
5554+
if (auto *Injected = T2->getAs<InjectedClassNameType>())
5555+
T2 = Injected->getInjectedSpecializationType();
5556+
}
5557+
if (!S.Context.hasSameType(T1, T2))
55435558
return TemplateDeductionResult::NonDeducedMismatch;
55445559
return TemplateDeductionResult::Success;
55455560
}

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)