Skip to content

Commit 2fc3967

Browse files
authored
Merge pull request #17091 from slavapestov/inlinable-fixes-4.2
Minor @inlinable improvements [4.2]
2 parents ebf25d5 + cef38cf commit 2fc3967

File tree

8 files changed

+62
-21
lines changed

8 files changed

+62
-21
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ class ASTContext {
894894
/// This is usually the check you want; for example, when introducing
895895
/// a new language feature which is only visible in Swift 5, you would
896896
/// check for isSwiftVersionAtLeast(5).
897-
bool isSwiftVersionAtLeast(unsigned major) const {
898-
return LangOpts.isSwiftVersionAtLeast(major);
897+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
898+
return LangOpts.isSwiftVersionAtLeast(major, minor);
899899
}
900900

901901
private:

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ DECL_ATTR(_cdecl, CDecl,
295295

296296
SIMPLE_DECL_ATTR(usableFromInline, UsableFromInline,
297297
OnFunc | OnVar | OnSubscript | OnConstructor |
298-
OnDestructor | OnStruct | OnEnum | OnClass |
298+
OnDestructor | OnStruct | OnEnum | OnClass | OnTypeAlias |
299299
OnProtocol | LongAttribute | UserInaccessible,
300300
64)
301301

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ namespace swift {
340340
/// This is usually the check you want; for example, when introducing
341341
/// a new language feature which is only visible in Swift 5, you would
342342
/// check for isSwiftVersionAtLeast(5).
343-
bool isSwiftVersionAtLeast(unsigned major) const {
344-
return EffectiveLanguageVersion.isVersionAtLeast(major);
343+
bool isSwiftVersionAtLeast(unsigned major, unsigned minor = 0) const {
344+
return EffectiveLanguageVersion.isVersionAtLeast(major, minor);
345345
}
346346

347347
/// Returns true if the given platform condition argument represents

include/swift/Basic/Version.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ class Version {
112112

113113
/// Whether this version is greater than or equal to the given major version
114114
/// number.
115-
bool isVersionAtLeast(unsigned major) const {
116-
return !empty() && Components[0] >= major;
115+
bool isVersionAtLeast(unsigned major, unsigned minor = 0) const {
116+
switch (size()) {
117+
case 0:
118+
return false;
119+
case 1:
120+
return ((Components[0] == major && 0 == minor) ||
121+
(Components[0] > major));
122+
default:
123+
return ((Components[0] == major && Components[1] >= minor) ||
124+
(Components[0] > major));
125+
}
117126
}
118127

119128
/// Return this Version struct with minor and sub-minor components stripped

lib/Parse/ParseDecl.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,15 +1466,17 @@ bool Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc) {
14661466
// over to the alternate parsing path.
14671467
DeclAttrKind DK = DeclAttribute::getAttrKindFromString(Tok.getText());
14681468

1469-
auto checkInvalidAttrName = [&](StringRef invalidName, StringRef correctName,
1470-
DeclAttrKind kind, Diag<StringRef, StringRef> diag) {
1469+
auto checkInvalidAttrName = [&](StringRef invalidName,
1470+
StringRef correctName,
1471+
DeclAttrKind kind,
1472+
Optional<Diag<StringRef, StringRef>> diag = None) {
14711473
if (DK == DAK_Count && Tok.getText() == invalidName) {
1472-
// We renamed @availability to @available, so if we see the former,
1473-
// treat it as the latter and emit a Fix-It.
14741474
DK = kind;
1475-
1476-
diagnose(Tok, diag, invalidName, correctName)
1475+
1476+
if (diag) {
1477+
diagnose(Tok, *diag, invalidName, correctName)
14771478
.fixItReplace(Tok.getLoc(), correctName);
1479+
}
14781480
}
14791481
};
14801482

@@ -1485,14 +1487,17 @@ bool Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc) {
14851487
// Check if attr is inlineable, and suggest inlinable instead
14861488
checkInvalidAttrName("inlineable", "inlinable", DAK_Inlinable, diag::attr_name_close_match);
14871489

1488-
// In Swift 5 and above, these become hard errors. Otherwise, emit a
1489-
// warning for compatibility.
1490-
if (!Context.isSwiftVersionAtLeast(5)) {
1490+
// In Swift 5 and above, these become hard errors. In Swift 4.2, emit a
1491+
// warning for compatibility. Otherwise, don't diagnose at all.
1492+
if (Context.isSwiftVersionAtLeast(5)) {
1493+
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed);
1494+
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed);
1495+
} else if (Context.isSwiftVersionAtLeast(4, 2)) {
14911496
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed_warning);
14921497
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed_warning);
14931498
} else {
1494-
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline, diag::attr_renamed);
1495-
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable, diag::attr_renamed);
1499+
checkInvalidAttrName("_versioned", "usableFromInline", DAK_UsableFromInline);
1500+
checkInvalidAttrName("_inlineable", "inlinable", DAK_Inlinable);
14961501
}
14971502

14981503
if (DK == DAK_Count && Tok.getText() == "warn_unused_result") {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 3
2+
// RUN: %target-typecheck-verify-swift -swift-version 4
3+
4+
// In -swift-version 4 mode, the old spelling is silently parsed as the new spelling.
5+
6+
@_inlineable public func oldInlinableFunction() {}
7+
@_versioned func oldVersionedFunction() {}

test/Compatibility/attr_inlinable_old_spelling.swift renamed to test/Compatibility/attr_inlinable_old_spelling_42.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 3
2-
// RUN: %target-typecheck-verify-swift -swift-version 4
31
// RUN: %target-typecheck-verify-swift -swift-version 4.2
42

5-
// Until -swift-version 5 mode, the old spelling only produces a warning.
3+
// In -swift-version 4.2 mode, the old spelling produces a warning.
64

75
@_inlineable public func oldInlinableFunction() {}
86
// expected-warning@-1 {{'@_inlineable' has been renamed to '@inlinable'}}{{2-13=inlinable}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// None of this is enforced for now, but make sure we don't crash or
4+
// do anything stupid when a typealias is annotated with @usableFromInline.
5+
6+
private typealias PrivateAlias = Int
7+
8+
internal typealias InternalAlias = Int
9+
10+
@usableFromInline typealias UsableFromInlineAlias = Int
11+
12+
public typealias PublicAlias = Int
13+
14+
@inlinable public func f() {
15+
_ = PrivateAlias.self
16+
17+
_ = InternalAlias.self
18+
19+
_ = UsableFromInlineAlias.self
20+
21+
_ = PublicAlias.self
22+
}

0 commit comments

Comments
 (0)