Skip to content

Commit 0f11feb

Browse files
committed
[Cleanup] Drop needless TupleTypeElt constructor calls
Now that TupleTypeElts are simpler in Swift 3 (though they're about to become more complicated for other reasons), most of the cases where we are explicitly constructing ones are really just plain copies or can otherwise use existing helper functions. NFC
1 parent a0b6cdd commit 0f11feb

File tree

7 files changed

+24
-67
lines changed

7 files changed

+24
-67
lines changed

include/swift/AST/Types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ class TupleTypeElt {
13411341
TupleTypeElt getWithType(Type T) const {
13421342
return TupleTypeElt(T, getName(), isVararg());
13431343
}
1344+
1345+
/// Retrieve a copy of this tuple type element with the name replaced.
1346+
TupleTypeElt getWithName(Identifier name = Identifier()) const {
1347+
return TupleTypeElt(getType(), name, isVararg());
1348+
}
13441349
};
13451350

13461351
inline Type getTupleEltType(const TupleTypeElt &elt) {

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,10 +3850,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
38503850
if (auto tupleTy = dyn_cast<TupleType>(inputType.getPointer())) {
38513851
SmallVector<TupleTypeElt, 4> elements;
38523852
elements.reserve(tupleTy->getNumElements());
3853-
for (const auto &elt : tupleTy->getElements()) {
3854-
elements.push_back(TupleTypeElt(elt.getType(), Identifier(),
3855-
elt.isVararg()));
3856-
}
3853+
for (const auto &elt : tupleTy->getElements())
3854+
elements.push_back(elt.getWithName());
38573855
inputType = TupleType::get(elements, inputType->getASTContext());
38583856
}
38593857

lib/AST/Decl.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,17 +1483,12 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
14831483
Type eltTy = mapSignatureParamType(ctx, elt.getType());
14841484
if (anyChanged || eltTy.getPointer() != elt.getType().getPointer()) {
14851485
if (!anyChanged) {
1486-
elements.reserve(tupleTy->getNumElements());
1487-
for (unsigned i = 0; i != idx; ++i) {
1488-
const TupleTypeElt &elt = tupleTy->getElement(i);
1489-
elements.push_back(TupleTypeElt(elt.getType(), elt.getName(),
1490-
elt.isVararg()));
1491-
}
1486+
elements.append(tupleTy->getElements().begin(),
1487+
tupleTy->getElements().begin() + idx);
14921488
anyChanged = true;
14931489
}
14941490

1495-
elements.push_back(TupleTypeElt(eltTy, elt.getName(),
1496-
elt.isVararg()));
1491+
elements.push_back(elt.getWithType(eltTy));
14971492
}
14981493
++idx;
14991494
}

lib/AST/Type.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,7 @@ static Type getStrippedType(const ASTContext &context, Type type,
737737
for (unsigned i = 0; i != idx; ++i) {
738738
const TupleTypeElt &elt = tuple->getElement(i);
739739
Identifier newName = stripLabels? Identifier() : elt.getName();
740-
elements.push_back(TupleTypeElt(elt.getType(), newName,
741-
elt.isVararg()));
740+
elements.push_back(elt.getWithName(newName));
742741
}
743742
anyChanged = true;
744743
}
@@ -1190,9 +1189,7 @@ CanType TypeBase::getCanonicalType() {
11901189
for (const TupleTypeElt &field : TT->getElements()) {
11911190
assert(!field.getType().isNull() &&
11921191
"Cannot get canonical type of un-typechecked TupleType!");
1193-
CanElts.push_back(TupleTypeElt(field.getType()->getCanonicalType(),
1194-
field.getName(),
1195-
field.isVararg()));
1192+
CanElts.push_back(field.getWithType(field.getType()->getCanonicalType()));
11961193
}
11971194

11981195
const ASTContext &C = CanElts[0].getType()->getASTContext();
@@ -3351,17 +3348,13 @@ case TypeKind::Id:
33513348
// elements.
33523349
if (!anyChanged) {
33533350
// Copy all of the previous elements.
3354-
for (unsigned I = 0; I != Index; ++I) {
3355-
const TupleTypeElt &FromElt =tuple->getElement(I);
3356-
elements.push_back(TupleTypeElt(FromElt.getType(), FromElt.getName(),
3357-
FromElt.isVararg()));
3358-
}
3359-
3351+
elements.append(tuple->getElements().begin(),
3352+
tuple->getElements().begin() + Index);
33603353
anyChanged = true;
33613354
}
33623355

33633356
// Add the new tuple element, with the new type, no initializer,
3364-
elements.push_back(TupleTypeElt(eltTy, elt.getName(), elt.isVararg()));
3357+
elements.push_back(elt.getWithType(eltTy));
33653358
++Index;
33663359
}
33673360

lib/Sema/CSApply.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,9 +4277,7 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
42774277
if (fromTupleExpr)
42784278
fromEltType = fromTupleExpr->getElement(sources[i])->getType();
42794279

4280-
toSugarFields.push_back(TupleTypeElt(fromEltType,
4281-
toElt.getName(),
4282-
toElt.isVararg()));
4280+
toSugarFields.push_back(toElt.getWithType(fromEltType));
42834281
fromTupleExprFields[sources[i]] = fromElt;
42844282
continue;
42854283
}
@@ -4314,12 +4312,9 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
43144312
fromTupleExpr->setElement(sources[i], convertedElt);
43154313

