Skip to content

Commit db423e7

Browse files
committed
Special-case the GenericParamList ctor for WhereClauseOwner
1 parent bf91cf9 commit db423e7

File tree

2 files changed

+58
-57
lines changed

2 files changed

+58
-57
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ class AccessControlCheckerBase {
108108

109109
public:
110110
void checkGenericParamAccess(
111-
const GenericParamList *params,
112-
const Decl *owner,
111+
const GenericContext *ownerCtx,
112+
const Decl *ownerDecl,
113113
AccessScope accessScope,
114114
AccessLevel contextAccess);
115115

116116
void checkGenericParamAccess(
117-
const GenericParamList *params,
118-
const ValueDecl *owner);
117+
const GenericContext *ownerCtx,
118+
const ValueDecl *ownerDecl);
119119
};
120120

121121
class TypeAccessScopeDiagnoser : private ASTWalker {
@@ -315,10 +315,11 @@ static void highlightOffendingType(InFlightDiagnostic &diag,
315315
}
316316

317317
void AccessControlCheckerBase::checkGenericParamAccess(
318-
const GenericParamList *params,
319-
const Decl *owner,
318+
const GenericContext *ownerCtx,
319+
const Decl *ownerDecl,
320320
AccessScope accessScope,
321321
AccessLevel contextAccess) {
322+
auto params = ownerCtx->getGenericParams();
322323
if (!params)
323324
return;
324325

@@ -348,7 +349,7 @@ void AccessControlCheckerBase::checkGenericParamAccess(
348349
}
349350
};
350351

351-
auto *DC = owner->getDeclContext();
352+
auto *DC = ownerDecl->getDeclContext();
352353

353354
for (auto param : *params) {
354355
if (param->getInherited().empty())
@@ -361,18 +362,17 @@ void AccessControlCheckerBase::checkGenericParamAccess(
361362
callbackACEK = ACEK::Requirement;
362363

363364
checkRequirementAccess(WhereClauseOwner(
364-
owner->getInnermostDeclContext(),
365-
const_cast<GenericParamList *>(params)),
365+
const_cast<GenericContext *>(ownerCtx)),
366366
accessScope, DC, callback);
367367

368368
if (minAccessScope.isPublic())
369369
return;
370370

371371
// FIXME: Promote these to an error in the next -swift-version break.
372-
if (isa<SubscriptDecl>(owner) || isa<TypeAliasDecl>(owner))
372+
if (isa<SubscriptDecl>(ownerDecl) || isa<TypeAliasDecl>(ownerDecl))
373373
downgradeToWarning = DowngradeToWarning::Yes;
374374

375-
auto &Context = owner->getASTContext();
375+
auto &Context = ownerDecl->getASTContext();
376376
if (checkUsableFromInline) {
377377
if (!Context.isSwiftVersionAtLeast(5))
378378
downgradeToWarning = DowngradeToWarning::Yes;
@@ -381,7 +381,7 @@ void AccessControlCheckerBase::checkGenericParamAccess(
381381
if (downgradeToWarning == DowngradeToWarning::Yes)
382382
diagID = diag::generic_param_usable_from_inline_warn;
383383
auto diag =
384-
Context.Diags.diagnose(owner, diagID, owner->getDescriptiveKind(),
384+
Context.Diags.diagnose(ownerDecl, diagID, ownerDecl->getDescriptiveKind(),
385385
accessControlErrorKind == ACEK::Requirement);
386386
highlightOffendingType(diag, complainRepr);
387387
return;
@@ -390,25 +390,25 @@ void AccessControlCheckerBase::checkGenericParamAccess(
390390
auto minAccess = minAccessScope.accessLevelForDiagnostics();
391391

392392
bool isExplicit =
393-
owner->getAttrs().hasAttribute<AccessControlAttr>() ||
394-
isa<ProtocolDecl>(owner->getDeclContext());
393+
ownerDecl->getAttrs().hasAttribute<AccessControlAttr>() ||
394+
isa<ProtocolDecl>(DC);
395395
auto diagID = diag::generic_param_access;
396396
if (downgradeToWarning == DowngradeToWarning::Yes)
397397
diagID = diag::generic_param_access_warn;
398398
auto diag = Context.Diags.diagnose(
399-
owner, diagID, owner->getDescriptiveKind(), isExplicit, contextAccess,
400-
minAccess, isa<FileUnit>(owner->getDeclContext()),
399+
ownerDecl, diagID, ownerDecl->getDescriptiveKind(), isExplicit,
400+
contextAccess, minAccess, isa<FileUnit>(DC),
401401
accessControlErrorKind == ACEK::Requirement);
402402
highlightOffendingType(diag, complainRepr);
403403
}
404404

405405
void AccessControlCheckerBase::checkGenericParamAccess(
406-
const GenericParamList *params,
407-
const ValueDecl *owner) {
408-
checkGenericParamAccess(params, owner,
409-
owner->getFormalAccessScope(nullptr,
410-
checkUsableFromInline),
411-
owner->getFormalAccess());
406+
const GenericContext *ownerCtx,
407+
const ValueDecl *ownerDecl) {
408+
checkGenericParamAccess(ownerCtx, ownerDecl,
409+
ownerDecl->getFormalAccessScope(
410+
nullptr, checkUsableFromInline),
411+
ownerDecl->getFormalAccess());
412412
}
413413

414414
namespace {
@@ -563,7 +563,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
563563
}
564564

565565
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
566-
checkGenericParamAccess(TAD->getGenericParams(), TAD);
566+
checkGenericParamAccess(TAD, TAD);
567567

568568
checkTypeAccess(TAD->getUnderlyingType(),
569569
TAD->getUnderlyingTypeRepr(), TAD, /*mayBeInferred*/false,
@@ -667,7 +667,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
667667
}
668668

669669
void visitEnumDecl(EnumDecl *ED) {
670-
checkGenericParamAccess(ED->getGenericParams(), ED);
670+
checkGenericParamAccess(ED, ED);
671671

672672
if (ED->hasRawType()) {
673673
Type rawType = ED->getRawType();
@@ -701,11 +701,11 @@ class AccessControlChecker : public AccessControlCheckerBase,
701701
}
702702

703703
void visitStructDecl(StructDecl *SD) {
704-
checkGenericParamAccess(SD->getGenericParams(), SD);
704+
checkGenericParamAccess(SD, SD);
705705
}
706706

707707
void visitClassDecl(ClassDecl *CD) {
708-
checkGenericParamAccess(CD->getGenericParams(), CD);
708+
checkGenericParamAccess(CD, CD);
709709

710710
if (const NominalTypeDecl *superclassDecl = CD->getSuperclassDecl()) {
711711
// Be slightly defensive here in the presence of badly-ordered
@@ -847,7 +847,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
847847
}
848848

849849
void visitSubscriptDecl(SubscriptDecl *SD) {
850-
checkGenericParamAccess(SD->getGenericParams(), SD);
850+
checkGenericParamAccess(SD, SD);
851851

852852
auto minAccessScope = AccessScope::getPublic();
853853
const TypeRepr *complainRepr = nullptr;
@@ -903,7 +903,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
903903
void visitAbstractFunctionDecl(AbstractFunctionDecl *fn) {
904904
bool isTypeContext = fn->getDeclContext()->isTypeContext();
905905

906-
checkGenericParamAccess(fn->getGenericParams(), fn);
906+
checkGenericParamAccess(fn, fn);
907907

908908
// This must stay in sync with diag::function_type_access.
909909
enum {
@@ -1175,7 +1175,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
11751175
}
11761176

11771177
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
1178-
checkGenericParamAccess(TAD->getGenericParams(), TAD);
1178+
checkGenericParamAccess(TAD, TAD);
11791179

11801180
checkTypeAccess(TAD->getUnderlyingType(),
11811181
TAD->getUnderlyingTypeRepr(), TAD, /*mayBeInferred*/false,
@@ -1243,7 +1243,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
12431243
}
12441244

12451245
void visitEnumDecl(const EnumDecl *ED) {
1246-
checkGenericParamAccess(ED->getGenericParams(), ED);
1246+
checkGenericParamAccess(ED, ED);
12471247

12481248
if (ED->hasRawType()) {
12491249
Type rawType = ED->getRawType();
@@ -1271,11 +1271,11 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
12711271
}
12721272

12731273
void visitStructDecl(StructDecl *SD) {
1274-
checkGenericParamAccess(SD->getGenericParams(), SD);
1274+
checkGenericParamAccess(SD, SD);
12751275
}
12761276

12771277
void visitClassDecl(ClassDecl *CD) {
1278-
checkGenericParamAccess(CD->getGenericParams(), CD);
1278+
checkGenericParamAccess(CD, CD);
12791279

12801280
if (CD->hasSuperclass()) {
12811281
const NominalTypeDecl *superclassDecl = CD->getSuperclassDecl();
@@ -1354,7 +1354,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
13541354
}
13551355

13561356
void visitSubscriptDecl(SubscriptDecl *SD) {
1357-
checkGenericParamAccess(SD->getGenericParams(), SD);
1357+
checkGenericParamAccess(SD, SD);
13581358

13591359
for (auto &P : *SD->getIndices()) {
13601360
checkTypeAccess(
@@ -1384,7 +1384,7 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
13841384
void visitAbstractFunctionDecl(AbstractFunctionDecl *fn) {
13851385
bool isTypeContext = fn->getDeclContext()->isTypeContext();
13861386

1387-
checkGenericParamAccess(fn->getGenericParams(), fn);
1387+
checkGenericParamAccess(fn, fn);
13881388

13891389
// This must stay in sync with diag::function_type_usable_from_inline.
13901390
enum {
@@ -1566,25 +1566,26 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
15661566
diagnoseConformance);
15671567
}
15681568

1569-
void checkGenericParams(const GenericParamList *params,
1570-
const Decl *owner) {
1569+
void checkGenericParams(const GenericContext *ownerCtx,
1570+
const ValueDecl *ownerDecl) {
1571+
const auto params = ownerCtx->getGenericParams();
15711572
if (!params)
15721573
return;
15731574

15741575
for (auto param : *params) {
15751576
if (param->getInherited().empty())
15761577
continue;
15771578
assert(param->getInherited().size() == 1);
1578-
checkType(param->getInherited().front(), owner,
1579-
getDiagnoseCallback(owner), getDiagnoseCallback(owner));
1579+
checkType(param->getInherited().front(), ownerDecl,
1580+
getDiagnoseCallback(ownerDecl),
1581+
getDiagnoseCallback(ownerDecl));
15801582
}
15811583

15821584
forAllRequirementTypes(WhereClauseOwner(
1583-
owner->getInnermostDeclContext(),
1584-
const_cast<GenericParamList *>(params)),
1585+
const_cast<GenericContext *>(ownerCtx)),
15851586
[&](Type type, TypeRepr *typeRepr) {
1586-
checkType(type, typeRepr, owner, getDiagnoseCallback(owner),
1587-
getDiagnoseCallback(owner));
1587+
checkType(type, typeRepr, ownerDecl, getDiagnoseCallback(ownerDecl),
1588+
getDiagnoseCallback(ownerDecl));
15881589
});
15891590
}
15901591

@@ -1809,7 +1810,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18091810
}
18101811

18111812
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
1812-
checkGenericParams(TAD->getGenericParams(), TAD);
1813+
checkGenericParams(TAD, TAD);
18131814
checkType(TAD->getUnderlyingType(),
18141815
TAD->getUnderlyingTypeRepr(), TAD, getDiagnoseCallback(TAD),
18151816
getDiagnoseCallback(TAD));
@@ -1835,7 +1836,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18351836
}
18361837

18371838
void visitNominalTypeDecl(const NominalTypeDecl *nominal) {
1838-
checkGenericParams(nominal->getGenericParams(), nominal);
1839+
checkGenericParams(nominal, nominal);
18391840

18401841
llvm::for_each(nominal->getInherited(),
18411842
[&](TypeLoc nextInherited) {
@@ -1860,7 +1861,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18601861
}
18611862

18621863
void visitSubscriptDecl(SubscriptDecl *SD) {
1863-
checkGenericParams(SD->getGenericParams(), SD);
1864+
checkGenericParams(SD, SD);
18641865

18651866
for (auto &P : *SD->getIndices()) {
18661867
checkType(P->getInterfaceType(), P->getTypeRepr(), SD,
@@ -1871,7 +1872,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18711872
}
18721873

18731874
void visitAbstractFunctionDecl(AbstractFunctionDecl *fn) {
1874-
checkGenericParams(fn->getGenericParams(), fn);
1875+
checkGenericParams(fn, fn);
18751876

18761877
for (auto *P : *fn->getParameters())
18771878
checkType(P->getInterfaceType(), P->getTypeRepr(), fn,
@@ -2011,7 +2012,7 @@ static void checkExtensionGenericParamAccess(const ExtensionDecl *ED) {
20112012
}
20122013

20132014
AccessControlChecker().checkGenericParamAccess(
2014-
ED->getGenericParams(), ED, desiredAccessScope, userSpecifiedAccess);
2015+
ED, ED, desiredAccessScope, userSpecifiedAccess);
20152016
}
20162017

20172018
void swift::checkAccessControl(Decl *D) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ static void checkForEmptyOptionSet(const VarDecl *VD) {
547547

548548
/// Check the inheritance clauses generic parameters along with any
549549
/// requirements stored within the generic parameter list.
550-
static void checkGenericParams(GenericParamList *genericParams,
551-
DeclContext *owningDC) {
550+
static void checkGenericParams(GenericContext *ownerCtx) {
551+
const auto genericParams = ownerCtx->getGenericParams();
552552
if (!genericParams)
553553
return;
554554

@@ -558,7 +558,7 @@ static void checkGenericParams(GenericParamList *genericParams,
558558
}
559559

560560
// Force visitation of each of the requirements here.
561-
WhereClauseOwner(owningDC, genericParams)
561+
WhereClauseOwner(ownerCtx)
562562
.visitRequirements(TypeResolutionStage::Interface,
563563
[](Requirement, RequirementRepr *) { return false; });
564564
}
@@ -2631,7 +2631,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
26312631

26322632
if (!SD->isInvalid()) {
26332633
TypeChecker::checkReferencedGenericParams(SD);
2634-
checkGenericParams(SD->getGenericParams(), SD);
2634+
checkGenericParams(SD);
26352635
TypeChecker::checkProtocolSelfRequirements(SD);
26362636
}
26372637

@@ -2777,7 +2777,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27772777
// FIXME: Remove this once we clean up the mess involving raw values.
27782778
(void) ED->getInterfaceType();
27792779

2780-
checkGenericParams(ED->getGenericParams(), ED);
2780+
checkGenericParams(ED);
27812781

27822782
{
27832783
// Check for circular inheritance of the raw type.
@@ -2824,7 +2824,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
28242824
void visitStructDecl(StructDecl *SD) {
28252825
checkUnsupportedNestedType(SD);
28262826

2827-
checkGenericParams(SD->getGenericParams(), SD);
2827+
checkGenericParams(SD);
28282828

28292829
// Force lowering of stored properties.
28302830
(void) SD->getStoredProperties();
@@ -2949,7 +2949,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
29492949
// Force creation of the generic signature.
29502950
(void) CD->getGenericSignature();
29512951

2952-
checkGenericParams(CD->getGenericParams(), CD);
2952+
checkGenericParams(CD);
29532953

29542954
{
29552955
// Check for circular inheritance.
@@ -3234,7 +3234,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32343234
(void) FD->getOperatorDecl();
32353235

32363236
if (!FD->isInvalid()) {
3237-
checkGenericParams(FD->getGenericParams(), FD);
3237+
checkGenericParams(FD);
32383238
TypeChecker::checkReferencedGenericParams(FD);
32393239
TypeChecker::checkProtocolSelfRequirements(FD);
32403240
}
@@ -3436,7 +3436,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
34363436
}
34373437
}
34383438

3439-
checkGenericParams(ED->getGenericParams(), ED);
3439+
checkGenericParams(ED);
34403440

34413441
for (Decl *Member : ED->getMembers())
34423442
visit(Member);
@@ -3478,7 +3478,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
34783478
(void) CD->getInitKind();
34793479

34803480
if (!CD->isInvalid()) {
3481-
checkGenericParams(CD->getGenericParams(), CD);
3481+
checkGenericParams(CD);
34823482
TypeChecker::checkReferencedGenericParams(CD);
34833483
TypeChecker::checkProtocolSelfRequirements(CD);
34843484
}

0 commit comments

Comments
 (0)