Skip to content

Commit 1843d49

Browse files
authored
Merge pull request #41626 from nkcsgexi/experiment-final-in-isPotentiallyOverridable
2 parents e1ac443 + 1bc7039 commit 1843d49

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,12 +1003,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
10031003
/// deployment target.
10041004
bool isWeakImported(ModuleDecl *fromModule) const;
10051005

1006-
/// Returns true if the nature of this declaration allows overrides.
1007-
/// Note that this does not consider whether it is final or whether
1008-
/// the class it's on is final.
1006+
/// Returns true if the nature of this declaration allows overrides syntactically.
10091007
///
10101008
/// If this returns true, the decl can be safely casted to ValueDecl.
1011-
bool isPotentiallyOverridable() const;
1009+
bool isSyntacticallyOverridable() const;
10121010

10131011
/// Retrieve the global actor attribute that applies to this declaration,
10141012
/// if any.
@@ -7935,13 +7933,16 @@ inline unsigned ValueDecl::getNumCurryLevels() const {
79357933
return curryLevels;
79367934
}
79377935

7938-
inline bool Decl::isPotentiallyOverridable() const {
7936+
inline bool Decl::isSyntacticallyOverridable() const {
79397937
if (isa<VarDecl>(this) ||
79407938
isa<SubscriptDecl>(this) ||
79417939
isa<FuncDecl>(this) ||
79427940
isa<DestructorDecl>(this)) {
7941+
if (static_cast<const ValueDecl*>(this)->isFinal()) {
7942+
return false;
7943+
}
79437944
auto classDecl = getDeclContext()->getSelfClassDecl();
7944-
return classDecl && !classDecl->isActor();
7945+
return classDecl && !classDecl->isActor() && !classDecl->isFinal();
79457946
} else {
79467947
return false;
79477948
}

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ class Verifier : public ASTWalker {
23142314

23152315
if (VD->hasAccess()) {
23162316
if (VD->getFormalAccess() == AccessLevel::Open) {
2317-
if (!isa<ClassDecl>(VD) && !VD->isPotentiallyOverridable()) {
2317+
if (!isa<ClassDecl>(VD) && !VD->isSyntacticallyOverridable()) {
23182318
Out << "decl cannot be 'open'\n";
23192319
VD->dump(Out);
23202320
abort();

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,7 +3439,7 @@ static AccessLevel getMaximallyOpenAccessFor(const ValueDecl *decl) {
34393439

34403440
// Non-final overridable class members are considered open to
34413441
// @testable importers.
3442-
} else if (decl->isPotentiallyOverridable()) {
3442+
} else if (decl->isSyntacticallyOverridable()) {
34433443
if (!cast<ValueDecl>(decl)->isSemanticallyFinal())
34443444
return AccessLevel::Open;
34453445
}
@@ -3793,7 +3793,7 @@ void ValueDecl::copyFormalAccessFrom(const ValueDecl *source,
37933793
access = AccessLevel::FilePrivate;
37943794

37953795
// Only certain declarations can be 'open'.
3796-
if (access == AccessLevel::Open && !isPotentiallyOverridable()) {
3796+
if (access == AccessLevel::Open && !isSyntacticallyOverridable()) {
37973797
assert(!isa<ClassDecl>(this) &&
37983798
"copying 'open' onto a class has complications");
37993799
access = AccessLevel::Public;

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ void AttributeChecker::visitAccessControlAttr(AccessControlAttr *attr) {
940940
if (attr->getAccess() == AccessLevel::Open) {
941941
auto classDecl = dyn_cast<ClassDecl>(D);
942942
if (!(classDecl && !classDecl->isActor()) &&
943-
!D->isPotentiallyOverridable() &&
943+
!D->isSyntacticallyOverridable() &&
944944
!attr->isInvalid()) {
945945
diagnose(attr->getLocation(), diag::access_control_open_bad_decl)
946946
.fixItReplace(attr->getRange(), "public");
@@ -3496,7 +3496,7 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
34963496
continue;
34973497

34983498
// Back deployment isn't compatible with dynamic dispatch.
3499-
if (VD->isPotentiallyOverridable() && !VD->isFinal()) {
3499+
if (VD->isSyntacticallyOverridable()) {
35003500
diagnose(Attr->getLocation(), diag::attr_incompatible_with_non_final,
35013501
Attr, D->getDescriptiveKind());
35023502
continue;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2543,7 +2543,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25432543

25442544
for (auto *member : superclass->getMembers()) {
25452545
if (auto *vd = dyn_cast<ValueDecl>(member)) {
2546-
if (vd->isPotentiallyOverridable()) {
2546+
if (vd->isSyntacticallyOverridable()) {
25472547
(void) vd->isObjC();
25482548
}
25492549
}

0 commit comments

Comments
 (0)