Skip to content

Commit 641a70a

Browse files
authored
Merge pull request swiftlang#19010 from slavapestov/function-type-get-old-nfc
More FunctionType::getOld() removals and related cleanups
2 parents 28808ac + 0a25d78 commit 641a70a

File tree

10 files changed

+83
-102
lines changed

10 files changed

+83
-102
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4870,9 +4870,6 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
48704870
const ParameterList *getIndices() const { return Indices; }
48714871
void setIndices(ParameterList *p);
48724872

4873-
/// Retrieve the interface type of the indices.
4874-
Type getIndicesInterfaceType() const;
4875-
48764873
/// \brief Retrieve the type of the element referenced by a subscript
48774874
/// operation.
48784875
Type getElementInterfaceType() const;

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,6 @@ class TypeConverter {
802802
bool isNonObjC = false);
803803
AbstractionPattern getAbstractionPattern(SubscriptDecl *subscript,
804804
bool isNonObjC = false);
805-
AbstractionPattern getIndicesAbstractionPattern(SubscriptDecl *subscript);
806805
AbstractionPattern getAbstractionPattern(EnumElementDecl *element);
807806

808807
SILType getLoweredTypeOfGlobal(VarDecl *var);

lib/AST/Decl.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4913,13 +4913,6 @@ void SubscriptDecl::setIndices(ParameterList *p) {
49134913
Indices->setDeclContextOfParamDecls(this);
49144914
}
49154915

4916-
Type SubscriptDecl::getIndicesInterfaceType() const {
4917-
auto indicesTy = getInterfaceType();
4918-
if (indicesTy->is<ErrorType>())
4919-
return indicesTy;
4920-
return indicesTy->castTo<AnyFunctionType>()->getInput();
4921-
}
4922-
49234916
Type SubscriptDecl::getElementInterfaceType() const {
49244917
auto elementTy = getInterfaceType();
49254918
if (elementTy->is<ErrorType>())

lib/ClangImporter/ImportDecl.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6298,6 +6298,27 @@ void SwiftDeclConverter::recordObjCOverride(AbstractFunctionDecl *decl) {
62986298
}
62996299
}
63006300

