Skip to content

Commit 59a74b4

Browse files
committed
Sema: Clean up configureImplicitSelf() and related code to not return a generic parameter list, NFC
Instead, get the generic parameter list from the DeclContext, since now we need it even if there's no 'self' type.
1 parent 74e575e commit 59a74b4

9 files changed

+50
-61
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,11 +4568,7 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
45684568
/// and return the type to be used for the 'self' argument of the type, or an
45694569
/// empty Type() if no 'self' argument should exist. This can
45704570
/// only be used after name binding has resolved types.
4571-
///
4572-
/// \param outerGenericParams If non-NULL, and this function is an instance
4573-
/// of a generic type, will be set to the generic parameter list of that
4574-
/// generic type.
4575-
Type computeSelfType(GenericParamList **outerGenericParams = nullptr);
4571+
Type computeSelfType();
45764572

45774573
/// \brief If this is a method in a type or extension thereof, compute
45784574
/// and return the type to be used for the 'self' argument of the interface

lib/AST/Decl.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,8 +3429,7 @@ SourceRange SubscriptDecl::getSourceRange() const {
34293429

34303430
static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
34313431
bool isInitializingCtor,
3432-
bool wantInterfaceType,
3433-
GenericParamList **outerGenericParams) {
3432+
bool wantInterfaceType) {
34343433
auto *dc = theMethod->getDeclContext();
34353434

34363435
// Determine the type of the container.
@@ -3481,10 +3480,6 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
34813480
}
34823481
}
34833482

3484-
// Capture the generic parameters, if requested.
3485-
if (outerGenericParams)
3486-
*outerGenericParams = dc->getGenericParamsOfContext();
3487-
34883483
// If the self type couldn't be computed, or is the result of an
34893484
// upstream error, return an error type.
34903485
if (!selfTy || selfTy->is<ErrorType>())
@@ -3560,13 +3555,12 @@ void AbstractFunctionDecl::setGenericParams(GenericParamList *GP) {
35603555
}
35613556

35623557

3563-
Type AbstractFunctionDecl::
3564-
computeSelfType(GenericParamList **outerGenericParams) {
3565-
return getSelfTypeForContainer(this, true, false, outerGenericParams);
3558+
Type AbstractFunctionDecl::computeSelfType() {
3559+
return getSelfTypeForContainer(this, true, false);
35663560
}
35673561

35683562
Type AbstractFunctionDecl::computeInterfaceSelfType(bool isInitializingCtor) {
3569-
return getSelfTypeForContainer(this, isInitializingCtor, true, nullptr);
3563+
return getSelfTypeForContainer(this, isInitializingCtor, true);
35703564
}
35713565

35723566
/// \brief This method returns the implicit 'self' decl.

lib/Sema/CodeSynthesis.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,13 +2238,12 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
22382238
ctx);
22392239

22402240
// Configure 'self'.
2241-
GenericParamList *outerGenericParams = nullptr;
2242-
Type selfType = configureImplicitSelf(tc, ctor, outerGenericParams);
2241+
Type selfType = configureImplicitSelf(tc, ctor);
22432242
selfBodyPattern->setType(selfType);
22442243
cast<TypedPattern>(selfBodyPattern)->getSubPattern()->setType(selfType);
22452244

