Skip to content

Commit 5a83c86

Browse files
committed
Eliminate default arguments from TupleType.
In Swift, default arguments are associated with a function or initializer's declaration---not with its type. This was not always the case, and TupleType's ability to store a default argument kind is a messy holdover from those dark times. Eliminate the default argument kind from TupleType, which involves migrating a few more clients over to declaration-centric handling of default arguments. Doing so is usually a bug-fix anyway: without the declaration, one didn't really have The SILGen test changes are due to a name-mangling fix that fell out of this change: a tuple type is mangled differently than a non-tuple type, and having a default argument would make the parameter list of a single-parameter function into a tuple type. Hence, func foo(x: Int = 5) would get a different mangling from func foo(x: Int) even though we didn't actually allow overloading. Fixes rdar://problem/24016341, and helps us along the way to SE-0111 (removing the significance of argument labels) because argument labels are also declaration-centric, and need the same information.
1 parent d8d7d1d commit 5a83c86

23 files changed

+294
-338
lines changed

include/swift/AST/Types.h

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#define SWIFT_TYPES_H
1919

2020
#include "swift/AST/DeclContext.h"
21-
#include "swift/AST/DefaultArgumentKind.h"
2221
#include "swift/AST/Ownership.h"
2322
#include "swift/AST/Requirement.h"
2423
#include "swift/AST/Type.h"
@@ -103,10 +102,9 @@ class RecursiveTypeProperties {
103102
/// function input.
104103
HasInOut = 0x10,
105104

106-
/// This type expression contains a tuple with default arguments
107-
/// other than as a function input.
108-
HasDefaultParameter = 0x20,
109-
105+
/// Whether this type expression contains an unbound generic type.
106+
HasUnboundGeneric = 0x20,
107+
110108
/// This type expression contains an LValueType other than as a
111109
/// function input, and can be loaded to convert to an rvalue.
112110
IsLValue = 0x40,
@@ -117,10 +115,7 @@ class RecursiveTypeProperties {
117115
/// This type expression contains a DynamicSelf type.
118116
HasDynamicSelf = 0x100,
119117

120-
/// Whether this type expression contains an unbound generic type.
121-
HasUnboundGeneric = 0x200,
122-
123-
IsNotMaterializable = (HasInOut | HasDefaultParameter | IsLValue)
118+
IsNotMaterializable = (HasInOut | IsLValue)
124119
};
125120

126121
private:
@@ -152,11 +147,6 @@ class RecursiveTypeProperties {
152147
/// inout, except as the parameter of a function?
153148
bool hasInOut() const { return Bits & HasInOut; }
154149

155-
/// Does a type with these properties structurally contain a
156-
/// tuple with default argument values, except as the parameter
157-
/// of a function?
158-
bool hasDefaultArg() const { return Bits & HasDefaultParameter; }
159-
160150
/// Is a type with these properties an lvalue?
161151
bool isLValue() const { return Bits & IsLValue; }
162152

@@ -437,12 +427,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
437427
return getRecursiveProperties().hasInOut();
438428
}
439429

440-
/// \brief Determine whether the type involves a tuple type with a
441-
/// default argument, except as a function input.
442-
bool hasDefaultArg() const {
443-
return getRecursiveProperties().hasDefaultArg();
444-
}
445-
446430
/// \brief Determine whether the type involves an archetype.
447431
bool hasArchetype() const {
448432
return getRecursiveProperties().hasArchetype();
@@ -743,9 +727,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
743727
/// the result would be the (parenthesized) type ((int, int)).
744728
Type getUnlabeledType(ASTContext &Context);
745729

746-
/// \brief Retrieve the type without any default arguments.
747-
Type getWithoutDefaultArgs(const ASTContext &Context);
748-
749730
/// Retrieve the type without any parentheses around it.
750731
Type getWithoutParens();
751732

@@ -1324,22 +1305,16 @@ class TupleTypeElt {
13241305
/// \brief This is the type of the field.
13251306
Type ElementType;
13261307

1327-
/// The default argument,
1328-
DefaultArgumentKind DefaultArg;
1329-
13301308
friend class TupleType;
13311309

13321310
public:
13331311
TupleTypeElt() = default;
13341312
inline /*implicit*/ TupleTypeElt(Type ty,
13351313
Identifier name = Identifier(),
1336-
DefaultArgumentKind defaultArg =
1337-
DefaultArgumentKind::None,
13381314
bool isVariadic = false);
13391315

13401316
/*implicit*/ TupleTypeElt(TypeBase *Ty)
1341-
: NameAndVariadic(Identifier(), false),
1342-
ElementType(Ty), DefaultArg(DefaultArgumentKind::None) { }
1317+
: NameAndVariadic(Identifier(), false), ElementType(Ty) { }
13431318

13441319
bool hasName() const { return !NameAndVariadic.getPointer().empty(); }
13451320
Identifier getName() const { return NameAndVariadic.getPointer(); }
@@ -1351,14 +1326,6 @@ class TupleTypeElt {
13511326
return NameAndVariadic.getInt();
13521327
}
13531328

1354-
/// Retrieve the kind of default argument available on this field.
1355-
DefaultArgumentKind getDefaultArgKind() const { return DefaultArg; }
1356-
1357-
/// Whether we have a default argument.
1358-
bool hasDefaultArg() const {
1359-
return getDefaultArgKind() != DefaultArgumentKind::None;
1360-
}
1361-
13621329
static inline Type getVarargBaseTy(Type VarArgT);
13631330

13641331
/// Remove the type of this varargs element designator, without the array
@@ -1370,7 +1337,7 @@ class TupleTypeElt {
13701337

13711338
/// Retrieve a copy of this tuple type element with the type replaced.
13721339
TupleTypeElt getWithType(Type T) const {
1373-
return TupleTypeElt(T, getName(), getDefaultArgKind(), isVararg());
1340+
return TupleTypeElt(T, getName(), isVararg());
13741341
}
13751342
};
13761343

@@ -1421,10 +1388,6 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
14211388
/// return the element index, otherwise return -1.
14221389
int getNamedElementId(Identifier I) const;
14231390

1424-
/// hasAnyDefaultValues - Return true if any of our elements has a default
1425-
/// value.
1426-
bool hasAnyDefaultValues() const;
1427-
14281391
/// getElementForScalarInit - If a tuple of this type can be initialized with
14291392
/// a scalar, return the element number that the scalar is assigned to. If
14301393
/// not, return -1.
@@ -4508,10 +4471,8 @@ inline bool TypeBase::mayHaveSuperclass() {
45084471

45094472
inline TupleTypeElt::TupleTypeElt(Type ty,
45104473
Identifier name,
4511-
DefaultArgumentKind defArg,
45124474
bool isVariadic)
4513-
: NameAndVariadic(name, isVariadic),
4514-
ElementType(ty), DefaultArg(defArg)
4475+
: NameAndVariadic(name, isVariadic), ElementType(ty)
45154476
{
45164477
assert(!isVariadic ||
45174478
isa<ErrorType>(ty.getPointer()) ||

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const unsigned char MODULE_DOC_SIGNATURE[] = { 0xE2, 0x9C, 0xA8, 0x07 };
4343

4444
/// Serialized module format major version number.
4545
///
46-
/// Always 0 for Swift 1.x and 2.x.
46+
/// Always 0 for Swift 1.x - 3.x.
4747
const uint16_t VERSION_MAJOR = 0;
4848

4949
/// Serialized module format minor version number.
@@ -53,7 +53,7 @@ const uint16_t VERSION_MAJOR = 0;
5353
/// in source control, you should also update the comment to briefly
5454
/// describe what change you made. The content of this comment isn't important;
5555
/// it just ensures a conflict if two people change the module format.
56-
const uint16_t VERSION_MINOR = 253; // Last change: Add an isStrict flag to SIL pointer_to_address.
56+
const uint16_t VERSION_MINOR = 254; // Last change: remove tuple default args
5757

5858
using DeclID = PointerEmbeddedInt<unsigned, 31>;
5959
using DeclIDField = BCFixed<31>;
@@ -578,7 +578,6 @@ namespace decls_block {
578578
TUPLE_TYPE_ELT,
579579
IdentifierIDField, // name
580580
TypeIDField, // type
581-
DefaultArgumentField, // default argument
582581
BCFixed<1> // vararg?
583582
>;
584583

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,22 +2543,18 @@ void TupleType::Profile(llvm::FoldingSetNodeID &ID,
25432543
for (const TupleTypeElt &Elt : Fields) {
25442544
ID.AddPointer(Elt.NameAndVariadic.getOpaqueValue());
25452545
ID.AddPointer(Elt.getType().getPointer());
2546-
ID.AddInteger(static_cast<unsigned>(Elt.getDefaultArgKind()));
25472546
}
25482547
}
25492548

25502549
/// getTupleType - Return the uniqued tuple type with the specified elements.
25512550
Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
2552-
if (Fields.size() == 1 && !Fields[0].isVararg() && !Fields[0].hasName()
2553-
&& Fields[0].getDefaultArgKind() == DefaultArgumentKind::None)
2551+
if (Fields.size() == 1 && !Fields[0].isVararg() && !Fields[0].hasName())
25542552
return ParenType::get(C, Fields[0].getType());
25552553

25562554
RecursiveTypeProperties properties;
25572555
for (const TupleTypeElt &Elt : Fields) {
25582556
if (Elt.getType())
25592557
properties |= Elt.getType()->getRecursiveProperties();
2560-
if (Elt.getDefaultArgKind() != DefaultArgumentKind::None)
2561-
properties |= RecursiveTypeProperties::HasDefaultParameter;
25622558
}
25632559

25642560
auto arena = getArena(properties);

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,50 +2642,6 @@ namespace {
26422642
printField("name", elt.getName().str());
26432643
if (elt.isVararg())
26442644
printFlag("vararg");
2645-
switch (elt.getDefaultArgKind()) {
2646-
case DefaultArgumentKind::None:
2647-
break;
2648-
2649-
case DefaultArgumentKind::Column:
2650-
printField("default_arg", "#column");
2651-
break;
2652-
2653-
case DefaultArgumentKind::DSOHandle:
2654-
printField("default_arg", "#dsohandle");
2655-
break;
2656-
2657-
case DefaultArgumentKind::File:
2658-
printField("default_arg", "#file");
2659-
break;
2660-
2661-
case DefaultArgumentKind::Function:
2662-
printField("default_arg", "#function");
2663-
break;
2664-
2665-
case DefaultArgumentKind::Inherited:
2666-
printField("default_arg", "inherited");
2667-
break;
2668-
2669-
case DefaultArgumentKind::Line:
2670-
printField("default_arg", "#line");
2671-
break;
2672-
2673-
case DefaultArgumentKind::Nil:
2674-
printField("default_arg", "nil");
2675-
break;
2676-
2677-
case DefaultArgumentKind::EmptyArray:
2678-
printField("default_arg", "[]");
2679-
break;
2680-
2681-
case DefaultArgumentKind::EmptyDictionary:
2682-
printField("default_arg", "[:]");
2683-
break;
2684-
2685-
case DefaultArgumentKind::Normal:
2686-
printField("default_arg", "normal");
2687-
break;
2688-
}
26892645

26902646
printRec(elt.getType());
26912647
OS << ")";

lib/AST/ASTVerifier.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,6 @@ struct ASTNodeBase {};
14671467

14681468
TupleType *TT = E->getType()->getAs<TupleType>();
14691469
TupleType *SubTT = E->getSubExpr()->getType()->getAs<TupleType>();
1470-
if (!TT || (!SubTT && !E->isSourceScalar())) {
1471-
Out << "Unexpected types in TupleShuffleExpr\n";
1472-
abort();
1473-
}
14741470
auto getSubElementType = [&](unsigned i) {
14751471
if (E->isSourceScalar()) {
14761472
assert(i == 0);
@@ -1480,6 +1476,17 @@ struct ASTNodeBase {};
14801476
}
14811477
};
14821478

1479+
/// Retrieve the ith element type from the resulting tuple type.
1480+
auto getOuterElementType = [&](unsigned i) -> Type {
1481+
if (!TT) {
1482+
if (auto parenTy = dyn_cast<ParenType>(E->getType().getPointer()))
1483+
return parenTy->getUnderlyingType();
1484+
return E->getType();
1485+
}
1486+
1487+
return TT->getElementType(i);
1488+
};
1489+
14831490
unsigned varargsIndex = 0;
14841491
Type varargsType;
14851492
unsigned callerDefaultArgIndex = 0;
@@ -1494,13 +1501,13 @@ struct ASTNodeBase {};
14941501
}
14951502
if (subElem == TupleShuffleExpr::CallerDefaultInitialize) {
14961503
auto init = E->getCallerDefaultArgs()[callerDefaultArgIndex++];
1497-
if (!TT->getElementType(i)->isEqual(init->getType())) {
1504+
if (!getOuterElementType(i)->isEqual(init->getType())) {
14981505
Out << "Type mismatch in TupleShuffleExpr\n";
14991506
abort();
15001507
}
15011508
continue;
15021509
}
1503-
if (!TT->getElementType(i)->isEqual(getSubElementType(subElem))) {
1510+
if (!getOuterElementType(i)->isEqual(getSubElementType(subElem))) {
15041511
Out << "Type mismatch in TupleShuffleExpr\n";
15051512
abort();
15061513
}

lib/AST/Decl.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,21 +1476,18 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
14761476
unsigned idx = 0;
14771477
for (const auto &elt : tupleTy->getElements()) {
14781478
Type eltTy = mapSignatureParamType(ctx, elt.getType());
1479-
if (anyChanged || eltTy.getPointer() != elt.getType().getPointer() ||
1480-
elt.getDefaultArgKind() != DefaultArgumentKind::None) {
1479+
if (anyChanged || eltTy.getPointer() != elt.getType().getPointer()) {
14811480
if (!anyChanged) {
14821481
elements.reserve(tupleTy->getNumElements());
14831482
for (unsigned i = 0; i != idx; ++i) {
14841483
const TupleTypeElt &elt = tupleTy->getElement(i);
14851484
elements.push_back(TupleTypeElt(elt.getType(), elt.getName(),
1486-
DefaultArgumentKind::None,
14871485
elt.isVararg()));
14881486
}
14891487
anyChanged = true;
14901488
}
14911489

14921490
elements.push_back(TupleTypeElt(eltTy, elt.getName(),
1493-
DefaultArgumentKind::None,
14941491
elt.isVararg()));
14951492
}
14961493
++idx;
@@ -1556,9 +1553,7 @@ OverloadSignature ValueDecl::getOverloadSignature() const {
15561553
}
15571554
}
15581555
} else if (isa<SubscriptDecl>(this)) {
1559-
signature.InterfaceType
1560-
= getInterfaceType()->getWithoutDefaultArgs(getASTContext())
1561-
->getCanonicalType();
1556+
signature.InterfaceType = getInterfaceType()->getCanonicalType();
15621557

15631558
// If the subscript occurs within a generic extension context,
15641559
// consider the generic signature of the extension.

lib/AST/Parameter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Type ParameterList::getType(const ASTContext &C) const {
124124

125125
argumentInfo.push_back({
126126
P->getType(), P->getArgumentName(),
127-
P->getDefaultArgumentKind(), P->isVariadic()
127+
P->isVariadic()
128128
});
129129
}
130130

@@ -155,7 +155,7 @@ Type ParameterList::getInterfaceType(DeclContext *DC) const {
155155

156156
argumentInfo.push_back({
157157
type, P->getArgumentName(),
158-
P->getDefaultArgumentKind(), P->isVariadic()
158+
P->isVariadic()
159159
});
160160
}
161161

0 commit comments

Comments
 (0)