Skip to content

Commit 5a90293

Browse files
committed
Sema: Explicitly set interface type on all AbstractFunctionDecls
Previously getInterfaceType() would punt to getType() if no interface type was set. This patch changes getInterfaceType() to assert if no interface type is set, and updates various places to set the interface type explicitly. This brings us a step closer to removing PolymorphicFunctionType.
1 parent 1e2d192 commit 5a90293

File tree

16 files changed

+117
-130
lines changed

16 files changed

+117
-130
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5508,9 +5508,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
55085508

55095509
ParameterList *ParameterLists[2];
55105510

5511-
/// The type of the initializing constructor.
5512-
Type InitializerType;
5513-
55145511
/// The interface type of the initializing constructor.
55155512
Type InitializerInterfaceType;
55165513

@@ -5542,10 +5539,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
55425539
/// \brief Get the type of the constructed object.
55435540
Type getResultType() const;
55445541

5545-
/// Get the type of the initializing constructor.
5546-
Type getInitializerType() const { return InitializerType; }
5547-
void setInitializerType(Type t) { InitializerType = t; }
5548-
55495542
/// Get the interface type of the initializing constructor.
55505543
Type getInitializerInterfaceType();
55515544
void setInitializerInterfaceType(Type t);

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,8 @@ static CanType stripImmediateLabels(CanType type) {
902902
/// Check whether the given function is non-generic.
903903
static bool isNonGenericIntrinsic(FuncDecl *fn, CanType &input,
904904
CanType &output) {
905-
auto fnType = dyn_cast<FunctionType>(fn->getType()->getCanonicalType());
905+
auto fnType = dyn_cast<FunctionType>(fn->getInterfaceType()
906+
->getCanonicalType());
906907
if (!fnType)
907908
return false;
908909

lib/AST/Builtins.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
168168
/*GenericParams=*/nullptr,
169169
paramList, FnType,
170170
TypeLoc::withoutLoc(ResType), DC);
171+
FD->setInterfaceType(FnType);
171172
FD->setImplicit();
172173
FD->setAccessibility(Accessibility::Public);
173174
return FD;

lib/AST/Decl.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,9 @@ Type ValueDecl::getInterfaceType() const {
17051705
if (!hasType())
17061706
return Type();
17071707

1708+
assert(!isa<AbstractFunctionDecl>(this) &&
1709+
"functions should have an interface type");
1710+
17081711
// If the type involves a type variable, don't cache it.
17091712
auto type = getType();
17101713
assert((type.isNull() || !type->is<PolymorphicFunctionType>())
@@ -4550,17 +4553,6 @@ Type ConstructorDecl::getResultType() const {
45504553
}
45514554

45524555
Type ConstructorDecl::getInitializerInterfaceType() {
4553-
if (!InitializerInterfaceType) {
4554-
assert((!InitializerType || !InitializerType->is<PolymorphicFunctionType>())
4555-
&& "polymorphic function type is invalid interface type");
4556-
4557-
// Don't cache type variable types.
4558-
if (InitializerType->hasTypeVariable())
4559-
return InitializerType;
4560-
4561-
InitializerInterfaceType = InitializerType;
4562-
}
4563-
45644556
return InitializerInterfaceType;
45654557
}
45664558

lib/ClangImporter/ImportDecl.cpp

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
373373
auto allocFnTy = FunctionType::get(metaTy, fnTy);
374374
auto initFnTy = FunctionType::get(enumTy, fnTy);
375375
ctorDecl->setType(allocFnTy);
376-
ctorDecl->setInitializerType(initFnTy);
376+
ctorDecl->setInterfaceType(allocFnTy);
377+
ctorDecl->setInitializerInterfaceType(initFnTy);
377378

378379
// Don't bother synthesizing the body if we've already finished type-checking.
379380
if (Impl.hasFinishedTypeChecking())
@@ -428,8 +429,13 @@ static FuncDecl *makeEnumRawValueGetter(ClangImporter::Implementation &Impl,
428429
/*GenericParams=*/nullptr, params, Type(),
429430
TypeLoc::withoutLoc(enumDecl->getRawType()), enumDecl);
430431
getterDecl->setImplicit();
431-
getterDecl->setType(ParameterList::getFullInterfaceType(enumDecl->getRawType(),
432-
params, enumDecl));
432+
433+
auto type = ParameterList::getFullInterfaceType(enumDecl->getRawType(),
434+
params, enumDecl);
435+
436+
getterDecl->setType(type);
437+
getterDecl->setInterfaceType(type);
438+
433439
getterDecl->setBodyResultType(enumDecl->getRawType());
434440
getterDecl->setAccessibility(Accessibility::Public);
435441

@@ -488,8 +494,13 @@ static FuncDecl *makeNewtypeBridgedRawValueGetter(
488494
params, Type(),
489495
TypeLoc::withoutLoc(computedType), structDecl);
490496
getterDecl->setImplicit();
491-
getterDecl->setType(ParameterList::getFullInterfaceType(computedType, params,
492-
structDecl));
497+
498+
auto type = ParameterList::getFullInterfaceType(computedType, params,
499+
structDecl);
500+
501+
getterDecl->setType(type);
502+
getterDecl->setInterfaceType(type);
503+
493504
getterDecl->setBodyResultType(computedType);
494505
getterDecl->setAccessibility(Accessibility::Public);
495506

@@ -535,8 +546,12 @@ static FuncDecl *makeFieldGetterDecl(ClangImporter::Implementation &Impl,
535546
/*GenericParams=*/nullptr, params, Type(),
536547
TypeLoc::withoutLoc(getterType), importedDecl, clangNode);
537548
getterDecl->setAccessibility(Accessibility::Public);
538-
getterDecl->setType(ParameterList::getFullInterfaceType(getterType, params,
539-
importedDecl));
549+
550+
auto type = ParameterList::getFullInterfaceType(getterType, params,
551+
importedDecl);
552+
getterDecl->setType(type);
553+
getterDecl->setInterfaceType(type);
554+
540555
getterDecl->setBodyResultType(getterType);
541556

542557
return getterDecl;
@@ -569,8 +584,11 @@ static FuncDecl *makeFieldSetterDecl(ClangImporter::Implementation &Impl,
569584
/*GenericParams=*/nullptr, params, Type(),
570585
TypeLoc::withoutLoc(voidTy), importedDecl, clangNode);
571586

572-
setterDecl->setType(ParameterList::getFullInterfaceType(voidTy, params,
573-
importedDecl));
587+
auto type = ParameterList::getFullInterfaceType(voidTy, params,
588+
importedDecl);
589+
setterDecl->setType(type);
590+
setterDecl->setInterfaceType(type);
591+
574592
setterDecl->setBodyResultType(voidTy);
575593
setterDecl->setAccessibility(Accessibility::Public);
576594
setterDecl->setMutating();
@@ -961,6 +979,7 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
961979
/*AccessorKeywordLoc=*/SourceLoc(),
962980
/*GenericParams=*/nullptr, params, toStringTy,
963981
TypeLoc::withoutLoc(stringTy), swiftDecl);
982+
getterDecl->setInterfaceType(toStringTy);
964983

965984
// Make the property decl
966985
auto errorDomainPropertyDecl = new (C) VarDecl(
@@ -1693,7 +1712,8 @@ namespace {
16931712
auto allocFnTy = FunctionType::get(selfMetatype, fnTy);
16941713
auto initFnTy = FunctionType::get(selfType, fnTy);
16951714
constructor->setType(allocFnTy);
1696-
constructor->setInitializerType(initFnTy);
1715+
constructor->setInterfaceType(allocFnTy);
1716+
constructor->setInitializerInterfaceType(initFnTy);
16971717

16981718
constructor->setAccessibility(Accessibility::Public);
16991719

@@ -1949,7 +1969,8 @@ namespace {
19491969
auto allocFnTy = FunctionType::get(selfMetatype, fnTy);
19501970
auto initFnTy = FunctionType::get(selfType, fnTy);
19511971
constructor->setType(allocFnTy);
1952-
constructor->setInitializerType(initFnTy);
1972+
constructor->setInterfaceType(allocFnTy);
1973+
constructor->setInitializerInterfaceType(initFnTy);
19531974

19541975
constructor->setAccessibility(Accessibility::Public);
19551976

@@ -2885,9 +2906,12 @@ namespace {
28852906
Type argType = parameterList->getType(Impl.SwiftContext);
28862907
Type fnType = FunctionType::get(argType, resultType);
28872908
Type selfType = selfParam->getType();
2888-
result->setInitializerType(FunctionType::get(selfType, fnType));
2909+
Type initType = FunctionType::get(selfType, fnType);
2910+
result->setInitializerInterfaceType(initType);
28892911
Type selfMetaType = MetatypeType::get(selfType->getInOutObjectType());
2890-
result->setType(FunctionType::get(selfMetaType, fnType));
2912+
Type allocType = FunctionType::get(selfMetaType, fnType);
2913+
result->setType(allocType);
2914+
result->setInterfaceType(allocType);
28912915

28922916
finishFuncDecl(decl, result);
28932917
return result;
@@ -2951,16 +2975,12 @@ namespace {
29512975
/*GenericParams=*/nullptr, bodyParams, Type(),
29522976
TypeLoc::withoutLoc(swiftResultTy), dc, decl);
29532977

2954-
if (auto proto = dc->getAsProtocolOrProtocolExtensionContext()) {
2955-
Type interfaceType;
2956-
std::tie(fnType, interfaceType) =
2957-
getGenericMethodType(proto, fnType->castTo<AnyFunctionType>());
2958-
result->setType(fnType);
2959-
result->setInterfaceType(interfaceType);
2960-
result->setGenericSignature(proto->getGenericSignature());
2961-
} else {
2962-
result->setType(fnType);
2963-
}
2978+
Type interfaceType;
2979+
std::tie(fnType, interfaceType) =
2980+
getGenericMethodType(dc, fnType->castTo<AnyFunctionType>());
2981+
result->setType(fnType);
2982+
result->setInterfaceType(interfaceType);
2983+
result->setGenericSignature(dc->getGenericSignatureOfContext());
29642984

29652985
result->setBodyResultType(swiftResultTy);
29662986
result->setAccessibility(Accessibility::Public);
@@ -3250,6 +3270,7 @@ namespace {
32503270
/*GenericParams=*/nullptr, bodyParams, type,
32513271
TypeLoc::withoutLoc(resultTy), dc, decl);
32523272

3273+
result->setInterfaceType(type);
32533274
result->setBodyResultType(resultTy);
32543275

32553276
result->setAccessibility(Accessibility::Public);
@@ -3734,10 +3755,8 @@ namespace {
37343755
// Add the 'self' parameter to the function type.
37353756
type = FunctionType::get(selfInterfaceType, type);
37363757

3737-
if (dc->isGenericContext()) {
3738-
std::tie(type, interfaceType)
3739-
= getGenericMethodType(dc, type->castTo<AnyFunctionType>());
3740-
}
3758+
std::tie(type, interfaceType)
3759+
= getGenericMethodType(dc, type->castTo<AnyFunctionType>());
37413760

37423761
result->setBodyResultType(resultTy);
37433762
result->setType(type);
@@ -4207,21 +4226,18 @@ namespace {
42074226
addObjCAttribute(result, selector);
42084227

42094228
// Calculate the function type of the result.
4210-
if (dc->isGenericContext()) {
4211-
Type interfaceAllocType;
4212-
Type interfaceInitType;
4213-
std::tie(allocType, interfaceAllocType)
4214-
= getGenericMethodType(dc, allocType->castTo<AnyFunctionType>());
4215-
std::tie(initType, interfaceInitType)
4216-
= getGenericMethodType(dc, initType->castTo<AnyFunctionType>());
4217-
4218-
result->setInitializerInterfaceType(interfaceInitType);
4219-
result->setInterfaceType(interfaceAllocType);
4220-
result->setGenericSignature(dc->getGenericSignatureOfContext());
4221-
}
4229+
Type interfaceAllocType;
4230+
Type interfaceInitType;
4231+
std::tie(allocType, interfaceAllocType)
4232+
= getGenericMethodType(dc, allocType->castTo<AnyFunctionType>());
4233+
std::tie(initType, interfaceInitType)
4234+
= getGenericMethodType(dc, initType->castTo<AnyFunctionType>());
4235+
4236+
result->setInitializerInterfaceType(interfaceInitType);
4237+
result->setInterfaceType(interfaceAllocType);
4238+
result->setGenericSignature(dc->getGenericSignatureOfContext());
42224239

42234240
result->setType(allocType);
4224-
result->setInitializerType(initType);
42254241

42264242
if (implicit)
42274243
result->setImplicit();
@@ -4305,6 +4321,10 @@ namespace {
43054321
AnyFunctionType *fnType) {
43064322
assert(!fnType->hasArchetype());
43074323

4324+
auto *sig = dc->getGenericSignatureOfContext();
4325+
if (!sig)
4326+
return { fnType, fnType };
4327+
43084328
Type inputType = ArchetypeBuilder::mapTypeIntoContext(
43094329
dc, fnType->getInput());
43104330
Type resultType = ArchetypeBuilder::mapTypeIntoContext(
@@ -4313,7 +4333,7 @@ namespace {
43134333
dc->getGenericParamsOfContext());
43144334

43154335
Type interfaceType = GenericFunctionType::get(
4316-
dc->getGenericSignatureOfContext(),
4336+
sig,
43174337
fnType->getInput(), fnType->getResult(),
43184338
AnyFunctionType::ExtInfo());
43194339

@@ -4338,10 +4358,8 @@ namespace {
43384358
ParameterList::getFullInterfaceType(elementTy, getterArgs, dc);
43394359

43404360
Type interfaceType;
4341-
if (dc->isGenericContext()) {
4342-
std::tie(getterType, interfaceType)
4343-
= getGenericMethodType(dc, getterType->castTo<AnyFunctionType>());
4344-
}
4361+
std::tie(getterType, interfaceType)
4362+
= getGenericMethodType(dc, getterType->castTo<AnyFunctionType>());
43454363

43464364
// Create the getter thunk.
43474365
FuncDecl *thunk = FuncDecl::create(
@@ -4409,10 +4427,8 @@ namespace {
44094427
dc);
44104428

44114429
Type interfaceType;
4412-
if (dc->isGenericContext()) {
4413-
std::tie(setterType, interfaceType)
4414-
= getGenericMethodType(dc, setterType->castTo<AnyFunctionType>());
4415-
}
4430+
std::tie(setterType, interfaceType)
4431+
= getGenericMethodType(dc, setterType->castTo<AnyFunctionType>());
44164432

44174433
// Create the setter thunk.
44184434
FuncDecl *thunk = FuncDecl::create(
@@ -4769,8 +4785,12 @@ namespace {
47694785
subscript->makeComputed(SourceLoc(), getterThunk, setterThunk, nullptr,
47704786
SourceLoc());
47714787
auto indicesType = bodyParams->getType(C);
4772-
4773-
subscript->setType(FunctionType::get(indicesType, elementTy)); // TODO: no good when generics are around
4788+
4789+
// TODO: no good when generics are around
4790+
auto fnType = FunctionType::get(indicesType, elementTy);
4791+
subscript->setType(fnType);
4792+
subscript->setInterfaceType(fnType);
4793+
47744794
addObjCAttribute(subscript, None);
47754795

47764796
// Optional subscripts in protocols.
@@ -6898,6 +6918,7 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
68986918
/*GenericParams=*/nullptr, getterArgs,
68996919
getterType, TypeLoc::withoutLoc(type), dc);
69006920
func->setStatic(isStatic);
6921+
func->setInterfaceType(getterType);
69016922
func->setBodyResultType(type);
69026923
func->setAccessibility(Accessibility::Public);
69036924

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,6 +3825,7 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
38253825
auto flagInvalidAccessor = [&](FuncDecl *&func) {
38263826
if (func) {
38273827
func->setType(ErrorType::get(P.Context));
3828+
func->setInterfaceType(ErrorType::get(P.Context));
38283829
func->setInvalid();
38293830
}
38303831
};

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,8 @@ namespace {
13091309

13101310
// Compute the concrete reference.
13111311
ConcreteDeclRef ref;
1312-
Type resultTy;
1313-
if (ctor->getInterfaceType()->is<GenericFunctionType>()) {
1312+
Type resultTy = ctor->getInitializerInterfaceType();
1313+
if (resultTy->is<GenericFunctionType>()) {
13141314
// Compute the reference to the generic constructor.
13151315
SmallVector<Substitution, 4> substitutions;
13161316
resultTy = solution.computeSubstitutions(
@@ -1333,7 +1333,6 @@ namespace {
13331333
resultFnTy->getExtInfo());
13341334
} else {
13351335
ref = ConcreteDeclRef(ctor);
1336-
resultTy = ctor->getInitializerType();
13371336
}
13381337

13391338
// Build the constructor reference.
@@ -2183,7 +2182,8 @@ namespace {
21832182
}
21842183

21852184
Expr *visitOtherConstructorDeclRefExpr(OtherConstructorDeclRefExpr *expr) {
2186-
expr->setType(expr->getDecl()->getInitializerType());
2185+
assert(!expr->getDeclRef().isSpecialized());
2186+
expr->setType(expr->getDecl()->getInitializerInterfaceType());
21872187
return expr;
21882188
}
21892189

lib/Sema/CodeSynthesis.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,10 +2142,8 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
21422142
auto selfType = configureImplicitSelf(tc, ctor);
21432143

21442144
// Set the interface type of the initializer.
2145-
if (classDecl->isGenericContext()) {
2146-
ctor->setGenericSignature(classDecl->getGenericSignatureOfContext());
2147-
tc.configureInterfaceType(ctor);
2148-
}
2145+
ctor->setGenericSignature(classDecl->getGenericSignatureOfContext());
2146+
tc.configureInterfaceType(ctor);
21492147

21502148
// Set the contextual type of the initializer. This will go away one day.
21512149
configureConstructorType(ctor, selfType, bodyParams->getType(ctx));

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,11 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
305305
Type selfMetatype = MetatypeType::get(selfType->getInOutObjectType());
306306

307307
Type allocType;
308-
Type initType;
309-
if (genericParams) {
308+
if (genericParams)
310309
allocType = PolymorphicFunctionType::get(selfMetatype, type, genericParams);
311-
initType = PolymorphicFunctionType::get(selfType, type, genericParams);
312-
} else {
310+
else
313311
allocType = FunctionType::get(selfMetatype, type);
314-
initType = FunctionType::get(selfType, type);
315-
}
316312
initDecl->setType(allocType);
317-
initDecl->setInitializerType(initType);
318313

319314
// Compute the interface type of the initializer.
320315
Type retInterfaceType

0 commit comments

Comments
 (0)