Skip to content

Commit 670c4a4

Browse files
committed
[Clang][Sema] Rehash the lambda within a type alias template decl
Fixes llvm#89853
1 parent 3ea9ed4 commit 670c4a4

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

clang/lib/Sema/SemaTemplateInstantiate.cpp

+32-34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/AST/Expr.h"
2121
#include "clang/AST/ExprConcepts.h"
2222
#include "clang/AST/PrettyDeclStackTrace.h"
23+
#include "clang/AST/RecursiveASTVisitor.h"
2324
#include "clang/AST/Type.h"
2425
#include "clang/AST/TypeLoc.h"
2526
#include "clang/AST/TypeVisitor.h"
@@ -87,12 +88,17 @@ struct Response {
8788
// than lambda classes.
8889
const FunctionDecl *
8990
getPrimaryTemplateOfGenericLambda(const FunctionDecl *LambdaCallOperator) {
91+
if (!isLambdaCallOperator(LambdaCallOperator))
92+
return LambdaCallOperator;
9093
while (true) {
9194
if (auto *FTD = dyn_cast_if_present<FunctionTemplateDecl>(
9295
LambdaCallOperator->getDescribedTemplate());
9396
FTD && FTD->getInstantiatedFromMemberTemplate()) {
9497
LambdaCallOperator =
9598
FTD->getInstantiatedFromMemberTemplate()->getTemplatedDecl();
99+
} else if (LambdaCallOperator->getPrimaryTemplate()) {
100+
LambdaCallOperator =
101+
LambdaCallOperator->getPrimaryTemplate()->getTemplatedDecl();
96102
} else if (auto *Prev = cast<CXXMethodDecl>(LambdaCallOperator)
97103
->getInstantiatedFromMemberFunction())
98104
LambdaCallOperator = Prev;
@@ -138,22 +144,26 @@ getEnclosingTypeAliasTemplateDecl(Sema &SemaRef) {
138144
// Check if we are currently inside of a lambda expression that is
139145
// surrounded by a using alias declaration. e.g.
140146
// template <class> using type = decltype([](auto) { ^ }());
141-
// By checking if:
142-
// 1. The lambda expression and the using alias declaration share the
143-
// same declaration context.
144-
// 2. They have the same template depth.
145147
// We have to do so since a TypeAliasTemplateDecl (or a TypeAliasDecl) is never
146148
// a DeclContext, nor does it have an associated specialization Decl from which
147149
// we could collect these template arguments.
148150
bool isLambdaEnclosedByTypeAliasDecl(
149-
const FunctionDecl *PrimaryLambdaCallOperator,
151+
const FunctionDecl *LambdaCallOperator,
150152
const TypeAliasTemplateDecl *PrimaryTypeAliasDecl) {
151-
return cast<CXXRecordDecl>(PrimaryLambdaCallOperator->getDeclContext())
152-
->getTemplateDepth() ==
153-
PrimaryTypeAliasDecl->getTemplateDepth() &&
154-
getLambdaAwareParentOfDeclContext(
155-
const_cast<FunctionDecl *>(PrimaryLambdaCallOperator)) ==
156-
PrimaryTypeAliasDecl->getDeclContext();
153+
struct Visitor : RecursiveASTVisitor<Visitor> {
154+
Visitor(const FunctionDecl *CallOperator) : CallOperator(CallOperator) {}
155+
bool VisitLambdaExpr(const LambdaExpr *LE) {
156+
auto *G = getPrimaryTemplateOfGenericLambda(LE->getCallOperator());
157+
return G != CallOperator;
158+
}
159+
const FunctionDecl *CallOperator;
160+
};
161+
QualType Underlying =
162+
PrimaryTypeAliasDecl->getTemplatedDecl()->getUnderlyingType();
163+
if (auto *DT = dyn_cast<DecltypeType>(Underlying.getTypePtr()))
164+
return !Visitor(getPrimaryTemplateOfGenericLambda(LambdaCallOperator))
165+
.TraverseStmt(DT->getUnderlyingExpr());
166+
return false;
157167
}
158168

159169
// Add template arguments from a variable template instantiation.
@@ -283,23 +293,8 @@ Response HandleFunction(Sema &SemaRef, const FunctionDecl *Function,
283293

284294
// If this function is a generic lambda specialization, we are done.
285295
if (!ForConstraintInstantiation &&
286-
isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) {
287-
// TypeAliasTemplateDecls should be taken into account, e.g.
288-
// when we're deducing the return type of a lambda.
289-
//
290-
// template <class> int Value = 0;
291-
// template <class T>
292-
// using T = decltype([]<int U = 0>() { return Value<T>; }());
293-
//
294-
if (auto TypeAlias = getEnclosingTypeAliasTemplateDecl(SemaRef)) {
295-
if (isLambdaEnclosedByTypeAliasDecl(
296-
/*PrimaryLambdaCallOperator=*/getPrimaryTemplateOfGenericLambda(
297-
Function),
298-
/*PrimaryTypeAliasDecl=*/TypeAlias.PrimaryTypeAliasDecl))
299-
return Response::UseNextDecl(Function);
300-
}
296+
isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function))
301297
return Response::Done();
302-
}
303298

304299
} else if (Function->getDescribedFunctionTemplate()) {
305300
assert(
@@ -412,9 +407,7 @@ Response HandleRecordDecl(Sema &SemaRef, const CXXRecordDecl *Rec,
412407
// This is necessary for constraint checking, since we always keep
413408
// constraints relative to the primary template.
414409
if (auto TypeAlias = getEnclosingTypeAliasTemplateDecl(SemaRef)) {
415-
const FunctionDecl *PrimaryLambdaCallOperator =
416-
getPrimaryTemplateOfGenericLambda(Rec->getLambdaCallOperator());
417-
if (isLambdaEnclosedByTypeAliasDecl(PrimaryLambdaCallOperator,
410+
if (isLambdaEnclosedByTypeAliasDecl(Rec->getLambdaCallOperator(),
418411
TypeAlias.PrimaryTypeAliasDecl)) {
419412
Result.addOuterTemplateArguments(TypeAlias.Template,
420413
TypeAlias.AssociatedTemplateArguments,
@@ -1670,12 +1663,17 @@ namespace {
16701663

16711664
CXXRecordDecl::LambdaDependencyKind
16721665
ComputeLambdaDependency(LambdaScopeInfo *LSI) {
1673-
auto &CCS = SemaRef.CodeSynthesisContexts.back();
1674-
if (CCS.Kind ==
1675-
Sema::CodeSynthesisContext::TypeAliasTemplateInstantiation) {
1676-
unsigned TypeAliasDeclDepth = CCS.Entity->getTemplateDepth();
1666+
if (auto TypeAlias =
1667+
TemplateInstArgsHelpers::getEnclosingTypeAliasTemplateDecl(
1668+
getSema());
1669+
TypeAlias && TemplateInstArgsHelpers::isLambdaEnclosedByTypeAliasDecl(
1670+
LSI->CallOperator, TypeAlias.PrimaryTypeAliasDecl)) {
1671+
unsigned TypeAliasDeclDepth = TypeAlias.Template->getTemplateDepth();
16771672
if (TypeAliasDeclDepth >= TemplateArgs.getNumSubstitutedLevels())
16781673
return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent;
1674+
for (TemplateArgument TA : TypeAlias.AssociatedTemplateArguments)
1675+
if (TA.isDependent())
1676+
return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent;
16791677
}
16801678
return inherited::ComputeLambdaDependency(LSI);
16811679
}

clang/test/SemaTemplate/alias-template-with-lambdas.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,35 @@ namespace GH82104 {
9494
template <typename, typename...> int Zero = 0;
9595

9696
template <typename T, typename...U>
97-
using T14 = decltype([]<int V = 0>() { return Zero<T, U...>; }());
97+
using T14 = decltype([]<int V = 0>(auto Param) { return Zero<T, U...> + V + (int)sizeof(Param); }("hello"));
9898

9999
template <typename T> using T15 = T14<T, T>;
100100

101101
static_assert(__is_same(T15<char>, int));
102102

103103
} // namespace GH82104
104104

105+
namespace GH89853 {
106+
template <typename = void>
107+
static constexpr auto innocuous = []<int m> { return m; };
108+
109+
template <auto Pred = innocuous<>>
110+
using broken = decltype(Pred.template operator()<42>());
111+
112+
broken<> *boom;
113+
114+
template <auto Pred =
115+
[]<const char c> {
116+
(void)static_cast<char>(c);
117+
}>
118+
using broken2 = decltype(Pred.template operator()<42>());
119+
120+
broken2<> *boom2;
121+
122+
template <auto Pred = []<const char m> { return m; }>
123+
using broken3 = decltype(Pred.template operator()<42>());
124+
125+
broken3<> *boom3;
126+
}
127+
105128
} // namespace lambda_calls

0 commit comments

Comments
 (0)