Skip to content

Suggest narrowing an existing availability context, when feasible. #6924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,15 +589,18 @@ class AvailableAttr : public DeclAttribute {
PlatformKind Platform,
StringRef Message, StringRef Rename,
const clang::VersionTuple &Introduced,
SourceRange IntroducedRange,
const clang::VersionTuple &Deprecated,
SourceRange DeprecatedRange,
const clang::VersionTuple &Obsoleted,
SourceRange ObsoletedRange,
PlatformAgnosticAvailabilityKind PlatformAgnostic,
bool Implicit)
: DeclAttribute(DAK_Available, AtLoc, Range, Implicit),
Message(Message), Rename(Rename),
INIT_VER_TUPLE(Introduced),
INIT_VER_TUPLE(Deprecated),
INIT_VER_TUPLE(Obsoleted),
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
PlatformAgnostic(PlatformAgnostic),
Platform(Platform)
{}
Expand All @@ -618,12 +621,21 @@ class AvailableAttr : public DeclAttribute {
/// Indicates when the symbol was introduced.
const Optional<clang::VersionTuple> Introduced;

/// Indicates where the Introduced version was specified.
const SourceRange IntroducedRange;

/// Indicates when the symbol was deprecated.
const Optional<clang::VersionTuple> Deprecated;

/// Indicates where the Deprecated version was specified.
const SourceRange DeprecatedRange;

/// Indicates when the symbol was obsoleted.
const Optional<clang::VersionTuple> Obsoleted;

/// Indicates where the Obsoleted version was specified.
const SourceRange ObsoletedRange;

/// Indicates if the declaration has platform-agnostic availability.
const PlatformAgnosticAvailabilityKind PlatformAgnostic;

Expand Down
11 changes: 10 additions & 1 deletion include/swift/AST/TypeRefinementContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,16 @@ class TypeRefinementContext {
/// or an invalid location if the context reflects the minimum deployment
// target.
SourceLoc getIntroductionLoc() const;


/// Returns the source range covering a _single_ decl-attribute or statement
/// condition that introduced the refinement context for a given platform
/// version; if zero or multiple such responsible attributes or statements
/// exist, returns an invalid SourceRange.
SourceRange
getAvailabilityConditionVersionSourceRange(
PlatformKind Platform,
const clang::VersionTuple &Version) const;

/// Returns the source range on which this context refines types.
SourceRange getSourceRange() const { return SrcRange; }

Expand Down
5 changes: 4 additions & 1 deletion lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ AvailableAttr::createPlatformAgnostic(ASTContext &C,
}
return new (C) AvailableAttr(
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
NoVersion, NoVersion, Obsoleted, Kind, /* isImplicit */ false);
NoVersion, SourceRange(),
NoVersion, SourceRange(),
Obsoleted, SourceRange(),
Kind, /* isImplicit */ false);
}

bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
Expand Down
5 changes: 4 additions & 1 deletion lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ createAvailableAttr(PlatformKind Platform,
return new (Context) AvailableAttr(
SourceLoc(), SourceRange(), Platform,
/*Message=*/StringRef(),
/*Rename=*/StringRef(), Introduced, Deprecated, Obsoleted,
/*Rename=*/StringRef(),
Introduced, /*IntroducedRange=*/SourceRange(),
Deprecated, /*DeprecatedRange=*/SourceRange(),
Obsoleted, /*ObsoletedRange=*/SourceRange(),
Inferred.PlatformAgnostic, /*Implicit=*/true);
}

Expand Down
93 changes: 93 additions & 0 deletions lib/AST/TypeRefinementContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,99 @@ SourceLoc TypeRefinementContext::getIntroductionLoc() const {
llvm_unreachable("Unhandled Reason in switch.");
}

static SourceRange
getAvailabilityConditionVersionSourceRange(const PoundAvailableInfo *PAI,
PlatformKind Platform,
const clang::VersionTuple &Version) {
SourceRange Range;
for (auto *S : PAI->getQueries()) {
if (auto *V = dyn_cast<PlatformVersionConstraintAvailabilitySpec>(S)) {
if (V->getPlatform() == Platform && V->getVersion() == Version) {
// More than one: return invalid range, no unique choice.
if (Range.isValid())
return SourceRange();
else
Range = V->getVersionSrcRange();
}
}
}
return Range;
}

static SourceRange
getAvailabilityConditionVersionSourceRange(
const MutableArrayRef<StmtConditionElement> &Conds,
PlatformKind Platform,
const clang::VersionTuple &Version) {
SourceRange Range;
for (auto const& C : Conds) {
if (C.getKind() == StmtConditionElement::CK_Availability) {
SourceRange R = getAvailabilityConditionVersionSourceRange(
C.getAvailability(), Platform, Version);
// More than one: return invalid range.
if (Range.isValid())
return SourceRange();
else
Range = R;
}
}
return Range;
}

static SourceRange
getAvailabilityConditionVersionSourceRange(const DeclAttributes &DeclAttrs,
PlatformKind Platform,
const clang::VersionTuple &Version) {
SourceRange Range;
for (auto *Attr : DeclAttrs) {
if (auto *AA = dyn_cast<AvailableAttr>(Attr)) {
if (AA->Introduced.hasValue() &&
AA->Introduced.getValue() == Version &&
AA->Platform == Platform) {

// More than one: return invalid range.
if (Range.isValid())
return SourceRange();
else
Range = AA->IntroducedRange;
}
}
}
return Range;
}