6301+
// Note: This function ignores labels.
6302+
static bool areParameterTypesEqual(const ParameterList &params1,
6303+
const ParameterList &params2) {
6304+
if (params1.size() != params2.size())
6305+
return false;
6306+
6307+
for (unsigned i : indices(params1)) {
6308+
if (!params1[i]->getInterfaceType()->isEqual(
6309+
params2[i]->getInterfaceType())) {
6310+
return false;
6311+
}
6312+
6313+
if (params1[i]->getValueOwnership() !=
6314+
params2[i]->getValueOwnership()) {
6315+
return false;
6316+
}
6317+
}
6318+
6319+
return true;
6320+
}
6321+
63016322
void SwiftDeclConverter::recordObjCOverride(SubscriptDecl *subscript) {
63026323
// Figure out the class in which this subscript occurs.
63036324
auto classTy = subscript->getDeclContext()->getSelfClassDecl();
@@ -6314,22 +6335,14 @@ void SwiftDeclConverter::recordObjCOverride(SubscriptDecl *subscript) {
63146335
subscript->getModuleContext()->lookupQualified(
63156336
superDecl, subscript->getFullName(),
63166337
NL_QualifiedDefault | NL_KnownNoDependency, lookup);
6317-
Type unlabeledIndices;
6338+
63186339
for (auto result : lookup) {
63196340
auto parentSub = dyn_cast<SubscriptDecl>(result);
63206341
if (!parentSub)
63216342
continue;
63226343

6323-
// Compute the type of indices for our own subscript operation, lazily.
6324-
if (!unlabeledIndices) {
6325-
unlabeledIndices = subscript->getIndicesInterfaceType()
6326-
->getUnlabeledType(Impl.SwiftContext);
6327-
}
6328-
6329-
// Compute the type of indices for the subscript we found.
6330-
auto parentUnlabeledIndices = parentSub->getIndicesInterfaceType()
6331-
->getUnlabeledType(Impl.SwiftContext);
6332-
if (!unlabeledIndices->isEqual(parentUnlabeledIndices))
6344+
if (!areParameterTypesEqual(*subscript->getIndices(),
6345+
*parentSub->getIndices()))
63336346
continue;
63346347

63356348
// The index types match. This is an override, so mark it as such.

lib/IDE/TypeReconstruction.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,15 +1466,15 @@ static void CreateFunctionType(ASTContext *ast,
14661466
bool escaping,
14671467
bool throws,
14681468
VisitNodeResult &result) {
1469-
Type arg_clang_type;
1470-
Type return_clang_type;
1469+
Type arg_type;
1470+
Type return_type;
14711471

14721472
switch (arg_type_result._types.size()) {
14731473
case 0:
1474-
arg_clang_type = TupleType::getEmpty(*ast);
1474+
arg_type = TupleType::getEmpty(*ast);
14751475
break;
14761476
case 1:
1477-
arg_clang_type = arg_type_result._types.front().getPointer();
1477+
arg_type = arg_type_result._types.front().getPointer();
14781478
break;
14791479
default:
14801480
result._error = "too many argument types for a function type";
@@ -1483,22 +1483,28 @@ static void CreateFunctionType(ASTContext *ast,
14831483

14841484
switch (return_type_result._types.size()) {
14851485
case 0:
1486-
return_clang_type = TupleType::getEmpty(*ast);
1486+
return_type = TupleType::getEmpty(*ast);
14871487
break;
14881488
case 1:
1489-
return_clang_type = return_type_result._types.front().getPointer();
1489+
return_type = return_type_result._types.front().getPointer();
14901490
break;
14911491
default:
14921492
result._error = "too many return types for a function type";
14931493
break;
14941494
}
14951495

1496-
if (arg_clang_type && return_clang_type) {
1497-
result._types.push_back(
1498-
FunctionType::getOld(arg_clang_type, return_clang_type,
1499-
FunctionType::ExtInfo()
1500-
.withNoEscape(!escaping)
1501-
.withThrows(throws)));
1496+
if (arg_type && return_type) {
1497+
// FIXME: We need to either refactor this code to build function parameters
1498+
// directly, or better yet, scrap TypeReconstruction altogether in favor of
1499+
// TypeDecoder which already does the right thing.
1500+
SmallVector<AnyFunctionType::Param, 8> params;
1501+
AnyFunctionType::decomposeInput(arg_type, params);
1502+
1503+
auto ext_info =
1504+
FunctionType::ExtInfo()
1505+
.withNoEscape(!escaping)
1506+
.withThrows(throws);
1507+
result._types.push_back(FunctionType::get(params, return_type, ext_info));
15021508
}
15031509
}
15041510

lib/SIL/AbstractionPattern.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ TypeConverter::getAbstractionPattern(SubscriptDecl *decl, bool isNonObjC) {
5151
->getCanonicalType());
5252
}
5353

54-
AbstractionPattern
55-
TypeConverter::getIndicesAbstractionPattern(SubscriptDecl *decl) {
56-
CanGenericSignature genericSig;
57-
if (auto sig = decl->getGenericSignatureOfContext())
58-
genericSig = sig->getCanonicalSignature();
59-
auto indicesTy = decl->getIndicesInterfaceType();
60-
auto indicesCanTy = indicesTy->getCanonicalType(genericSig);
61-
return AbstractionPattern(genericSig, indicesCanTy);
62-
}
63-
6454
static const clang::Type *getClangType(const clang::Decl *decl) {
6555
if (auto valueDecl = dyn_cast<clang::ValueDecl>(decl)) {
6656
return valueDecl->getType().getTypePtr();

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,14 +2566,7 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
25662566
// If we're emitting into an initialization, we can try shuffling the
25672567
// elements of the initialization.
25682568
if (Initialization *I = C.getEmitInto()) {
2569-
// In Swift 3 mode, we might be stripping off labels from a
2570-
// one-element tuple; the destination type is a ParenType in
2571-
// that case.
2572-
//
2573-
// FIXME: Remove this eventually.
2574-
if (I->canSplitIntoTupleElements() &&
2575-
!(E->getType()->hasParenSugar() &&
2576-
SGF.getASTContext().isSwiftVersion3())) {
2569+
if (I->canSplitIntoTupleElements()) {
25772570
emitTupleShuffleExprInto(*this, E, I);
25782571
return RValue::forInContext();
25792572
}
@@ -2590,24 +2583,6 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
25902583
// Prepare a new tuple to hold the shuffled result.
25912584
RValue result(E->getType()->getCanonicalType());
25922585

2593-
// In Swift 3 mode, we might be stripping off labels from a
2594-
// one-element tuple; the destination type is a ParenType in
2595-
// that case.
2596-
//
2597-
// FIXME: Remove this eventually.
2598-
if (E->getType()->hasParenSugar() &&
2599-
SGF.getASTContext().isSwiftVersion3()) {
2600-
assert(E->getElementMapping().size() == 1);
2601-
auto shuffleIndex = E->getElementMapping()[0];
2602-
assert(shuffleIndex != TupleShuffleExpr::DefaultInitialize &&
2603-
shuffleIndex != TupleShuffleExpr::CallerDefaultInitialize &&
2604-
shuffleIndex != TupleShuffleExpr::Variadic &&
2605-
"Only argument tuples can have default initializers & varargs");
2606-
2607-
result.addElement(std::move(elements[shuffleIndex]).ensurePlusOne(SGF, E));
2608-
return result;
2609-
}
2610-
26112586
auto outerFields = E->getType()->castTo<TupleType>()->getElements();
26122587
auto shuffleIndexIterator = E->getElementMapping().begin();
26132588
auto shuffleIndexEnd = E->getElementMapping().end();

lib/Sema/CSApply.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4429,7 +4429,18 @@ namespace {
44294429
cs.TC.requestMemberLayout(subscript);
44304430

44314431
auto dc = subscript->getInnermostDeclContext();
4432-
auto indexType = subscript->getIndicesInterfaceType();
4432+
4433+
// FIXME: It's not correct to turn the subscript's parameter list
4434+
// into a single type here. Further down we pass it to coerceToType(),
4435+
// but the rules for argument list conversions are different than
4436+
// tuple conversions, and coerceToType() doesn't handle varargs or
4437+
// default arguments.
4438+
auto indexType = AnyFunctionType::composeInput(
4439+
cs.TC.Context,
4440+
subscript->getInterfaceType()
4441+
->castTo<AnyFunctionType>()
4442+
->getParams(),
4443+
/*canonicalVararg=*/false);
44334444

44344445
SubstitutionMap subs;
44354446
if (auto sig = dc->getGenericSignatureOfContext()) {

lib/Sema/CSDiag.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5403,18 +5403,19 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
54035403
return false;
54045404

54055405
class ClosureCalleeListener : public ExprTypeCheckListener {
5406-
Type InputType;
5406+
FunctionType *InputType;
54075407
Type ResultType;
54085408

54095409
public:
5410-
explicit ClosureCalleeListener(Type inputType, Type resultType)
5410+
explicit ClosureCalleeListener(FunctionType *inputType, Type resultType)
54115411
: InputType(inputType), ResultType(resultType) {}
54125412

54135413
bool builtConstraints(ConstraintSystem &cs, Expr *expr) override {
5414-
if (!InputType || !ResultType)
5414+
if (!ResultType)
54155415
return false;
54165416

5417-
auto expectedType = FunctionType::getOld(InputType, ResultType);
5417+
AnyFunctionType::Param Input(InputType);
5418+
auto expectedType = FunctionType::get({Input}, ResultType);
54185419
cs.addConstraint(ConstraintKind::Conversion, cs.getType(expr),
54195420
expectedType, cs.getConstraintLocator(expr),
54205421
/*isFavored*/ true);
@@ -5451,11 +5452,9 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
54515452
auto *DC = CS.DC;
54525453

54535454
auto typeCheckExpr = [](TypeChecker &TC, Expr *expr, DeclContext *DC,
5454-
SmallPtrSetImpl<TypeBase *> &types,
5455-
Type contextualType = Type()) {
5456-
CalleeListener listener(contextualType);
5455+
SmallPtrSetImpl<TypeBase *> &types) {
54575456
TC.getPossibleTypesOfExpressionWithoutApplying(
5458-
expr, DC, types, FreeTypeVariableBinding::Disallow, &listener);
5457+
expr, DC, types, FreeTypeVariableBinding::Disallow);
54595458
};
54605459

54615460
// First let's type-check expression without contextual type, and
@@ -5470,22 +5469,9 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
54705469
if (withoutContextual.empty())
54715470
return false;
54725471

5473-
SmallPtrSet<TypeBase *, 4> withContextual;
5474-
typeCheckExpr(TC, callExpr, DC, withContextual, contextualType);
5475-
// If type-checking with contextual type didn't produce any results
5476-
// it means that we have a contextual mismatch.
5477-
if (withContextual.empty()) {
5478-
// If there is just a single choice, we can hit contextual diagnostics
5479-
// about it in case re-typecheck fails.
5480-
Type exprType = withoutContextual.size() == 1 ? *withoutContextual.begin() : Type();
5481-
return diagnoseContextualConversionError(callExpr, contextualType, CTP,
5482-
exprType);
5483-
}
5484-
5485-
// If call produces a single type when type-checked with contextual
5486-
// expression, it means that the problem is elsewhere, any other
5487-
// outcome is ambiguous.
5488-
return false;
5472+
Type exprType = withoutContextual.size() == 1 ? *withoutContextual.begin() : Type();
5473+
return diagnoseContextualConversionError(callExpr, contextualType, CTP,
5474+
exprType);
54895475
}
54905476

54915477
bool FailureDiagnosis::diagnoseSubscriptMisuse(ApplyExpr *callExpr) {

lib/Sema/CSGen.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,9 +1530,16 @@ namespace {
15301530
CS.getConstraintLocator(expr, ConstraintLocator::RValueAdjustment));
15311531

15321532
// The function/enum case must be callable with the given argument.
1533-
auto funcTy = FunctionType::getOld(CS.getType(arg), outputTy);
1534-
CS.addConstraint(ConstraintKind::ApplicableFunction, funcTy,
1535-
memberTy,
1533+
1534+
// FIXME: Redesign the AST so that an UnresolvedMemberExpr directly
1535+
// stores a list of arguments together with their inout-ness, instead of
1536+
// a single ParenExpr or TupleExpr argument.
1537+
SmallVector<AnyFunctionType::Param, 8> params;
1538+
AnyFunctionType::decomposeInput(CS.getType(arg), params);
1539+
1540+
CS.addConstraint(ConstraintKind::ApplicableFunction,
1541+
FunctionType::get(params, outputTy),
1542+
memberTy,
15361543
CS.getConstraintLocator(expr, ConstraintLocator::ApplyFunction));
15371544

15381545
return baseTy;
@@ -2530,11 +2537,15 @@ namespace {
25302537
FunctionType::ExtInfo extInfo;
25312538
if (isa<ClosureExpr>(fnExpr->getSemanticsProvidingExpr()))
25322539
extInfo = extInfo.withNoEscape();
2533-
2534-
auto funcTy = FunctionType::getOld(CS.getType(expr->getArg()), outputTy,
2535-
extInfo);
25362540

2537-
CS.addConstraint(ConstraintKind::ApplicableFunction, funcTy,
2541+
// FIXME: Redesign the AST so that an ApplyExpr directly stores a list of
2542+
// arguments together with their inout-ness, instead of a single
2543+
// ParenExpr or TupleExpr.
2544+
SmallVector<AnyFunctionType::Param, 8> params;
2545+
AnyFunctionType::decomposeInput(CS.getType(expr->getArg()), params);
2546+
2547+
CS.addConstraint(ConstraintKind::ApplicableFunction,
2548+
FunctionType::get(params, outputTy, extInfo),
25382549
CS.getType(expr->getFn()),
25392550
CS.getConstraintLocator(expr, ConstraintLocator::ApplyFunction));
25402551

0 commit comments

Comments
 (0)