Skip to content

Commit 9cbe177

Browse files
authored
Merge pull request swiftlang#78938 from tshortli/allow-overrides-as-available-as-their-context-6.1
[6.1] Sema: Always allow method overrides to be as available as the context
2 parents e108415 + 29aaec8 commit 9cbe177

5 files changed

+235
-8
lines changed

lib/Sema/TypeCheckAvailability.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,12 @@ TypeChecker::availabilityAtLocation(SourceLoc loc, const DeclContext *DC,
15371537
return availability;
15381538
}
15391539

1540+
AvailabilityContext
1541+
TypeChecker::availabilityForDeclSignature(const Decl *decl) {
1542+
return TypeChecker::availabilityAtLocation(decl->getLoc(),
1543+
decl->getInnermostDeclContext());
1544+
}
1545+
15401546
AvailabilityRange TypeChecker::overApproximateAvailabilityAtLocation(
15411547
SourceLoc loc, const DeclContext *DC,
15421548
const AvailabilityScope **MostRefined) {

lib/Sema/TypeCheckDeclOverride.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1816,10 +1816,10 @@ static bool isAvailabilitySafeForOverride(ValueDecl *override,
18161816

18171817
// Allow overrides that are not as available as the base decl as long as the
18181818
// override is as available as its context.
1819-
auto overrideTypeAvailability = AvailabilityInference::availableRange(
1819+
auto availabilityContext = TypeChecker::availabilityForDeclSignature(
18201820
override->getDeclContext()->getSelfNominalTypeDecl());
18211821

1822-
return overrideTypeAvailability.isContainedIn(overrideInfo);
1822+
return availabilityContext.getPlatformRange().isContainedIn(overrideInfo);
18231823
}
18241824

18251825
/// Returns true if a diagnostic about an accessor being less available

lib/Sema/TypeChecker.h

+3
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,9 @@ AvailabilityContext
10051005
availabilityAtLocation(SourceLoc loc, const DeclContext *DC,
10061006
const AvailabilityScope **MostRefined = nullptr);
10071007

1008+
/// Returns the availability context of the signature of the given declaration.
1009+
AvailabilityContext availabilityForDeclSignature(const Decl *decl);
1010+
10081011
/// Returns an over-approximation of the range of operating system versions
10091012
/// that could the passed-in location could be executing upon for
10101013
/// the target platform. If MostRefined != nullptr, set to the most-refined

test/Sema/availability_versions.swift

+57-6
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protocol BaseProto {
238238

239239
var property: A { get set } // expected-note {{overridden declaration is here}}
240240

241-
@available(OSX 10.51, *)
241+
@available(OSX 51, *)
242242
var newProperty: A { get set } // expected-note {{overridden declaration is here}}
243243

244244
func method() // expected-note {{overridden declaration is here}}
@@ -247,24 +247,24 @@ protocol BaseProto {
247247
protocol RefinesBaseProto_AsAvailableOverrides: BaseProto {
248248
var property: A { get set }
249249

250-
@available(OSX 10.51, *)
250+
@available(OSX 51, *)
251251
var newProperty: A { get set }
252252

253253
func method()
254254
}
255255

256256
protocol RefinesBaseProto_LessAvailableOverrides: BaseProto {
257-
@available(OSX 10.52, *)
257+
@available(OSX 52, *)
258258
var property: A { get set } // expected-error {{overriding 'property' must be as available as declaration it overrides}}
259259

260-
@available(OSX 10.52, *)
260+
@available(OSX 52, *)
261261
var newProperty: A { get set } // expected-error {{overriding 'newProperty' must be as available as declaration it overrides}}
262262

263-
@available(OSX 10.52, *)
263+
@available(OSX 52, *)
264264
func method() // expected-error {{overriding 'method' must be as available as declaration it overrides}}
265265
}
266266

267-
@available(OSX 10.52, *)
267+
@available(OSX 52, *)
268268
protocol RefinesBaseProto_LessAvailable: BaseProto {
269269
var property: A { get set }
270270
var newProperty: A { get set }
@@ -892,6 +892,57 @@ class SubWithLimitedMemberAvailability : SuperWithAlwaysAvailableMembers {
892892
}
893893
}
894894

895+
extension ClassAvailableOn10_9 {
896+
class NestedSubWithLimitedMemberAvailability: SuperWithAlwaysAvailableMembers {
897+
@available(OSX, introduced: 10.9)
898+
override func shouldAlwaysBeAvailableMethod() {}
899+
900+
@available(OSX, introduced: 10.9)
901+
override var shouldAlwaysBeAvailableProperty: Int {
902+
get { return 10 }
903+
set(newVal) {}
904+
}
905+
906+
override var setterShouldAlwaysBeAvailableProperty: Int {
907+
get { return 9 }
908+
@available(OSX, introduced: 10.9)
909+
set(newVal) {}
910+
}
911+
912+
override var getterShouldAlwaysBeAvailableProperty: Int {
913+
@available(OSX, introduced: 10.9)
914+
get { return 9 }
915+
set(newVal) {}
916+
}
917+
}
918+
}
919+
920+
@available(OSX, introduced: 51)
921+
extension ClassAvailableOn51 {
922+
class NestedSubWithLimitedMemberAvailability: SuperWithAlwaysAvailableMembers {
923+
@available(OSX, introduced: 51)
924+
override func shouldAlwaysBeAvailableMethod() {}
925+
926+
@available(OSX, introduced: 51)
927+
override var shouldAlwaysBeAvailableProperty: Int {
928+
get { return 10 }
929+
set(newVal) {}
930+
}
931+
932+
override var setterShouldAlwaysBeAvailableProperty: Int {
933+
get { return 9 }
934+
@available(OSX, introduced: 51)
935+
set(newVal) {}
936+
}
937+
938+
override var getterShouldAlwaysBeAvailableProperty: Int {
939+
@available(OSX, introduced: 51)
940+
get { return 9 }
941+
set(newVal) {}
942+
}
943+
}
944+
}
945+
895946
@available(OSX, introduced: 51)
896947
class SubWithLimitedAvailablility : SuperWithAlwaysAvailableMembers {
897948
override func shouldAlwaysBeAvailableMethod() {}

test/attr/attr_inlinable_available.swift

+167
Original file line numberDiff line numberDiff line change
@@ -1690,3 +1690,170 @@ public enum UnavailableEnumWithClasses {
16901690
public class InheritsAfterDeploymentTarget: AfterDeploymentTargetClass {}
16911691
public class InheritsUnavailable: UnavailableClass {}
16921692
}
1693+
1694+
// MARK: - Overrides
1695+
1696+
public class Base {
1697+
@available(macOS 10.9, *)
1698+
public func beforeInliningTargetMethod() {} // expected-note 3 {{overridden declaration is here}}
1699+
1700+
@available(macOS 10.10, *)
1701+
public func atInliningTargetMethod() {}// expected-note 3 {{overridden declaration is here}}
1702+
1703+
@available(macOS 10.14.5, *)
1704+
public func betweenTargetsMethod() {}// expected-note 3 {{overridden declaration is here}}
1705+
1706+
@available(macOS 10.15, *)
1707+
public func atDeploymentTargetMethod() {}// expected-note 2 {{overridden declaration is here}}
1708+
1709+
@available(macOS 11, *)
1710+
public func afterDeploymentTargetMethod() {}// expected-note {{overridden declaration is here}}
1711+
}
1712+
1713+
public class DerivedNoAvailable: Base {
1714+
public override func beforeInliningTargetMethod() {}
1715+
public override func atInliningTargetMethod() {}
1716+
public override func betweenTargetsMethod() {}
1717+
public override func atDeploymentTargetMethod() {}
1718+
public override func afterDeploymentTargetMethod() {}
1719+
}
1720+
1721+
@available(macOS 10.9, *)
1722+
public class DerivedBeforeInliningTarget: Base {
1723+
@available(macOS 10.9, *)
1724+
public override func beforeInliningTargetMethod() {}
1725+
@available(macOS 10.9, *)
1726+
public override func atInliningTargetMethod() {}
1727+
@available(macOS 10.9, *)
1728+
public override func betweenTargetsMethod() {}
1729+
@available(macOS 10.9, *)
1730+
public override func atDeploymentTargetMethod() {}
1731+
@available(macOS 10.9, *)
1732+
public override func afterDeploymentTargetMethod() {}
1733+
}
1734+
1735+
@available(macOS 10.10, *)
1736+
public class DerivedAtInliningTarget: Base {
1737+
@available(macOS 10.10, *)
1738+
public override func beforeInliningTargetMethod() {}
1739+
@available(macOS 10.10, *)
1740+
public override func atInliningTargetMethod() {}
1741+
@available(macOS 10.10, *)
1742+
public override func betweenTargetsMethod() {}
1743+
@available(macOS 10.10, *)
1744+
public override func atDeploymentTargetMethod() {}
1745+
@available(macOS 10.10, *)
1746+
public override func afterDeploymentTargetMethod() {}
1747+
}
1748+
1749+
@available(macOS 10.14.5, *)
1750+
public class DerivedBetweenTargets: Base {
1751+
@available(macOS 10.14.5, *)
1752+
public override func beforeInliningTargetMethod() {}
1753+
@available(macOS 10.14.5, *)
1754+
public override func atInliningTargetMethod() {}
1755+
@available(macOS 10.14.5, *)
1756+
public override func betweenTargetsMethod() {}
1757+
@available(macOS 10.14.5, *)
1758+
public override func atDeploymentTargetMethod() {}
1759+
@available(macOS 10.14.5, *)
1760+
public override func afterDeploymentTargetMethod() {}
1761+
}
1762+
1763+
@available(macOS 10.15, *)
1764+
public class DerivedAtDeploymentTarget: Base {
1765+
@available(macOS 10.15, *)
1766+
public override func beforeInliningTargetMethod() {}
1767+
@available(macOS 10.15, *)
1768+
public override func atInliningTargetMethod() {}
1769+
@available(macOS 10.15, *)
1770+
public override func betweenTargetsMethod() {}
1771+
@available(macOS 10.15, *)
1772+
public override func atDeploymentTargetMethod() {}
1773+
@available(macOS 10.15, *)
1774+
public override func afterDeploymentTargetMethod() {}
1775+
}
1776+
1777+
@available(macOS 11, *)
1778+
public class DerivedAfterDeploymentTarget: Base {
1779+
@available(macOS 11, *)
1780+
public override func beforeInliningTargetMethod() {}
1781+
@available(macOS 11, *)
1782+
public override func atInliningTargetMethod() {}
1783+
@available(macOS 11, *)
1784+
public override func betweenTargetsMethod() {}
1785+
@available(macOS 11, *)
1786+
public override func atDeploymentTargetMethod() {}
1787+
@available(macOS 11, *)
1788+
public override func afterDeploymentTargetMethod() {}
1789+
}
1790+
1791+
public class DerivedAtDeploymentTargetOverrides: Base {
1792+
@available(macOS 10.15, *)
1793+
public override func beforeInliningTargetMethod() {} // expected-error {{overriding 'beforeInliningTargetMethod' must be as available as declaration it overrides}}
1794+
1795+
@available(macOS 10.15, *)
1796+
public override func atInliningTargetMethod() {} // expected-error {{overriding 'atInliningTargetMethod' must be as available as declaration it overrides}}
1797+
1798+
@available(macOS 10.15, *)
1799+
public override func betweenTargetsMethod() {} // expected-error {{overriding 'betweenTargetsMethod' must be as available as declaration it overrides}}
1800+
1801+
@available(macOS 10.15, *)
1802+
public override func atDeploymentTargetMethod() {}
1803+
1804+
@available(macOS 10.15, *)
1805+
public override func afterDeploymentTargetMethod() {}
1806+
}
1807+
1808+
public class DerivedFutureOverrides: Base {
1809+
@available(macOS 12, *)
1810+
public override func beforeInliningTargetMethod() {} // expected-error {{overriding 'beforeInliningTargetMethod' must be as available as declaration it overrides}}
1811+
1812+
@available(macOS 12, *)
1813+
public override func atInliningTargetMethod() {} // expected-error {{overriding 'atInliningTargetMethod' must be as available as declaration it overrides}}
1814+
1815+
@available(macOS 12, *)
1816+
public override func betweenTargetsMethod() {} // expected-error {{overriding 'betweenTargetsMethod' must be as available as declaration it overrides}}
1817+
1818+
@available(macOS 12, *)
1819+
public override func atDeploymentTargetMethod() {} // expected-error {{overriding 'atDeploymentTargetMethod' must be as available as declaration it overrides}}
1820+
1821+
@available(macOS 12, *)
1822+
public override func afterDeploymentTargetMethod() {} // expected-error {{overriding 'afterDeploymentTargetMethod' must be as available as declaration it overrides}}
1823+
}
1824+
1825+
extension AtDeploymentTarget {
1826+
public class DerivedAtDeploymentTargetOverrides: Base {
1827+
@available(macOS 10.15, *)
1828+
public override func beforeInliningTargetMethod() {}
1829+
1830+
@available(macOS 10.15, *)
1831+
public override func atInliningTargetMethod() {}
1832+
1833+
@available(macOS 10.15, *)
1834+
public override func betweenTargetsMethod() {}
1835+
1836+
@available(macOS 10.15, *)
1837+
public override func atDeploymentTargetMethod() {}
1838+
1839+
@available(macOS 10.15, *)
1840+
public override func afterDeploymentTargetMethod() {}
1841+
}
1842+
1843+
public class DerivedAfterDeploymentTargetOverrides: Base {
1844+
@available(macOS 11, *)
1845+
public override func beforeInliningTargetMethod() {} // expected-error {{overriding 'beforeInliningTargetMethod' must be as available as declaration it overrides}}
1846+
1847+
@available(macOS 11, *)
1848+
public override func atInliningTargetMethod() {} // expected-error {{overriding 'atInliningTargetMethod' must be as available as declaration it overrides}}
1849+
1850+
@available(macOS 11, *)
1851+
public override func betweenTargetsMethod() {} // expected-error {{overriding 'betweenTargetsMethod' must be as available as declaration it overrides}}
1852+
1853+
@available(macOS 11, *)
1854+
public override func atDeploymentTargetMethod() {} // expected-error {{overriding 'atDeploymentTargetMethod' must be as available as declaration it overrides}}
1855+
1856+
@available(macOS 11, *)
1857+
public override func afterDeploymentTargetMethod() {}
1858+
}
1859+
}

0 commit comments

Comments
 (0)