43164314
// Record the sugared field name.
4317-
toSugarFields.push_back(TupleTypeElt(convertedElt->getType(),
4318-
toElt.getName(),
4319-
toElt.isVararg()));
4320-
fromTupleExprFields[sources[i]] = TupleTypeElt(convertedElt->getType(),
4321-
fromElt.getName(),
4322-
fromElt.isVararg());
4315+
toSugarFields.push_back(toElt.getWithType(convertedElt->getType()));
4316+
fromTupleExprFields[sources[i]] =
4317+
fromElt.getWithType(convertedElt->getType());
43234318
}
43244319

43254320
// Convert all of the variadic arguments to the destination type.
@@ -4356,10 +4351,8 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
43564351

43574352
fromTupleExpr->setElement(fromFieldIdx, convertedElt);
43584353

4359-
fromTupleExprFields[fromFieldIdx] = TupleTypeElt(
4360-
convertedElt->getType(),
4361-
fromElt.getName(),
4362-
fromElt.isVararg());
4354+
fromTupleExprFields[fromFieldIdx] =
4355+
fromElt.getWithType(convertedElt->getType());
43634356
}
43644357

43654358
// Find the appropriate injection function.
@@ -4452,15 +4445,12 @@ Expr *ExprRewriter::coerceScalarToTuple(Expr *expr, TupleType *toTuple,
44524445
assert(expr->getType()->isEqual(field.getVarargBaseTy()) &&
44534446
"scalar field is not equivalent to dest vararg field?!");
44544447

4455-
sugarFields.push_back(TupleTypeElt(field.getType(),
4456-
field.getName(),
4457-
true));
4448+
sugarFields.push_back(field);
44584449
}
44594450
else {
44604451
assert(expr->getType()->isEqual(field.getType()) &&
44614452
"scalar field is not equivalent to dest tuple field?!");
4462-
sugarFields.push_back(TupleTypeElt(expr->getType(),
4463-
field.getName()));
4453+
sugarFields.push_back(field.getWithType(expr->getType()));
44644454
}
44654455

44664456
// Record the

lib/Sema/CSRanking.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,6 @@ llvm::raw_ostream &constraints::operator<<(llvm::raw_ostream &out,
109109
return out;
110110
}
111111

112-
/// \brief Remove the initializers from any tuple types within the
113-
/// given type.
114-
static Type stripInitializers(Type origType) {
115-
return origType.transform([&](Type type) -> Type {
116-
if (auto tupleTy = type->getAs<TupleType>()) {
117-
SmallVector<TupleTypeElt, 4> fields;
118-
for (const auto &field : tupleTy->getElements()) {
119-
fields.push_back(TupleTypeElt(field.getType(),
120-
field.getName(),
121-
field.isVararg()));
122-
123-
}
124-
return TupleType::get(fields, type->getASTContext());
125-
}
126-
return type;
127-
});
128-
}
129-
130112
///\ brief Compare two declarations for equality when they are used.
131113
///
132114
static bool sameDecl(Decl *decl1, Decl *decl2) {
@@ -1015,11 +997,6 @@ ConstraintSystem::compareSolutions(ConstraintSystem &cs,
1015997
auto type1 = binding.bindings[idx1];
1016998
auto type2 = binding.bindings[idx2];
1017999

1018-
// Strip any initializers from tuples in the type; they aren't
1019-
// to be compared.
1020-
type1 = stripInitializers(type1);
1021-
type2 = stripInitializers(type2);
1022-
10231000
// If the types are equivalent, there's nothing more to do.
10241001
if (type1->isEqual(type2))
10251002
continue;

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ static Type removeArgumentLabels(Type type, unsigned numArgumentLabels) {
625625
SmallVector<TupleTypeElt, 4> elements;
626626
elements.reserve(tupleTy->getNumElements());
627627
for (const auto &elt : tupleTy->getElements()) {
628-
elements.push_back(TupleTypeElt(elt.getType(), Identifier(),
629-
elt.isVararg()));
628+
elements.push_back(elt.getWithName());
630629
}
631630
inputType = TupleType::get(elements, type->getASTContext());
632631
}

0 commit comments

Comments
 (0)