Skip to content

Commit 3142803

Browse files
committed
AST: Remove runtime version from AvailabilitySpec.
Instead of canonicalizing platform versions during parsing and storing two versions, just canonicalize the parsed version on-demand when its requested.
1 parent 6daea78 commit 3142803

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,12 @@ BridgedSourceRange
654654
BridgedAvailabilitySpec_getVersionRange(BridgedAvailabilitySpec spec);
655655

656656
SWIFT_NAME("BridgedPlatformVersionConstraintAvailabilitySpec.createParsed(_:"
657-
"platform:platformLoc:version:runtimeVersion:versionRange:)")
657+
"platform:platformLoc:version:versionRange:)")
658658
BridgedPlatformVersionConstraintAvailabilitySpec
659659
BridgedPlatformVersionConstraintAvailabilitySpec_createParsed(
660660
BridgedASTContext cContext, BridgedPlatformKind cPlatform,
661661
BridgedSourceLoc cPlatformLoc, BridgedVersionTuple cVersion,
662-
BridgedVersionTuple cRuntimeVersion, BridgedSourceRange cVersionSrcRange);
662+
BridgedSourceRange cVersionSrcRange);
663663

664664
SWIFT_NAME("BridgedPlatformAgnosticVersionConstraintAvailabilitySpec."
665665
"createParsed(_:kind:nameLoc:version:versionRange:)")

include/swift/AST/AvailabilitySpec.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
7575

7676
llvm::VersionTuple Version;
7777

78-
// For macOS Big Sur, we canonicalize 10.16 to 11.0 for compile-time
79-
// checking since clang canonicalizes availability markup. However, to
80-
// support Beta versions of macOS Big Sur where the OS
81-
// reports 10.16 at run time, we need to compare against 10.16,
82-
//
83-
// This means for:
84-
//
85-
// if #available(macOS 10.16, *) { ... }
86-
//
87-
// we need to keep around both a canonical version for use in compile-time
88-
// checks and an uncanonicalized version for the version to actually codegen
89-
// with.
90-
llvm::VersionTuple RuntimeVersion;
91-
9278
SourceRange VersionSrcRange;
9379

9480
// Location of the macro expanded to create this spec.
@@ -98,13 +84,10 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
9884
PlatformVersionConstraintAvailabilitySpec(PlatformKind Platform,
9985
SourceLoc PlatformLoc,
10086
llvm::VersionTuple Version,
101-
llvm::VersionTuple RuntimeVersion,
10287
SourceRange VersionSrcRange)
103-
: AvailabilitySpec(AvailabilitySpecKind::PlatformVersionConstraint),
104-
Platform(Platform),
105-
PlatformLoc(PlatformLoc), Version(Version),
106-
RuntimeVersion(RuntimeVersion),
107-
VersionSrcRange(VersionSrcRange) {}
88+
: AvailabilitySpec(AvailabilitySpecKind::PlatformVersionConstraint),
89+
Platform(Platform), PlatformLoc(PlatformLoc), Version(Version),
90+
VersionSrcRange(VersionSrcRange) {}
10891

10992
/// The required platform.
11093
PlatformKind getPlatform() const { return Platform; }
@@ -116,13 +99,13 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
11699
bool isUnrecognizedPlatform() const { return Platform == PlatformKind::none; }
117100

118101
// The platform version to compare against.
119-
llvm::VersionTuple getVersion() const { return Version; }
102+
llvm::VersionTuple getVersion() const;
120103
SourceRange getVersionSrcRange() const { return VersionSrcRange; }
121104

122105
// The version to be used in codegen for version comparisons at run time.
123106
// This is required to support beta versions of macOS Big Sur that
124107
// report 10.16 at run time.
125-
llvm::VersionTuple getRuntimeVersion() const { return RuntimeVersion; }
108+
llvm::VersionTuple getRuntimeVersion() const;
126109

127110
SourceRange getSourceRange() const;
128111

lib/AST/AvailabilitySpec.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ SourceRange PlatformAgnosticVersionConstraintAvailabilitySpec::getSourceRange()
116116
return SourceRange(PlatformAgnosticNameLoc, VersionSrcRange.End);
117117
}
118118