SourceRange
TypeRefinementContext::getAvailabilityConditionVersionSourceRange(
PlatformKind Platform,
const clang::VersionTuple &Version) const {
switch (getReason()) {
case Reason::Decl:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsDecl()->getAttrs(), Platform, Version);

case Reason::IfStmtThenBranch:
case Reason::IfStmtElseBranch:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsIfStmt()->getCond(), Platform, Version);

case Reason::ConditionFollowingAvailabilityQuery:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsPoundAvailableInfo(), Platform, Version);

case Reason::GuardStmtFallthrough:
case Reason::GuardStmtElseBranch:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsGuardStmt()->getCond(), Platform, Version);

case Reason::WhileStmtBody:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsWhileStmt()->getCond(), Platform, Version);

case Reason::Root:
return SourceRange();
}
}

void TypeRefinementContext::print(raw_ostream &OS, SourceManager &SrcMgr,
unsigned Indent) const {
OS.indent(Indent);
Expand Down
10 changes: 9 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,11 @@ static void applyAvailableAttribute(Decl *decl, AvailabilityContext &info,
/*Message=*/StringRef(),
/*Rename=*/StringRef(),
info.getOSVersion().getLowerEndpoint(),
/*IntroducedRange*/SourceRange(),
/*Deprecated=*/noVersion,
/*DeprecatedRange*/SourceRange(),
/*Obsoleted=*/noVersion,
/*ObsoletedRange*/SourceRange(),
PlatformAgnosticAvailabilityKind::None,
/*Implicit=*/false);

Expand Down Expand Up @@ -6571,7 +6574,12 @@ void ClangImporter::Implementation::importAttributes(
auto AvAttr = new (C) AvailableAttr(SourceLoc(), SourceRange(),
platformK.getValue(),
message, swiftReplacement,
introduced, deprecated, obsoleted,
introduced,
/*IntroducedRange=*/SourceRange(),
deprecated,
/*DeprecatedRange=*/SourceRange(),
obsoleted,
/*ObsoletedRange=*/SourceRange(),
PlatformAgnostic, /*Implicit=*/false);

MappedDecl->getAttrs().add(AvAttr);
Expand Down
19 changes: 14 additions & 5 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,18 +897,21 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
for (auto *Spec : Specs) {
PlatformKind Platform;
clang::VersionTuple Version;
SourceRange VersionRange;
PlatformAgnosticAvailabilityKind PlatformAgnostic;

if (auto *PlatformVersionSpec =
dyn_cast<PlatformVersionConstraintAvailabilitySpec>(Spec)) {
Platform = PlatformVersionSpec->getPlatform();
Version = PlatformVersionSpec->getVersion();
VersionRange = PlatformVersionSpec->getVersionSrcRange();
PlatformAgnostic = PlatformAgnosticAvailabilityKind::None;

} else if (auto *LanguageVersionSpec =
dyn_cast<LanguageVersionConstraintAvailabilitySpec>(Spec)) {
Platform = PlatformKind::none;
Version = LanguageVersionSpec->getVersion();
VersionRange = LanguageVersionSpec->getVersionSrcRange();
PlatformAgnostic =
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific;

Expand All @@ -922,8 +925,11 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
/*Message=*/StringRef(),
/*Rename=*/StringRef(),
/*Introduced=*/Version,
/*IntroducedRange=*/VersionRange,
/*Deprecated=*/clang::VersionTuple(),
/*DeprecatedRange=*/SourceRange(),
/*Obsoleted=*/clang::VersionTuple(),
/*ObsoletedRange=*/SourceRange(),
PlatformAgnostic,
/*Implicit=*/true));
}
Expand All @@ -941,6 +947,7 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,

StringRef Message, Renamed;
clang::VersionTuple Introduced, Deprecated, Obsoleted;
SourceRange IntroducedRange, DeprecatedRange, ObsoletedRange;
auto PlatformAgnostic = PlatformAgnosticAvailabilityKind::None;
bool AnyAnnotations = false;
int ParamIndex = 0;
Expand Down Expand Up @@ -1060,8 +1067,10 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
(ArgumentKind == IsDeprecated) ? Deprecated :
Obsoleted;

SourceRange VersionRange;

auto &VersionRange = (ArgumentKind == IsIntroduced) ? IntroducedRange :
(ArgumentKind == IsDeprecated) ? DeprecatedRange :
ObsoletedRange;

if (parseVersionTuple(
VersionArg, VersionRange,
Diagnostic(diag::attr_availability_expected_version,
Expand Down Expand Up @@ -1127,9 +1136,9 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
AvailableAttr(AtLoc, AttrRange,
PlatformKind.getValue(),
Message, Renamed,
Introduced,
Deprecated,
Obsoleted,
Introduced, IntroducedRange,
Deprecated, DeprecatedRange,
Obsoleted, ObsoletedRange,
PlatformAgnostic,
/*Implicit=*/false));
} else {
Expand Down
Loading