Skip to content

SIL: Final methods need a subclass scope if they override something [5.1] #27874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/SIL/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,12 +984,13 @@ SubclassScope SILDeclRef::getSubclassScope() const {
if (isDefaultArgGenerator())
return SubclassScope::NotApplicable;

// Only non-final methods in non-final classes go in the vtable.
// Only methods in non-final classes go in the vtable.
auto *classType = context->getSelfClassDecl();
if (!classType || classType->isFinal())
return SubclassScope::NotApplicable;

if (decl->isFinal())
// Final methods only go in the vtable if they override something.
if (decl->isFinal() && !decl->getOverriddenDecl())
return SubclassScope::NotApplicable;

assert(decl->getEffectiveAccess() <= classType->getEffectiveAccess() &&
Expand Down
31 changes: 29 additions & 2 deletions test/IRGen/method_linkage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ class Base {
// RESILIENT: define hidden swiftcc void @"$s14method_linkage4Base{{.*}}5other0
fileprivate func other() {
}

// CHECK: define hidden swiftcc void @"$s14method_linkage4BaseC4prop{{.*}}LLytvg
// RESILIENT: define hidden swiftcc void @"$s14method_linkage4BaseC4prop{{.*}}LLytvg
fileprivate var prop: () {
return ()
}
}
class Derived : Base {
// CHECK: define internal swiftcc void @"$s14method_linkage7Derived{{.*}}3foo0
// RESILIENT: define internal swiftcc void @"$s14method_linkage7Derived{{.*}}3foo0
// CHECK: define hidden swiftcc void @"$s14method_linkage7Derived{{.*}}3foo0
// RESILIENT: define hidden swiftcc void @"$s14method_linkage7Derived{{.*}}3foo0
fileprivate final override func foo() {
}

// CHECK: define hidden swiftcc void @"$s14method_linkage7DerivedC4prop{{.*}}LLytvg
// RESILIENT: define hidden swiftcc void @"$s14method_linkage7DerivedC4prop{{.*}}LLytvg
fileprivate final override var prop: () {
return ()
}
}

extension Base {
Expand Down Expand Up @@ -92,6 +104,20 @@ open class OpenClass {
// RESILIENT: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s14method_linkage9OpenClassC5pquuxyyF"
public final func pquux() {
}

// CHECK: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s14method_linkage9OpenClassC4prop{{.*}}LLytvg
// RESILIENT: define hidden swiftcc void @"$s14method_linkage9OpenClassC4prop{{.*}}LLytvg
fileprivate var prop: () {
return ()
}
}

open class OpenSubclass : OpenClass {
// CHECK: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s14method_linkage12OpenSubclassC4prop{{.*}}LLytvg
// RESILIENT: define hidden swiftcc void @"$s14method_linkage12OpenSubclassC4prop{{.*}}LLytvg
fileprivate final override var prop: () {
return ()
}
}

// Just in case anyone wants to delete unused methods...
Expand All @@ -100,4 +126,5 @@ func callit(b: Base) {
b.bar()
b.other()
b.extfunc()
_ = b.prop
}