119+
llvm::VersionTuple
120+
PlatformVersionConstraintAvailabilitySpec::getVersion() const {
121+
// For macOS Big Sur, we canonicalize 10.16 to 11.0 for compile-time
122+
// checking since clang canonicalizes availability markup. However, to
123+
// support Beta versions of macOS Big Sur where the OS
124+
// reports 10.16 at run time, we need to compare against 10.16,
125+
//
126+
// This means for:
127+
//
128+
// if #available(macOS 10.16, *) { ... }
129+
//
130+
// we need to store the uncanonicalized version for codegen and canonicalize
131+
// it as necessary for compile-time checks.
132+
return canonicalizePlatformVersion(Platform, Version);
133+
}
134+
135+
llvm::VersionTuple
136+
PlatformVersionConstraintAvailabilitySpec::getRuntimeVersion() const {
137+
return Version;
138+
}
139+
119140
void PlatformAgnosticVersionConstraintAvailabilitySpec::print(raw_ostream &OS,
120141
unsigned Indent) const {
121142
OS.indent(Indent) << '('

lib/AST/Bridging/AvailabilityBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ BridgedPlatformVersionConstraintAvailabilitySpec
135135
BridgedPlatformVersionConstraintAvailabilitySpec_createParsed(
136136
BridgedASTContext cContext, BridgedPlatformKind cPlatform,
137137
BridgedSourceLoc cPlatformLoc, BridgedVersionTuple cVersion,
138-
BridgedVersionTuple cRuntimeVersion, BridgedSourceRange cVersionSrcRange) {
138+
BridgedSourceRange cVersionSrcRange) {
139139
return new (cContext.unbridged()) PlatformVersionConstraintAvailabilitySpec(
140140
unbridge(cPlatform), cPlatformLoc.unbridged(), cVersion.unbridged(),
141-
cRuntimeVersion.unbridged(), cVersionSrcRange.unbridged());
141+
cVersionSrcRange.unbridged());
142142
}
143143

144144
BridgedPlatformAgnosticVersionConstraintAvailabilitySpec

lib/ASTGen/Sources/ASTGen/Availability.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ extension ASTGenVisitor {
334334
platform: platform,
335335
platformLoc: nameLoc,
336336
version: version.bridged,
337-
runtimeVersion: version.bridged,
338337
versionRange: versionRange
339338
)
340339
result.append(spec.asAvailabilitySpec)

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,11 +3787,7 @@ Parser::parsePlatformVersionConstraintSpec() {
37873787
// Register the platform name as a keyword token.
37883788
TokReceiver->registerTokenKindChange(PlatformLoc, tok::contextual_keyword);
37893789

3790-
// Keep the original version around for run-time checks to support
3791-
// macOS Big Sur betas that report 10.16 at
3792-
// run time.
3793-
llvm::VersionTuple RuntimeVersion = Version;
3794-
Version = canonicalizePlatformVersion(*Platform, Version);
3795-
return makeParserResult(new (Context) PlatformVersionConstraintAvailabilitySpec(
3796-
Platform.value(), PlatformLoc, Version, RuntimeVersion, VersionRange));
3790+
return makeParserResult(
3791+
new (Context) PlatformVersionConstraintAvailabilitySpec(
3792+
Platform.value(), PlatformLoc, Version, VersionRange));
37973793
}

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ struct RuntimeVersionCheck {
201201
Stmt *createEarlyReturnStmt(ASTContext &C) const {
202202
// platformSpec = "\(attr.platform) \(attr.introduced)"
203203
auto platformSpec = new (C) PlatformVersionConstraintAvailabilitySpec(
204-
Platform, SourceLoc(),
205-
Version, Version, SourceLoc()
206-
);
204+
Platform, SourceLoc(), Version, SourceLoc());
207205

208206
// otherSpec = "*"
209207
auto otherSpec = new (C) OtherPlatformAvailabilitySpec(SourceLoc());

0 commit comments

Comments
 (0)