Skip to content

Commit 938e54c

Browse files
Merge pull request #76128 from ian-twilightcoder/unknown-platform-attr_6.0.0
[cherry-pick release/6.0.0][Sema] Allow unrecognized platforms in @backDeployed and @_originallyDefinedIn
2 parents 88a347d + e6113b3 commit 938e54c

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

include/swift/Parse/Parser.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,12 +2096,15 @@ class Parser {
20962096

20972097
using PlatformAndVersion = std::pair<PlatformKind, llvm::VersionTuple>;
20982098

2099-
/// Parse a platform and version tuple (e.g. "macOS 12.0") and append it to the
2100-
/// given vector. Wildcards ('*') parse successfully but are ignored. Assumes
2101-
/// that the tuples are part of a comma separated list ending with a trailing
2102-
/// ')'.
2103-
ParserStatus parsePlatformVersionInList(StringRef AttrName,
2104-
llvm::SmallVector<PlatformAndVersion, 4> &PlatformAndVersions);
2099+
/// Parse a platform and version tuple (e.g. "macOS 12.0") and append it to
2100+
/// the given vector. Wildcards ('*') parse successfully but are ignored.
2101+
/// Unrecognized platform names also parse successfully but are ignored.
2102+
/// Assumes that the tuples are part of a comma separated list ending with a
2103+
/// trailing ')'.
2104+
ParserStatus parsePlatformVersionInList(
2105+
StringRef AttrName,
2106+
llvm::SmallVector<PlatformAndVersion, 4> & PlatformAndVersions,
2107+
bool &ParsedUnrecognizedPlatformName);
21052108

21062109
//===--------------------------------------------------------------------===//
21072110
// Code completion second pass.

lib/Parse/ParseDecl.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,8 @@ void Parser::parseAllAvailabilityMacroArguments() {
21882188
}
21892189

21902190
ParserStatus Parser::parsePlatformVersionInList(StringRef AttrName,
2191-
llvm::SmallVector<PlatformAndVersion, 4> &PlatformAndVersions) {
2191+
llvm::SmallVector<PlatformAndVersion, 4> &PlatformAndVersions,
2192+
bool &ParsedUnrecognizedPlatformName) {
21922193
// Check for availability macros first.
21932194
if (peekAvailabilityMacroName()) {
21942195
SmallVector<AvailabilitySpec *, 4> Specs;
@@ -2225,6 +2226,7 @@ ParserStatus Parser::parsePlatformVersionInList(StringRef AttrName,
22252226
// Parse the platform name.
22262227
StringRef platformText = Tok.getText();
22272228
auto MaybePlatform = platformFromString(platformText);
2229+
ParsedUnrecognizedPlatformName = ParsedUnrecognizedPlatformName || !MaybePlatform.has_value();
22282230
SourceLoc PlatformLoc = Tok.getLoc();
22292231
consumeToken();
22302232

@@ -2287,6 +2289,7 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
22872289
SourceLoc RightLoc;
22882290
ParserStatus Status;
22892291
bool SuppressLaterDiags = false;
2292+
bool ParsedUnrecognizedPlatformName = false;
22902293
llvm::SmallVector<PlatformAndVersion, 4> PlatformAndVersions;
22912294

22922295
{
@@ -2305,12 +2308,12 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
23052308
if (!Tok.is(tok::r_paren)) {
23062309
ParseListItemResult Result;
23072310
do {
2308-
Result = parseListItem(Status, tok::r_paren, LeftLoc, RightLoc,
2309-
/*AllowSepAfterLast=*/false,
2310-
[&]() -> ParserStatus {
2311-
return parsePlatformVersionInList(
2312-
AtAttrName, PlatformAndVersions);
2313-
});
2311+
Result = parseListItem(
2312+
Status, tok::r_paren, LeftLoc, RightLoc,
2313+
/*AllowSepAfterLast=*/false, [&]() -> ParserStatus {
2314+
return parsePlatformVersionInList(AtAttrName, PlatformAndVersions,
2315+
ParsedUnrecognizedPlatformName);
2316+
});
23142317
} while (Result == ParseListItemResult::Continue);
23152318
}
23162319
}
@@ -2323,12 +2326,11 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
23232326
return false;
23242327
}
23252328

2326-
if (PlatformAndVersions.empty()) {
2329+
if (PlatformAndVersions.empty() && !ParsedUnrecognizedPlatformName) {
23272330
diagnose(Loc, diag::attr_availability_need_platform_version, AtAttrName);
23282331
return false;
23292332
}
23302333

2331-
assert(!PlatformAndVersions.empty());
23322334
auto AttrRange = SourceRange(Loc, RightLoc);
23332335
for (auto &Item : PlatformAndVersions) {
23342336
Attributes.add(new (Context) BackDeployedAttr(AtLoc, AttrRange, Item.first,
@@ -3456,6 +3458,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34563458

34573459
StringRef AttrName = "@_originallyDefinedIn";
34583460
bool SuppressLaterDiags = false;
3461+
bool ParsedUnrecognizedPlatformName = false;
34593462
if (parseList(tok::r_paren, LeftLoc, RightLoc, false,
34603463
diag::originally_defined_in_missing_rparen,
34613464
[&]() -> ParserStatus {
@@ -3495,8 +3498,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34953498
}
34963499
// Parse 'OSX 13.13'.
34973500
case NextSegmentKind::PlatformVersion: {
3498-
ParserStatus ListItemStatus =
3499-
parsePlatformVersionInList(AttrName, PlatformAndVersions);
3501+
ParserStatus ListItemStatus = parsePlatformVersionInList(
3502+
AttrName, PlatformAndVersions, ParsedUnrecognizedPlatformName);
35003503
if (ListItemStatus.isErrorOrHasCompletion())
35013504
SuppressLaterDiags = true;
35023505
return ListItemStatus;
@@ -3510,13 +3513,13 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
35103513
diagnose(AtLoc, diag::originally_defined_in_need_nonempty_module_name);
35113514
return makeParserSuccess();
35123515
}
3513-
if (PlatformAndVersions.empty()) {
3516+
3517+
if (PlatformAndVersions.empty() && !ParsedUnrecognizedPlatformName) {
35143518
diagnose(AtLoc, diag::attr_availability_need_platform_version, AttrName);
35153519
return makeParserSuccess();
35163520
}
35173521

35183522
assert(!OriginalModuleName.empty());
3519-
assert(!PlatformAndVersions.empty());
35203523
assert(NK == NextSegmentKind::PlatformVersion);
35213524
AttrRange = SourceRange(Loc, RightLoc);
35223525
for (auto &Item: PlatformAndVersions) {

test/Sema/diag_originally_definedin.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,24 @@ public func macroUnknown() {}
3434

3535
@available(macOS 10.9, *)
3636
@_originallyDefinedIn(module: "original", macos 10.13) // expected-warning {{unknown platform 'macos' for attribute '@_originallyDefinedIn'; did you mean 'macOS'?}} {{43-48=macOS}}
37-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
3837
public func incorrectPlatformCase() {}
3938

4039
@available(macOS 10.9, *)
4140
@_originallyDefinedIn(module: "original", mscos 10.13) // expected-warning {{unknown platform 'mscos' for attribute '@_originallyDefinedIn'; did you mean 'macOS'?}} {{43-48=macOS}}
42-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
4341
public func incorrectPlatformSimilar1() {}
4442

4543
@available(macOS 10.9, *)
4644
@_originallyDefinedIn(module: "original", macoss 10.13) // expected-warning {{unknown platform 'macoss' for attribute '@_originallyDefinedIn'; did you mean 'macOS'?}} {{43-49=macOS}}
47-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
4845
public func incorrectPlatformSimilar2() {}
4946

5047
@available(macOS 10.9, *)
5148
@_originallyDefinedIn(module: "original", mac 10.13) // expected-warning {{unknown platform 'mac' for attribute '@_originallyDefinedIn'; did you mean 'macOS'?}} {{43-46=macOS}}
52-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
5349
public func incorrectPlatformSimilar3() {}
5450

5551
@available(macOS 10.9, *)
5652
@_originallyDefinedIn(module: "original", notValid 10.13) // expected-warning {{unknown platform 'notValid' for attribute '@_originallyDefinedIn'}} {{none}}
57-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
5853
public func incorrectPlatformNotSimilar() {}
5954

6055
@available(macOS 10.9, *)
6156
@_originallyDefinedIn(module: "original", swift 5.1) // expected-warning {{unknown platform 'swift' for attribute '@_originallyDefinedIn'}}
62-
// expected-error@-1 {{expected at least one platform version in '@_originallyDefinedIn' attribute}}
6357
public func swiftVersionMacro() {}

test/attr/Inputs/SymbolMove/LowLevel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public func printMessageMoved() {
55
}
66

77
@available(OSX 10.7, iOS 7.0, *)
8+
@available(unrecognizedOS 1.0, *)
89
@_originallyDefinedIn(module: "HighLevel", OSX 10.9, iOS 13.0)
10+
@_originallyDefinedIn(module: "HighLevel", unrecognizedOS 2.0)
911
public struct Entity {
1012
public let value = "LowLevel"
1113
public init() {}

test/attr/attr_backDeployed.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ public func incorrectPlatformCaseFunc() {}
367367
public func incorrectPlatformSimilarFunc() {}
368368

369369
@backDeployed(before: macOS 12.0, unknownOS 1.0) // expected-warning {{unknown platform 'unknownOS' for attribute '@backDeployed'}}
370-
public func unknownOSFunc() {}
370+
public func unknownOSFunc1() {}
371+
372+
@backDeployed(before: macOS 12.0)
373+
@backDeployed(before: unknownOS 1.0) // expected-warning {{unknown platform 'unknownOS' for attribute '@backDeployed'}}
374+
public func unknownOSFunc2() {}
371375

372376
@backDeployed(before: @) // expected-error {{expected platform in '@backDeployed' attribute}}
373377
public func badPlatformFunc1() {}
@@ -414,7 +418,6 @@ public func missingMacroVersion() {}
414418
public func unknownMacroMissingVersion() {}
415419

416420
@backDeployed(before: _unknownMacro 1.0) // expected-warning {{unknown platform '_unknownMacro' for attribute '@backDeployed'}}
417-
// expected-error@-1 {{expected at least one platform version in '@backDeployed' attribute}}
418421
public func unknownMacroVersioned() {}
419422

420423
@backDeployed(before: _unknownMacro 1.0, _myProject 2.0) // expected-warning {{unknown platform '_unknownMacro' for attribute '@backDeployed'}}

0 commit comments

Comments
 (0)