Skip to content

Commit f254619

Browse files
authored
Merge pull request #29336 from nkcsgexi/diag-refactor-original-defined
Sema: refactor some code to diagnose multiple active platforms for @_originallyDefinedFrom. NFC
2 parents 26f0924 + 7987725 commit f254619

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

include/swift/AST/PlatformKind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace swift {
2626
class LangOptions;
2727

2828
/// Available platforms for the availability attribute.
29-
enum class PlatformKind {
29+
enum class PlatformKind: uint8_t {
3030
none,
3131
#define AVAILABILITY_PLATFORM(X, PrettyName) X,
3232
#include "swift/AST/PlatformKinds.def"

lib/Sema/TypeCheckAttr.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,42 +2662,40 @@ void AttributeChecker::checkOriginalDefinedInAttrs(Decl *D,
26622662
if (Attrs.empty())
26632663
return;
26642664
auto &Ctx = D->getASTContext();
2665-
OriginallyDefinedInAttr* theAttr = nullptr;
2665+
std::map<PlatformKind, SourceLoc> seenPlatforms;
2666+
26662667
// Attrs are in the reverse order of the source order. We need to visit them
26672668
// in source order to diagnose the later attribute.
26682669
for (auto *Attr: Attrs) {
26692670
if (!Attr->isActivePlatform(Ctx))
26702671
continue;
2671-
if (theAttr) {
2672-
// Only one version number is allowed for one platform name.
2673-
diagnose(theAttr->AtLoc, diag::originally_defined_in_dupe_platform,
2674-
platformString(Attr->Platform));
2672+
auto AtLoc = Attr->AtLoc;
2673+
auto Platform = Attr->Platform;
2674+
if (!seenPlatforms.insert({Platform, AtLoc}).second) {
2675+
// We've seen the platform before, emit error to the previous one which
2676+
// comes later in the source order.
2677+
diagnose(seenPlatforms[Platform],
2678+
diag::originally_defined_in_dupe_platform,
2679+
platformString(Platform));
2680+
return;
2681+
}
2682+
static StringRef AttrName = "_originallyDefinedIn";
2683+
if (!D->getDeclContext()->isModuleScopeContext()) {
2684+
diagnose(AtLoc, diag::originally_definedin_topleve_decl, AttrName);
2685+
return;
2686+
}
2687+
auto IntroVer = D->getIntroducedOSVersion(Platform);
2688+
if (!IntroVer.hasValue()) {
2689+
diagnose(AtLoc, diag::originally_definedin_need_available,
2690+
AttrName);
2691+
return;
2692+
}
2693+
if (IntroVer.getValue() >= Attr->MovedVersion) {
2694+
diagnose(AtLoc,
2695+
diag::originally_definedin_must_after_available_version,
2696+
AttrName);
26752697
return;
2676-
} else {
2677-
theAttr = Attr;
26782698
}
2679-
}
2680-
if (!theAttr)
2681-
return;
2682-
assert(theAttr);
2683-
static StringRef AttrName = "_originallyDefinedIn";
2684-
auto AtLoc = theAttr->AtLoc;
2685-
if (!D->getDeclContext()->isModuleScopeContext()) {
2686-
diagnose(AtLoc, diag::originally_definedin_topleve_decl, AttrName);
2687-
return;
2688-
}
2689-
auto AvailRange = AvailabilityInference::availableRange(D, Ctx);
2690-
if (!AvailRange.getOSVersion().hasLowerEndpoint()) {
2691-
diagnose(AtLoc, diag::originally_definedin_need_available,
2692-
AttrName);
2693-
return;
2694-
}
2695-
auto AvailBegin = AvailRange.getOSVersion().getLowerEndpoint();
2696-
if (AvailBegin >= theAttr->MovedVersion) {
2697-
diagnose(AtLoc,
2698-
diag::originally_definedin_must_after_available_version,
2699-
AttrName);
2700-
return;
27012699
}
27022700
}
27032701

test/Parse/original_defined_in_attr.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ToplevelClass2 {}
1919
@_originallyDefinedIn(module: "foo", // expected-error {{expected at least one platform version in @_originallyDefinedIn}}
2020
class ToplevelClass3 {}
2121
22+
@available(OSX 13.10, *)
2223
@_originallyDefinedIn(module: "foo", * 13.13) // expected-warning {{* as platform name has no effect}} expected-error {{expected at least one platform version in @_originallyDefinedIn}}
2324
@_originallyDefinedIn(module: "foo", OSX 13.13, iOS 7.0)
2425
@_originallyDefinedIn(module: "foo", OSX 13.14, * 7.0) // expected-warning {{* as platform name has no effect}} expected-error {{duplicate version number for platform OSX}}

0 commit comments

Comments
 (0)