22462245
// Set the type of the initializer.
2247-
configureConstructorType(ctor, outerGenericParams, selfType,
2246+
configureConstructorType(ctor, selfType,
22482247
bodyParamPatterns->getType(),
22492248
superclassCtor->isBodyThrowing());
22502249
if (superclassCtor->isObjC()) {

lib/Sema/CodeSynthesis.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ void markAsObjC(TypeChecker &TC, ValueDecl *D,
4747
Optional<ObjCReason> isObjC,
4848
Optional<ForeignErrorConvention> errorConvention = None);
4949
Type configureImplicitSelf(TypeChecker &tc,
50-
AbstractFunctionDecl *func,
51-
GenericParamList *&outerGenericParams);
50+
AbstractFunctionDecl *func);
5251
void configureConstructorType(ConstructorDecl *ctor,
53-
GenericParamList *outerGenericParams,
5452
Type selfType,
5553
Type argType,
5654
bool throws);

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,29 @@ deriveEquatable_enum_eq(TypeChecker &tc, Decl *parentDecl, EnumDecl *enumDecl) {
266266
eqDecl->setDerivedForTypeDecl(enumDecl);
267267
eqDecl->setBodySynthesizer(&deriveBodyEquatable_enum_eq);
268268

269-
// Compute the type and interface type.
270-
Type fnTy, interfaceTy;
271-
if (genericParams) {
269+
// Compute the type.
270+
Type fnTy;
271+
if (genericParams)
272272
fnTy = PolymorphicFunctionType::get(paramsTy, boolTy, genericParams);
273-
273+
else
274+
fnTy = FunctionType::get(paramsTy, boolTy);
275+
eqDecl->setType(fnTy);
276+
277+
// Compute the interface type.
278+
Type interfaceTy;
279+
if (auto genericSig = parentDC->getGenericSignatureOfContext()) {
274280
auto enumIfaceTy = parentDC->getDeclaredInterfaceType();
275281
TupleTypeElt ifaceParamElts[] = {
276282
enumIfaceTy, enumIfaceTy,
277283
};
278284
auto ifaceParamsTy = TupleType::get(ifaceParamElts, C);
279285

280286
interfaceTy = GenericFunctionType::get(
281-
parentDC->getGenericSignatureOfContext(),
282-
ifaceParamsTy, boolTy,
287+
genericSig, ifaceParamsTy, boolTy,
283288
AnyFunctionType::ExtInfo());
284289
} else {
285-
fnTy = interfaceTy = FunctionType::get(paramsTy, boolTy);
290+
interfaceTy = FunctionType::get(paramsTy, boolTy);
286291
}
287-
eqDecl->setType(fnTy);
288292
eqDecl->setInterfaceType(interfaceTy);
289293

290294
// Since we can't insert the == operator into the same FileUnit as the enum,
@@ -414,9 +418,9 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl,
414418
getterDecl->setBodySynthesizer(deriveBodyHashable_enum_hashValue);
415419

416420
// Compute the type of hashValue().
417-
GenericParamList *genericParams = nullptr;
421+
GenericParamList *genericParams = getterDecl->getGenericParamsOfContext();
418422
Type methodType = FunctionType::get(TupleType::getEmpty(tc.Context), intType);
419-
Type selfType = getterDecl->computeSelfType(&genericParams);
423+
Type selfType = getterDecl->computeSelfType();
420424
Type type;
421425
if (genericParams)
422426
type = PolymorphicFunctionType::get(selfType, methodType, genericParams);
@@ -432,7 +436,7 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl,
432436
interfaceType = GenericFunctionType::get(sig, selfIfaceType, methodType,
433437
AnyFunctionType::ExtInfo());
434438
else
435-
interfaceType = type;
439+
interfaceType = FunctionType::get(selfType, methodType);
436440

437441
getterDecl->setInterfaceType(interfaceType);
438442
getterDecl->setAccessibility(enumDecl->getFormalAccess());

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,16 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
318318
initDecl->setBodySynthesizer(&deriveBodyRawRepresentable_init);
319319

320320
// Compute the type of the initializer.
321-
GenericParamList *genericParams = nullptr;
321+
GenericParamList *genericParams = initDecl->getGenericParamsOfContext();
322322

323323
TupleTypeElt element(rawType, C.Id_rawValue);
324324
auto argType = TupleType::get(element, C);
325325
TupleTypeElt interfaceElement(rawInterfaceType, C.Id_rawValue);
326326
auto interfaceArgType = TupleType::get(interfaceElement, C);
327327

328328
Type type = FunctionType::get(argType, retTy);
329-
Type selfType = initDecl->computeSelfType(&genericParams);
329+
330+
Type selfType = initDecl->computeSelfType();
330331
Type selfMetatype = MetatypeType::get(selfType->getInOutObjectType());
331332

332333
Type allocType;
@@ -359,8 +360,8 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
359360
interfaceType,
360361
FunctionType::ExtInfo());
361362
} else {
362-
allocIfaceType = allocType;
363-
initIfaceType = initType;
363+
allocIfaceType = FunctionType::get(selfMetatype, type);
364+
initIfaceType = FunctionType::get(selfType, type);
364365
}
365366
initDecl->setInterfaceType(allocIfaceType);
366367
initDecl->setInitializerInterfaceType(initIfaceType);

lib/Sema/DerivedConformances.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ FuncDecl *DerivedConformance::declareDerivedPropertyGetter(TypeChecker &tc,
165165
getterDecl->setStatic(isStatic);
166166

167167
// Compute the type of the getter.
168-
GenericParamList *genericParams = nullptr;
168+
GenericParamList *genericParams = getterDecl->getGenericParamsOfContext();
169169
Type type = FunctionType::get(TupleType::getEmpty(C),
170170
propertyContextType);
171-
selfType = getterDecl->computeSelfType(&genericParams);
171+
selfType = getterDecl->computeSelfType();
172172
if (genericParams)
173173
type = PolymorphicFunctionType::get(selfType, type, genericParams);
174174
else

lib/Sema/TypeCheckDecl.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,14 +1374,10 @@ void swift::makeDynamic(ASTContext &ctx, ValueDecl *D) {
13741374
/// pattern, etc.
13751375
///
13761376
/// \param func The function whose 'self' is being configured.
1377-
/// \param outerGenericParams The generic parameters from the outer scope.
13781377
///
13791378
/// \returns the type of 'self'.
13801379
Type swift::configureImplicitSelf(TypeChecker &tc,
1381-
AbstractFunctionDecl *func,
1382-
GenericParamList *&outerGenericParams) {
1383-
outerGenericParams = nullptr;
1384-
1380+
AbstractFunctionDecl *func) {
13851381
auto selfDecl = func->getImplicitSelfDecl();
13861382

13871383
// Validate the context.
@@ -1392,7 +1388,7 @@ Type swift::configureImplicitSelf(TypeChecker &tc,
13921388
}
13931389

13941390
// Compute the type of self.
1395-
Type selfTy = func->computeSelfType(&outerGenericParams);
1391+
Type selfTy = func->computeSelfType();
13961392
assert(selfDecl && selfTy && "Not a method");
13971393

13981394
if (selfDecl->hasType())
@@ -1413,7 +1409,6 @@ Type swift::configureImplicitSelf(TypeChecker &tc,
14131409
/// Compute the allocating and initializing constructor types for
14141410
/// the given constructor.
14151411
void swift::configureConstructorType(ConstructorDecl *ctor,
1416-
GenericParamList *outerGenericParams,
14171412
Type selfType,
14181413
Type argType,
14191414
bool throws) {
@@ -1431,6 +1426,9 @@ void swift::configureConstructorType(ConstructorDecl *ctor,
14311426

14321427
auto extInfo = AnyFunctionType::ExtInfo().withThrows(throws);
14331428

1429+
GenericParamList *outerGenericParams =
1430+
ctor->getDeclContext()->getGenericParamsOfContext();
1431+
14341432
if (GenericParamList *innerGenericParams = ctor->getGenericParams()) {
14351433
innerGenericParams->setOuterParameters(outerGenericParams);
14361434
fnType = PolymorphicFunctionType::get(argType, resultType,
@@ -4009,13 +4007,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
40094007
}
40104008

40114009
// Before anything else, set up the 'self' argument correctly if present.
4012-
GenericParamList *outerGenericParams = nullptr;
40134010
if (FD->getDeclContext()->isTypeContext())
4014-
configureImplicitSelf(TC, FD, outerGenericParams);
4011+
configureImplicitSelf(TC, FD);
40154012

40164013
// If we have generic parameters, check the generic signature now.
40174014
if (auto gp = FD->getGenericParams()) {
4018-
gp->setOuterParameters(outerGenericParams);
4015+
gp->setOuterParameters(FD->getDeclContext()->getGenericParamsOfContext());
40194016

40204017
if (TC.validateGenericFuncSignature(FD)) {
40214018
markInvalidGenericSignature(FD, TC);
@@ -5394,13 +5391,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
53945391
}
53955392
}
53965393

5397-
GenericParamList *outerGenericParams;
5398-
Type SelfTy = configureImplicitSelf(TC, CD, outerGenericParams);
5394+
Type SelfTy = configureImplicitSelf(TC, CD);
53995395

54005396
Optional<ArchetypeBuilder> builder;
54015397
if (auto gp = CD->getGenericParams()) {
54025398
// Write up generic parameters and check the generic parameter list.
5403-
gp->setOuterParameters(outerGenericParams);
5399+
gp->setOuterParameters(CD->getDeclContext()->getGenericParamsOfContext());
54045400

54055401
if (TC.validateGenericFuncSignature(CD)) {
54065402
markInvalidGenericSignature(CD, TC);
@@ -5433,7 +5429,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
54335429
CD->overwriteType(ErrorType::get(TC.Context));
54345430
CD->setInvalid();
54355431
} else {
5436-
configureConstructorType(CD, outerGenericParams, SelfTy,
5432+
configureConstructorType(CD, SelfTy,
54375433
CD->getBodyParamPatterns()[1]->getType(),
54385434
CD->getThrowsLoc().isValid());
54395435
}
@@ -5562,8 +5558,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
55625558
DD->setAccessibility(enclosingClass->getFormalAccess());
55635559
}
55645560

5565-
GenericParamList *outerGenericParams;
5566-
Type SelfTy = configureImplicitSelf(TC, DD, outerGenericParams);
5561+
Type SelfTy = configureImplicitSelf(TC, DD);
55675562

55685563
if (DD->getDeclContext()->isGenericTypeContext())
55695564
TC.validateGenericFuncSignature(DD);
@@ -5577,7 +5572,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
55775572
if (DD->getDeclContext()->isGenericTypeContext())
55785573
FnTy = PolymorphicFunctionType::get(SelfTy,
55795574
TupleType::getEmpty(TC.Context),
5580-
outerGenericParams);
5575+
DD->getDeclContext()->getGenericParamsOfContext());
55815576
else
55825577
FnTy = FunctionType::get(SelfTy, TupleType::getEmpty(TC.Context));
55835578

@@ -5891,9 +5886,8 @@ void TypeChecker::validateDecl(ValueDecl *D, bool resolveTypeParams) {
58915886
// Validate the generic type signature, which is just <Self : P>.
58925887
validateGenericTypeSignature(proto);
58935888

5894-
GenericParamList *outerGenericParams =
5895-
proto->getDeclContext()->getGenericParamsOfContext();
5896-
gp->setOuterParameters(outerGenericParams);
5889+
assert(gp->getOuterParameters() ==
5890+
proto->getDeclContext()->getGenericParamsOfContext());
58975891

58985892
revertGenericParamList(gp);
58995893

@@ -5985,8 +5979,7 @@ void TypeChecker::validateDecl(ValueDecl *D, bool resolveTypeParams) {
59855979
if (isa<NominalTypeDecl>(VD->getDeclContext()->getParent())) {
59865980
if (auto funcDeclContext =
59875981
dyn_cast<AbstractFunctionDecl>(VD->getDeclContext())) {
5988-
GenericParamList *outerGenericParams = nullptr;
5989-
configureImplicitSelf(*this, funcDeclContext, outerGenericParams);
5982+
configureImplicitSelf(*this, funcDeclContext);
59905983
}
59915984
} else {
59925985
D->setType(ErrorType::get(Context));

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ Type DependentGenericTypeResolver::resolveSelfAssociatedType(
4343
Type selfTy,
4444
DeclContext *DC,
4545
AssociatedTypeDecl *assocType) {
46-
return Builder.resolveArchetype(selfTy)->getRepresentative()
46+
auto archetype = Builder.resolveArchetype(selfTy);
47+
if (!archetype)
48+
return ErrorType::get(DC->getASTContext());
49+
50+
return archetype->getRepresentative()
4751
->getNestedType(assocType->getName(), Builder)
4852
->getDependentType(Builder, true);
4953
}

0 commit comments

Comments
 (0)