Skip to content

Commit e75174e

Browse files
committed
AST: Introduce TypeBase::getTypeSequenceParameters()
1 parent e08e525 commit e75174e

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

include/swift/AST/Types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ class alignas(1 << TypeAlignInBits) TypeBase
654654
void getRootOpenedExistentials(
655655
SmallVectorImpl<OpenedArchetypeType *> &rootOpenedArchetypes) const;
656656

657+
/// Retrieve the set of type sequence generic parameters that occur
658+
/// within this type.
659+
void getTypeSequenceParameters(
660+
SmallVectorImpl<Type> &rootTypeSequenceParams) const;
661+
657662
/// Replace opened archetypes with the given root with their most
658663
/// specific non-dependent upper bounds throughout this type.
659664
Type typeEraseOpenedArchetypesWithRoot(const OpenedArchetypeType *root,

lib/AST/Type.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,28 @@ Type TypeBase::typeEraseOpenedArchetypesWithRoot(
580580
return transformFn(type);
581581
}
582582

583+
void TypeBase::getTypeSequenceParameters(
584+
SmallVectorImpl<Type> &rootTypeSequenceParams) const {
585+
llvm::SmallDenseSet<CanType, 2> visited;
586+
587+
auto recordType = [&](Type t) {
588+
if (visited.insert(t->getCanonicalType()).second)
589+
rootTypeSequenceParams.push_back(t);
590+
};
591+
592+
Type(const_cast<TypeBase *>(this)).visit([&](Type t) {
593+
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
594+
if (paramTy->isTypeSequence()) {
595+
recordType(paramTy);
596+
}
597+
} else if (auto *archetypeTy = t->getAs<SequenceArchetypeType>()) {
598+
if (archetypeTy->isRoot()) {
599+
recordType(t);
600+
}
601+
}
602+
});
603+
}
604+
583605
Type TypeBase::addCurriedSelfType(const DeclContext *dc) {
584606
if (!dc->isTypeContext())
585607
return this;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,21 +2228,13 @@ static Type validateParameterType(ParamDecl *decl) {
22282228

22292229
if (decl->isVariadic()) {
22302230
// Find the first type sequence parameter and use that as the count type.
2231-
Type countTy;
2232-
(void) Ty.findIf([&](Type t) -> bool {
2233-
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
2234-
if (paramTy->isTypeSequence()) {
2235-
countTy = paramTy;
2236-
return true;
2237-
}
2238-
}
2231+
SmallVector<Type, 2> rootTypeSequenceParams;
2232+
Ty->getTypeSequenceParameters(rootTypeSequenceParams);
22392233

2240-
return false;
2241-
});
22422234
// Handle the monovariadic/polyvariadic interface type split.
2243-
if (countTy) {
2235+
if (!rootTypeSequenceParams.empty()) {
22442236
// Polyvariadic types (T...) for <T...> resolve to pack expansions.
2245-
Ty = PackExpansionType::get(Ty, countTy);
2237+
Ty = PackExpansionType::get(Ty, rootTypeSequenceParams[0]);
22462238
} else {
22472239
// Monovariadic types (T...) for <T> resolve to [T].
22482240
Ty = VariadicSequenceType::get(Ty);

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,27 +4031,18 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,
40314031
complained = true;
40324032

40334033
// Find the first type sequence parameter and use that as the count type.
4034-
Type countTy;
4035-
(void) patternTy.get().findIf([&](Type t) -> bool {
4036-
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
4037-
if (paramTy->isTypeSequence()) {
4038-
countTy = paramTy;
4039-
return true;
4040-
}
4041-
}
4042-
4043-
return false;
4044-
});
4034+
SmallVector<Type, 1> rootTypeSequenceParams;
4035+
patternTy->getTypeSequenceParameters(rootTypeSequenceParams);
40454036

40464037
// If there's no reference to a variadic generic parameter, complain
40474038
// - the pack won't actually expand to anything meaningful.
4048-
if (!countTy) {
4039+
if (rootTypeSequenceParams.empty()) {
40494040
diagnose(repr->getLoc(), diag::expansion_not_variadic, patternTy)
40504041
.highlight(repr->getParens());
40514042
return ErrorType::get(getASTContext());
40524043
}
40534044

4054-
return PackExpansionType::get(patternTy, countTy);
4045+
return PackExpansionType::get(patternTy, rootTypeSequenceParams[0]);
40554046
} else {
40564047
// Variadic tuples are not permitted.
40574048
//

0 commit comments

Comments
 (0)