Skip to content

Commit 045269b

Browse files
authored
Merge branch 'main' into fix/100394
2 parents 928c763 + 2bb3d3a commit 045269b

33 files changed

+1242
-773
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,14 @@ Bug Fixes to C++ Support
502502
in certain friend declarations. (#GH93099)
503503
- Clang now instantiates the correct lambda call operator when a lambda's class type is
504504
merged across modules. (#GH110401)
505+
- Clang now uses the correct set of template argument lists when comparing the constraints of
506+
out-of-line definitions and member templates explicitly specialized for a given implicit instantiation of
507+
a class template. (#GH102320)
505508
- Fix a crash when parsing a pseudo destructor involving an invalid type. (#GH111460)
506509
- Fixed an assertion failure when invoking recovery call expressions with explicit attributes
507510
and undeclared templates. (#GH107047, #GH49093)
511+
- Clang no longer crashes when a lambda contains an invalid block declaration that contains an unexpanded
512+
parameter pack. (#GH109148)
508513
- Fixed overload handling for object parameters with top-level cv-qualifiers in explicit member functions (#GH100394)
509514

510515
Bug Fixes to AST Handling

clang/include/clang/AST/ComputeDependence.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ ExprDependence computeDependence(ArrayInitLoopExpr *E);
133133
ExprDependence computeDependence(ImplicitValueInitExpr *E);
134134
ExprDependence computeDependence(InitListExpr *E);
135135
ExprDependence computeDependence(ExtVectorElementExpr *E);
136-
ExprDependence computeDependence(BlockExpr *E);
136+
ExprDependence computeDependence(BlockExpr *E,
137+
bool ContainsUnexpandedParameterPack);
137138
ExprDependence computeDependence(AsTypeExpr *E);
138139
ExprDependence computeDependence(DeclRefExpr *E, const ASTContext &Ctx);
139140
ExprDependence computeDependence(RecoveryExpr *E);

clang/include/clang/AST/DeclTemplate.h

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -781,15 +781,11 @@ class RedeclarableTemplateDecl : public TemplateDecl,
781781
EntryType *Entry, void *InsertPos);
782782

783783
struct CommonBase {
784-
CommonBase() : InstantiatedFromMember(nullptr, false) {}
784+
CommonBase() {}
785785

786786
/// The template from which this was most
787787
/// directly instantiated (or null).
788-
///
789-
/// The boolean value indicates whether this template
790-
/// was explicitly specialized.
791-
llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
792-
InstantiatedFromMember;
788+
RedeclarableTemplateDecl *InstantiatedFromMember = nullptr;
793789

794790
/// If non-null, points to an array of specializations (including
795791
/// partial specializations) known only by their external declaration IDs.
@@ -809,14 +805,19 @@ class RedeclarableTemplateDecl : public TemplateDecl,
809805
};
810806

811807
/// Pointer to the common data shared by all declarations of this
812-
/// template.
813-
mutable CommonBase *Common = nullptr;
808+
/// template, and a flag indicating if the template is a member
809+
/// specialization.
810+
mutable llvm::PointerIntPair<CommonBase *, 1, bool> Common;
811+
812+
CommonBase *getCommonPtrInternal() const { return Common.getPointer(); }
814813

815814
/// Retrieves the "common" pointer shared by all (re-)declarations of
816815
/// the same template. Calling this routine may implicitly allocate memory
817816
/// for the common pointer.
818817
CommonBase *getCommonPtr() const;
819818

819+
void setCommonPtr(CommonBase *C) const { Common.setPointer(C); }
820+
820821
virtual CommonBase *newCommon(ASTContext &C) const = 0;
821822

822823
// Construct a template decl with name, parameters, and templated element.
@@ -857,15 +858,22 @@ class RedeclarableTemplateDecl : public TemplateDecl,
857858
/// template<> template<typename T>
858859
/// struct X<int>::Inner { /* ... */ };
859860
/// \endcode
860-
bool isMemberSpecialization() const {
861-
return getCommonPtr()->InstantiatedFromMember.getInt();
861+
bool isMemberSpecialization() const { return Common.getInt(); }
862+
863+
/// Determines whether any redeclaration of this template was
864+
/// a specialization of a member template.
865+
bool hasMemberSpecialization() const {
866+
for (const auto *D : redecls()) {
867+
if (D->isMemberSpecialization())
868+
return true;
869+
}
870+
return false;
862871
}
863872

864873
/// Note that this member template is a specialization.
865874
void setMemberSpecialization() {
866-
assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
867-
"Only member templates can be member template specializations");
868-
getCommonPtr()->InstantiatedFromMember.setInt(true);
875+
assert(!isMemberSpecialization() && "already a member specialization");
876+
Common.setInt(true);
869877
}
870878

871879
/// Retrieve the member template from which this template was
@@ -905,12 +913,12 @@ class RedeclarableTemplateDecl : public TemplateDecl,
905913
/// void X<T>::f(T, U);
906914
/// \endcode
907915
RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
908-
return getCommonPtr()->InstantiatedFromMember.getPointer();
916+
return getCommonPtr()->InstantiatedFromMember;
909917
}
910918

911919
void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
912-
assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
913-
getCommonPtr()->InstantiatedFromMember.setPointer(TD);
920+
assert(!getCommonPtr()->InstantiatedFromMember);
921+
getCommonPtr()->InstantiatedFromMember = TD;
914922
}
915923

916924
/// Retrieve the "injected" template arguments that correspond to the
@@ -1989,6 +1997,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
19891997
/// template arguments have been deduced.
19901998
void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
19911999
const TemplateArgumentList *TemplateArgs) {
2000+
assert(!isa<ClassTemplatePartialSpecializationDecl>(this) &&
2001+
"A partial specialization cannot be instantiated from a template");
19922002
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
19932003
"Already set to a class template partial specialization!");
19942004
auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
@@ -2000,6 +2010,8 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
20002010
/// Note that this class template specialization is an instantiation
20012011
/// of the given class template.
20022012
void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
2013+
assert(!isa<ClassTemplatePartialSpecializationDecl>(this) &&
2014+
"A partial specialization cannot be instantiated from a template");
20032015
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
20042016
"Previously set to a class template partial specialization!");
20052017
SpecializedTemplate = TemplDecl;
@@ -2187,19 +2199,23 @@ class ClassTemplatePartialSpecializationDecl
21872199
/// struct X<int>::Inner<T*> { /* ... */ };
21882200
/// \endcode
21892201
bool isMemberSpecialization() const {
2190-
const auto *First =
2191-
cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
2192-
return First->InstantiatedFromMember.getInt();
2202+
return InstantiatedFromMember.getInt();
21932203
}
21942204

2195-
/// Note that this member template is a specialization.
2196-
void setMemberSpecialization() {
2197-
auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
2198-
assert(First->InstantiatedFromMember.getPointer() &&
2199-
"Only member templates can be member template specializations");
2200-
return First->InstantiatedFromMember.setInt(true);
2205+
/// Determines whether any redeclaration of this this class template partial
2206+
/// specialization was a specialization of a member partial specialization.
2207+
bool hasMemberSpecialization() const {
2208+
for (const auto *D : redecls()) {
2209+
if (cast<ClassTemplatePartialSpecializationDecl>(D)
2210+
->isMemberSpecialization())
2211+
return true;
2212+
}
2213+
return false;
22012214
}
22022215

2216+
/// Note that this member template is a specialization.
2217+
void setMemberSpecialization() { return InstantiatedFromMember.setInt(true); }
2218+
22032219
/// Retrieves the injected specialization type for this partial
22042220
/// specialization. This is not the same as the type-decl-type for
22052221
/// this partial specialization, which is an InjectedClassNameType.
@@ -2268,10 +2284,6 @@ class ClassTemplateDecl : public RedeclarableTemplateDecl {
22682284
return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
22692285
}
22702286

2271-
void setCommonPtr(Common *C) {
2272-
RedeclarableTemplateDecl::Common = C;
2273-
}
2274-
22752287
public:
22762288

22772289
friend class ASTDeclReader;
@@ -2754,6 +2766,8 @@ class VarTemplateSpecializationDecl : public VarDecl,
27542766
/// template arguments have been deduced.
27552767
void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
27562768
const TemplateArgumentList *TemplateArgs) {
2769+
assert(!isa<VarTemplatePartialSpecializationDecl>(this) &&
2770+
"A partial specialization cannot be instantiated from a template");
27572771
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
27582772
"Already set to a variable template partial specialization!");
27592773
auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
@@ -2765,6 +2779,8 @@ class VarTemplateSpecializationDecl : public VarDecl,
27652779
/// Note that this variable template specialization is an instantiation
27662780
/// of the given variable template.
27672781
void setInstantiationOf(VarTemplateDecl *TemplDecl) {
2782+
assert(!isa<VarTemplatePartialSpecializationDecl>(this) &&
2783+
"A partial specialization cannot be instantiated from a template");
27682784
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
27692785
"Previously set to a variable template partial specialization!");
27702786
SpecializedTemplate = TemplDecl;
@@ -2949,19 +2965,24 @@ class VarTemplatePartialSpecializationDecl
29492965
/// U* X<int>::Inner<T*> = (T*)(0) + 1;
29502966
/// \endcode
29512967
bool isMemberSpecialization() const {
2952-
const auto *First =
2953-
cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2954-
return First->InstantiatedFromMember.getInt();
2968+
return InstantiatedFromMember.getInt();
29552969
}
29562970

2957-
/// Note that this member template is a specialization.
2958-
void setMemberSpecialization() {
2959-
auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2960-
assert(First->InstantiatedFromMember.getPointer() &&
2961-
"Only member templates can be member template specializations");
2962-
return First->InstantiatedFromMember.setInt(true);
2971+
/// Determines whether any redeclaration of this this variable template
2972+
/// partial specialization was a specialization of a member partial
2973+
/// specialization.
2974+
bool hasMemberSpecialization() const {
2975+
for (const auto *D : redecls()) {
2976+
if (cast<VarTemplatePartialSpecializationDecl>(D)
2977+
->isMemberSpecialization())
2978+
return true;
2979+
}
2980+
return false;
29632981
}
29642982

2983+
/// Note that this member template is a specialization.
2984+
void setMemberSpecialization() { return InstantiatedFromMember.setInt(true); }
2985+
29652986
SourceRange getSourceRange() const override LLVM_READONLY;
29662987

29672988
void Profile(llvm::FoldingSetNodeID &ID) const {

clang/include/clang/AST/Expr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6413,9 +6413,9 @@ class BlockExpr : public Expr {
64136413
protected:
64146414
BlockDecl *TheBlock;
64156415
public:
6416-
BlockExpr(BlockDecl *BD, QualType ty)
6416+
BlockExpr(BlockDecl *BD, QualType ty, bool ContainsUnexpandedParameterPack)
64176417
: Expr(BlockExprClass, ty, VK_PRValue, OK_Ordinary), TheBlock(BD) {
6418-
setDependence(computeDependence(this));
6418+
setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
64196419
}
64206420

64216421
/// Build an empty block expression.

clang/include/clang/Sema/ScopeInfo.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,16 @@ class CapturingScopeInfo : public FunctionScopeInfo {
724724
/// is deduced (e.g. a lambda or block with omitted return type).
725725
bool HasImplicitReturnType = false;
726726

727+
/// Whether this contains an unexpanded parameter pack.
728+
bool ContainsUnexpandedParameterPack = false;
729+
727730
/// ReturnType - The target type of return statements in this context,
728731
/// or null if unknown.
729732
QualType ReturnType;
730733

734+
/// Packs introduced by this, if any.
735+
SmallVector<NamedDecl *, 4> LocalPacks;
736+
731737
void addCapture(ValueDecl *Var, bool isBlock, bool isByref, bool isNested,
732738
SourceLocation Loc, SourceLocation EllipsisLoc,
733739
QualType CaptureType, bool Invalid) {
@@ -895,12 +901,6 @@ class LambdaScopeInfo final :
895901
/// Whether any of the capture expressions requires cleanups.
896902
CleanupInfo Cleanup;
897903

898-
/// Whether the lambda contains an unexpanded parameter pack.
899-
bool ContainsUnexpandedParameterPack = false;
900-
901-
/// Packs introduced by this lambda, if any.
902-
SmallVector<NamedDecl*, 4> LocalPacks;
903-
904904
/// Source range covering the explicit template parameter list (if it exists).
905905
SourceRange ExplicitTemplateParamsRange;
906906

clang/include/clang/Sema/Sema.h

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,10 @@ class Sema final : public SemaBase {
702702
/// Retrieve the current block, if any.
703703
sema::BlockScopeInfo *getCurBlock();
704704

705-
/// Get the innermost lambda enclosing the current location, if any. This
706-
/// looks through intervening non-lambda scopes such as local functions and
707-
/// blocks.
708-
sema::LambdaScopeInfo *getEnclosingLambda() const;
705+
/// Get the innermost lambda or block enclosing the current location, if any.
706+
/// This looks through intervening non-lambda, non-block scopes such as local
707+
/// functions.
708+
sema::CapturingScopeInfo *getEnclosingLambdaOrBlock() const;
709709

710710
/// Retrieve the current lambda scope info, if any.
711711
/// \param IgnoreNonLambdaCapturingScope true if should find the top-most
@@ -11327,9 +11327,9 @@ class Sema final : public SemaBase {
1132711327
CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc,
1132811328
const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams,
1132911329
AccessSpecifier AS, SourceLocation ModulePrivateLoc,
11330-
SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists,
11331-
TemplateParameterList **OuterTemplateParamLists,
11332-
SkipBodyInfo *SkipBody = nullptr);
11330+
SourceLocation FriendLoc,
11331+
ArrayRef<TemplateParameterList *> OuterTemplateParamLists,
11332+
bool IsMemberSpecialization, SkipBodyInfo *SkipBody = nullptr);
1133311333

1133411334
/// Translates template arguments as provided by the parser
1133511335
/// into template arguments used by semantic analysis.
@@ -11368,7 +11368,8 @@ class Sema final : public SemaBase {
1136811368
DeclResult ActOnVarTemplateSpecialization(
1136911369
Scope *S, Declarator &D, TypeSourceInfo *DI, LookupResult &Previous,
1137011370
SourceLocation TemplateKWLoc, TemplateParameterList *TemplateParams,
11371-
StorageClass SC, bool IsPartialSpecialization);
11371+
StorageClass SC, bool IsPartialSpecialization,
11372+
bool IsMemberSpecialization);
1137211373

1137311374
/// Get the specialization of the given variable template corresponding to
1137411375
/// the specified argument list, or a null-but-valid result if the arguments
@@ -13009,28 +13010,14 @@ class Sema final : public SemaBase {
1300913010
/// dealing with a specialization. This is only relevant for function
1301013011
/// template specializations.
1301113012
///
13012-
/// \param Pattern If non-NULL, indicates the pattern from which we will be
13013-
/// instantiating the definition of the given declaration, \p ND. This is
13014-
/// used to determine the proper set of template instantiation arguments for
13015-
/// friend function template specializations.
13016-
///
1301713013
/// \param ForConstraintInstantiation when collecting arguments,
1301813014
/// ForConstraintInstantiation indicates we should continue looking when
1301913015
/// encountering a lambda generic call operator, and continue looking for
1302013016
/// arguments on an enclosing class template.
13021-
///
13022-
/// \param SkipForSpecialization when specified, any template specializations
13023-
/// in a traversal would be ignored.
13024-
/// \param ForDefaultArgumentSubstitution indicates we should continue looking
13025-
/// when encountering a specialized member function template, rather than
13026-
/// returning immediately.
1302713017
MultiLevelTemplateArgumentList getTemplateInstantiationArgs(
1302813018
const NamedDecl *D, const DeclContext *DC = nullptr, bool Final = false,
1302913019
std::optional<ArrayRef<TemplateArgument>> Innermost = std::nullopt,
13030-
bool RelativeToPrimary = false, const FunctionDecl *Pattern = nullptr,
13031-
bool ForConstraintInstantiation = false,
13032-
bool SkipForSpecialization = false,
13033-
bool ForDefaultArgumentSubstitution = false);
13020+
bool RelativeToPrimary = false, bool ForConstraintInstantiation = false);
1303413021

1303513022
/// RAII object to handle the state changes required to synthesize
1303613023
/// a function body.

clang/include/clang/Sema/Template.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ enum class TemplateSubstitutionKind : char {
411411
/// lookup will search our outer scope.
412412
bool CombineWithOuterScope;
413413

414-
/// Whether this scope is being used to instantiate a lambda expression,
415-
/// in which case it should be reused for instantiating the lambda's
416-
/// FunctionProtoType.
417-
bool InstantiatingLambda = false;
414+
/// Whether this scope is being used to instantiate a lambda or block
415+
/// expression, in which case it should be reused for instantiating the
416+
/// lambda's FunctionProtoType.
417+
bool InstantiatingLambdaOrBlock = false;
418418

419419
/// If non-NULL, the template parameter pack that has been
420420
/// partially substituted per C++0x [temp.arg.explicit]p9.
@@ -431,10 +431,10 @@ enum class TemplateSubstitutionKind : char {
431431

432432
public:
433433
LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false,
434-
bool InstantiatingLambda = false)
434+
bool InstantiatingLambdaOrBlock = false)
435435
: SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope),
436436
CombineWithOuterScope(CombineWithOuterScope),
437-
InstantiatingLambda(InstantiatingLambda) {
437+
InstantiatingLambdaOrBlock(InstantiatingLambdaOrBlock) {
438438
SemaRef.CurrentInstantiationScope = this;
439439
}
440440

@@ -561,8 +561,8 @@ enum class TemplateSubstitutionKind : char {
561561
/// Determine whether D is a pack expansion created in this scope.
562562
bool isLocalPackExpansion(const Decl *D);
563563

564-
/// Determine whether this scope is for instantiating a lambda.
565-
bool isLambda() const { return InstantiatingLambda; }
564+
/// Determine whether this scope is for instantiating a lambda or block.
565+
bool isLambdaOrBlock() const { return InstantiatingLambdaOrBlock; }
566566
};
567567

568568
class TemplateDeclInstantiator

clang/lib/AST/ComputeDependence.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,13 @@ ExprDependence clang::computeDependence(ExtVectorElementExpr *E) {
252252
return E->getBase()->getDependence();
253253
}
254254

255-
ExprDependence clang::computeDependence(BlockExpr *E) {
255+
ExprDependence clang::computeDependence(BlockExpr *E,
256+
bool ContainsUnexpandedParameterPack) {
256257
auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
257258
if (E->getBlockDecl()->isDependentContext())
258259
D |= ExprDependence::Instantiation;
260+
if (ContainsUnexpandedParameterPack)
261+
D |= ExprDependence::UnexpandedPack;
259262
return D;
260263
}
261264

0 commit comments

Comments
 (0)