diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -741,23 +741,15 @@ Optional NormalizedConstraint::fromConstraintExprs(Sema &S, NamedDecl *D, ArrayRef E) { - assert(E.size() != 0); - auto First = fromConstraintExpr(S, D, E[0]); - if (E.size() == 1) - return First; - auto Second = fromConstraintExpr(S, D, E[1]); - if (!Second) + auto Conjunction = fromConstraintExpr(S, D, E[0]); + if (!Conjunction) return None; - llvm::Optional Conjunction; - Conjunction.emplace(S.Context, std::move(*First), std::move(*Second), - CCK_Conjunction); - for (unsigned I = 2; I < E.size(); ++I) { + for (unsigned I = 1; I < E.size(); ++I) { auto Next = fromConstraintExpr(S, D, E[I]); if (!Next) - return llvm::Optional{}; - NormalizedConstraint NewConjunction(S.Context, std::move(*Conjunction), - std::move(*Next), CCK_Conjunction); - *Conjunction = std::move(NewConjunction); + return None; + Conjunction.emplace(S.Context, std::move(*Conjunction), std::move(*Next), + CCK_Conjunction); } return Conjunction; } diff --git a/clang/test/CXX/temp/temp.constr/temp.constr.normal/p1.cpp b/clang/test/CXX/temp/temp.constr/temp.constr.normal/p1.cpp --- a/clang/test/CXX/temp/temp.constr/temp.constr.normal/p1.cpp +++ b/clang/test/CXX/temp/temp.constr/temp.constr.normal/p1.cpp @@ -67,3 +67,14 @@ static_assert((foo<1>(), true)); } + +namespace PR47174 { + // This checks that we don't crash with a failed substitution on the first constrained argument when + // performing normalization. + template requires true struct S3; // expected-note {{template is declared here}} + template requires true struct S3; // expected-error {{class template partial specialization is not more specialized than the primary template}} + + // Same as above, for the second position (but this was already working). + template requires true struct S4; // expected-note {{template is declared here}} + template requires true struct S4; // expected-error {{class template partial specialization is not more specialized than the primary template}} +} // namespace PR47174