Skip to content

Commit f769729

Browse files
committed
Parse: Backward compatibility for old spellings @_inlineable and @_versioned
Just parse these as @inlinable and @versioned, then emit a warning (Swift 4.2 and below) or error (Swift 5).
1 parent d3e3238 commit f769729

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,12 @@ ERROR(attr_only_on_parameters_parse, none,
13111311
ERROR(attr_access_expected_set,none,
13121312
"expected 'set' as subject of '%0' modifier", (StringRef))
13131313

1314+
// Attributes
1315+
ERROR(attr_renamed, none,
1316+
"'@%0' has been renamed to '@%1'", (StringRef, StringRef))
1317+
WARNING(attr_renamed_warning, none,
1318+
"'@%0' has been renamed to '@%1'", (StringRef, StringRef))
1319+
13141320
// availability
13151321
ERROR(attr_availability_platform,none,
13161322
"expected platform name or '*' for '%0' attribute", (StringRef))
@@ -1319,7 +1325,7 @@ ERROR(attr_availability_unavailable_deprecated,none,
13191325
"'deprecated'", (StringRef))
13201326

13211327
WARNING(attr_availability_unknown_platform,none,
1322-
"unknown platform '%0' for attribute '%1'", (StringRef, StringRef))
1328+
"unknown platform '%0' for attribute '%1'", (StringRef, StringRef))
13231329
ERROR(attr_availability_invalid_renamed,none,
13241330
"'renamed' argument of '%0' attribute must be an operator, identifier, "
13251331
"or full function name, optionally prefixed by a type name", (StringRef))
@@ -1334,9 +1340,6 @@ ERROR(attr_availability_expected_equal,none,
13341340
ERROR(attr_availability_expected_version,none,
13351341
"expected version number in '%0' attribute", (StringRef))
13361342

1337-
ERROR(attr_availability_renamed, none,
1338-
"@availability has been renamed to @available", ())
1339-
13401343
// autoclosure
13411344
ERROR(attr_autoclosure_expected_r_paren,PointsToFirstBadToken,
13421345
"expected ')' in @autoclosure", ())

lib/Parse/ParseDecl.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,9 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
13541354
if (DuplicateAttribute) {
13551355
diagnose(Loc, diag::duplicate_attribute, DeclAttribute::isDeclModifier(DK))
13561356
.highlight(AttrRange);
1357-
diagnose(DuplicateAttribute->getLocation(), diag::previous_attribute, DeclAttribute::isDeclModifier(DK))
1357+
diagnose(DuplicateAttribute->getLocation(),
1358+
diag::previous_attribute,
1359+
DeclAttribute::isDeclModifier(DK))
13581360
.highlight(DuplicateAttribute->getRange());
13591361
}
13601362

@@ -1464,14 +1466,35 @@ bool Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc) {
14641466
// over to the alternate parsing path.
14651467
DeclAttrKind DK = DeclAttribute::getAttrKindFromString(Tok.getText());
14661468

1467-
if (DK == DAK_Count && Tok.getText() == "availability") {
1468-
// We renamed @availability to @available, so if we see the former,
1469-
// treat it as the latter and emit a Fix-It.
1470-
DK = DAK_Available;
1471-
diagnose(Tok, diag::attr_availability_renamed)
1472-
.fixItReplace(Tok.getLoc(), "available");
1473-
}
1469+
auto checkRenamedAttr = [&](StringRef oldName, StringRef newName,
1470+
DeclAttrKind kind, bool warning) {
1471+
if (DK == DAK_Count && Tok.getText() == oldName) {
1472+
// We renamed @availability to @available, so if we see the former,
1473+
// treat it as the latter and emit a Fix-It.
1474+
DK = kind;
1475+
if (warning) {
1476+
diagnose(Tok, diag::attr_renamed_warning, oldName, newName)
1477+
.fixItReplace(Tok.getLoc(), newName);
1478+
} else {
1479+
diagnose(Tok, diag::attr_renamed, oldName, newName)
1480+
.fixItReplace(Tok.getLoc(), newName);
1481+
}
1482+
}
1483+
};
1484+
1485+
// FIXME: This renaming happened before Swift 3, we can probably remove
1486+
// the specific fallback path at some point.
1487+
checkRenamedAttr("availability", "available", DAK_Available, false);
14741488

1489+
// In Swift 5 and above, these become hard errors. Otherwise, emit a
1490+
// warning for compatibility.
1491+
if (!Context.isSwiftVersionAtLeast(5)) {
1492+
checkRenamedAttr("_versioned", "usableFromInline", DAK_UsableFromInline, true);
1493+
checkRenamedAttr("_inlineable", "inlinable", DAK_Inlinable, true);
1494+
} else {
1495+
checkRenamedAttr("_versioned", "usableFromInline", DAK_UsableFromInline, false);
1496+
checkRenamedAttr("_inlineable", "inlinable", DAK_Inlinable, false);
1497+
}
14751498

14761499
if (DK == DAK_Count && Tok.getText() == "warn_unused_result") {
14771500
// The behavior created by @warn_unused_result is now the default. Emit a
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 3
2+
// RUN: %target-typecheck-verify-swift -swift-version 4
3+
// RUN: %target-typecheck-verify-swift -swift-version 4.2
4+
5+
// Until -swift-version 5 mode, the old spelling only produces a warning.
6+
7+
@_inlineable public func oldInlinableFunction() {}
8+
// expected-warning@-1 {{'@_inlineable' has been renamed to '@inlinable'}}{{2-13=inlinable}}
9+
10+
@_versioned func oldVersionedFunction() {}
11+
// expected-warning@-1 {{'@_versioned' has been renamed to '@usableFromInline'}}{{2-12=usableFromInline}}

test/attr/attr_availability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func shortFormWithWildcardInMiddle() {}
228228
@available(iOS 8.0, OSX 10.10.3) // expected-error {{must handle potential future platforms with '*'}} {{32-32=, *}}
229229
func shortFormMissingWildcard() {}
230230
231-
@availability(OSX, introduced: 10.10) // expected-error {{@availability has been renamed to @available}} {{2-14=available}}
231+
@availability(OSX, introduced: 10.10) // expected-error {{'@availability' has been renamed to '@available'}} {{2-14=available}}
232232
func someFuncUsingOldAttribute() { }
233233
234234
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 5
2+
3+
@_inlineable public func oldInlinableFunction() {}
4+
// expected-error@-1 {{'@_inlineable' has been renamed to '@inlinable'}}{{2-13=inlinable}}
5+
6+
@_versioned func oldVersionedFunction() {}
7+
// expected-error@-1 {{'@_versioned' has been renamed to '@usableFromInline'}}{{2-12=usableFromInline}}

0 commit comments

Comments
 (0)