Skip to content

Commit 8eb583c

Browse files
Merge pull request #76073 from ian-twilightcoder/unknown-platform-attr
[Sema] Allow unrecognized platforms in @backDeployed and @_originallyDefinedIn
2 parents 7907f1f + eb11ad9 commit 8eb583c

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
@@ -2104,12 +2104,15 @@ class Parser {
21042104

21052105
using PlatformAndVersion = std::pair<PlatformKind, llvm::VersionTuple>;
21062106

2107-
/// Parse a platform and version tuple (e.g. "macOS 12.0") and append it to the
2108-
/// given vector. Wildcards ('*') parse successfully but are ignored. Assumes
2109-
/// that the tuples are part of a comma separated list ending with a trailing
2110-
/// ')'.
2111-
ParserStatus parsePlatformVersionInList(StringRef AttrName,
2112-
llvm::SmallVector<PlatformAndVersion, 4> &PlatformAndVersions);
2107+
/// Parse a platform and version tuple (e.g. "macOS 12.0") and append it to
2108+
/// the given vector. Wildcards ('*') parse successfully but are ignored.
2109+
/// Unrecognized platform names also parse successfully but are ignored.
2110+
/// Assumes that the tuples are part of a comma separated list ending with a
2111+
/// trailing ')'.
2112+
ParserStatus parsePlatformVersionInList(
2113+
StringRef AttrName,
2114+
llvm::SmallVector<PlatformAndVersion, 4> & PlatformAndVersions,
2115+
bool &ParsedUnrecognizedPlatformName);
21132116

21142117
//===--------------------------------------------------------------------===//
21152118
// Code completion second pass.

lib/Parse/ParseDecl.cpp

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

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

@@ -2288,6 +2290,7 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
22882290
SourceLoc RightLoc;
22892291
ParserStatus Status;
22902292
bool SuppressLaterDiags = false;
2293+
bool ParsedUnrecognizedPlatformName = false;
22912294
llvm::SmallVector<PlatformAndVersion, 4> PlatformAndVersions;
22922295

22932296
{
@@ -2306,12 +2309,12 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
23062309
if (!Tok.is(tok::r_paren)) {
23072310
ParseListItemResult Result;
23082311
do {
2309-
Result = parseListItem(Status, tok::r_paren, LeftLoc, RightLoc,
2310-
/*AllowSepAfterLast=*/false,
2311-
[&]() -> ParserStatus {
2312-
return parsePlatformVersionInList(
2313-
AtAttrName, PlatformAndVersions);
2314-
});
2312+
Result = parseListItem(
2313+
Status, tok::r_paren, LeftLoc, RightLoc,
2314+
/*AllowSepAfterLast=*/false, [&]() -> ParserStatus {
2315+
return parsePlatformVersionInList(AtAttrName, PlatformAndVersions,
2316+
ParsedUnrecognizedPlatformName);
2317+
});
23152318
} while (Result == ParseListItemResult::Continue);
23162319
}
23172320
}
@@ -2324,12 +2327,11 @@ bool Parser::parseBackDeployedAttribute(DeclAttributes &Attributes,
23242327
return false;
23252328
}
23262329

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

2332-
assert(!PlatformAndVersions.empty());
23332335
auto AttrRange = SourceRange(Loc, RightLoc);
23342336
for (auto &Item : PlatformAndVersions) {
23352337
Attributes.add(new (Context) BackDeployedAttr(AtLoc, AttrRange, Item.first,
@@ -3445,6 +3447,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34453447

34463448
StringRef AttrName = "@_originallyDefinedIn";
34473449
bool SuppressLaterDiags = false;
3450+
bool ParsedUnrecognizedPlatformName = false;
34483451
if (parseList(tok::r_paren, LeftLoc, RightLoc, false,
34493452
diag::originally_defined_in_missing_rparen,
34503453
[&]() -> ParserStatus {
@@ -3484,8 +3487,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34843487
}
34853488
// Parse 'OSX 13.13'.
34863489
case NextSegmentKind::PlatformVersion: {
3487-
ParserStatus ListItemStatus =
3488-
parsePlatformVersionInList(AttrName, PlatformAndVersions);
3490+
ParserStatus ListItemStatus = parsePlatformVersionInList(
3491+
AttrName, PlatformAndVersions, ParsedUnrecognizedPlatformName);
34893492
if (ListItemStatus.isErrorOrHasCompletion())
34903493
SuppressLaterDiags = true;
34913494
return ListItemStatus;
@@ -3499,13 +3502,13 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34993502
diagnose(AtLoc, diag::originally_defined_in_need_nonempty_module_name);
35003503
return makeParserSuccess();
35013504
}
3502-
if (PlatformAndVersions.empty()) {
3505+
3506+
if (PlatformAndVersions.empty() && !ParsedUnrecognizedPlatformName) {
35033507
diagnose(AtLoc, diag::attr_availability_need_platform_version, AttrName);
35043508
return makeParserSuccess();
35053509
}
35063510

35073511
assert(!OriginalModuleName.empty());
3508-
assert(!PlatformAndVersions.empty());
35093512
assert(NK == NextSegmentKind::PlatformVersion);
35103513
AttrRange = SourceRange(Loc, RightLoc);
35113514
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)