Skip to content

Commit 0a41588

Browse files
authored
Merge pull request #13644 from davezarzycki/nfc_hasParenSema
2 parents 01f42c8 + 41a0553 commit 0a41588

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

include/swift/AST/Types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,16 @@ class TupleType final : public TypeBase, public llvm::FoldingSetNode,
16331633
return static_cast<bool>(Bits.TupleType.HasInOutElement);
16341634
}
16351635

1636+
/// Returns true if this tuple has parenthesis semantics.
1637+
bool hasParenSema(bool allowName = false) const {
1638+
auto Fields = getElements();
1639+
if (Fields.size() != 1 || Fields[0].isVararg())
1640+
return false;
1641+
if (allowName)
1642+
return true;
1643+
return !Fields[0].hasName();
1644+
}
1645+
16361646
// Implement isa/cast/dyncast/etc.
16371647
static bool classof(const TypeBase *T) {
16381648
return T->getKind() == TypeKind::Tuple;

lib/AST/Type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ Type TypeBase::getWithoutParens() {
685685
Type TypeBase::getWithoutImmediateLabel() {
686686
Type Ty = this;
687687
if (auto tupleTy = dyn_cast<TupleType>(Ty.getPointer())) {
688-
if (tupleTy->getNumElements() == 1 && !tupleTy->getElement(0).isVararg())
688+
if (tupleTy->hasParenSema(/*allowName*/true))
689689
Ty = tupleTy->getElementType(0);
690690
}
691691
return Ty;
@@ -870,7 +870,7 @@ Type TypeBase::getRValueInstanceType() {
870870

871871
// Look through argument list tuples.
872872
if (auto tupleTy = type->getAs<TupleType>()) {
873-
if (tupleTy->getNumElements() == 1 && !tupleTy->getElement(0).isVararg())
873+
if (tupleTy->hasParenSema(/*allowName*/true))
874874
type = tupleTy->getElementType(0);
875875
}
876876

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,8 +1965,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
19651965
// For non-argument tuples, we can do the same conversion but not
19661966
// to a tuple with varargs.
19671967
if (!type1->is<LValueType>() &&
1968-
((tuple2->getNumElements() == 1 &&
1969-
!tuple2->getElement(0).isVararg()) ||
1968+
(tuple2->hasParenSema(/*allowName*/true) ||
19701969
(kind >= ConstraintKind::Conversion &&
19711970
tuple2->getElementForScalarInit() >= 0 &&
19721971
(isArgumentTupleConversion ||

lib/Sema/TypeCheckPattern.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
973973
if ((options & TypeResolutionFlags::EnumPatternPayload)
974974
&& !isa<TuplePattern>(semantic)) {
975975
if (auto tupleType = type->getAs<TupleType>()) {
976-
if (tupleType->getNumElements() == 1
977-
&& !tupleType->getElement(0).isVararg()) {
976+
if (tupleType->hasParenSema(/*allowName*/true)) {
978977
auto elementTy = tupleType->getElementType(0);
979978
if (coercePatternToType(sub, dc, elementTy, subOptions, resolver))
980979
return true;

0 commit comments

Comments
 (0)