Skip to content

[Clang] Fix a partial ordering bug involving CTAD injected template arguments #149782

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 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions clang/lib/Sema/SemaTemplateDeduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5525,6 +5525,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
// FIXME: A substitution can be incomplete on a non-structural part of the
// type. Use the canonical type for now, until the TemplateInstantiator can
// deal with that.

// Workaround: Implicit deduction guides use InjectedClassNameTypes, whereas
// the explicit guides don't. The substitution doesn't transform these types,
// so let it transform their specializations instead.
bool IsDeductionGuide = isa<CXXDeductionGuideDecl>(FTD->getTemplatedDecl());
if (IsDeductionGuide) {
if (auto *Injected = P->getAs<InjectedClassNameType>())
P = Injected->getInjectedSpecializationType();
}
QualType InstP = S.SubstType(P.getCanonicalType(), MLTAL, FTD->getLocation(),
FTD->getDeclName(), &IsIncompleteSubstitution);
if (InstP.isNull() && !IsIncompleteSubstitution)
Expand All @@ -5539,9 +5548,15 @@ static TemplateDeductionResult CheckDeductionConsistency(
if (auto *PA = dyn_cast<PackExpansionType>(A);
PA && !isa<PackExpansionType>(InstP))
A = PA->getPattern();
if (!S.Context.hasSameType(
S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType()),
S.Context.getUnqualifiedArrayType(A.getNonReferenceType())))
auto T1 = S.Context.getUnqualifiedArrayType(InstP.getNonReferenceType());
auto T2 = S.Context.getUnqualifiedArrayType(A.getNonReferenceType());
if (IsDeductionGuide) {
if (auto *Injected = T1->getAs<InjectedClassNameType>())
T1 = Injected->getInjectedSpecializationType();
if (auto *Injected = T2->getAs<InjectedClassNameType>())
T2 = Injected->getInjectedSpecializationType();
}
if (!S.Context.hasSameType(T1, T2))
return TemplateDeductionResult::NonDeducedMismatch;
return TemplateDeductionResult::Success;
}
Expand Down
16 changes: 16 additions & 0 deletions clang/test/SemaTemplate/deduction-guide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,19 @@ Expand<Type, Invocable<>> _{};
// CHECK-NEXT: | `-ParmVarDecl {{.+}} 'T...' pack

}

namespace GH134613 {
template <typename R> struct Foo {
using value_type = R;

Foo() = default;
Foo(Foo<Foo<R>> &&rhs) {}
};

void main() {
auto r1 = Foo(Foo<Foo<int>>{});

static_assert(__is_same(decltype(r1)::value_type, int));
}

}