Skip to content

Commit 7eb5323

Browse files
committed
Sema: Adopt SemanticAvailableAttr in deprecation diagnostics.
1 parent 3716e97 commit 7eb5323

File tree

1 file changed

+66
-64
lines changed

1 file changed

+66
-64
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,16 +2376,16 @@ diagnosePotentialUnavailability(const RootProtocolConformance *rootConf,
23762376

23772377
/// Returns the availability attribute indicating deprecation of the
23782378
/// declaration is deprecated or null otherwise.
2379-
static const AvailableAttr *getDeprecated(const Decl *D) {
2379+
static std::optional<SemanticAvailableAttr> getDeprecated(const Decl *D) {
23802380
auto &Ctx = D->getASTContext();
23812381
if (auto Attr = D->getDeprecatedAttr())
2382-
return Attr->getParsedAttr();
2382+
return Attr;
23832383

23842384
if (Ctx.LangOpts.WarnSoftDeprecated) {
23852385
// When -warn-soft-deprecated is specified, treat any declaration that is
23862386
// deprecated in the future as deprecated.
23872387
if (auto Attr = D->getSoftDeprecatedAttr())
2388-
return Attr->getParsedAttr();
2388+
return Attr;
23892389
}
23902390

23912391
// Treat extensions methods as deprecated if their extension
@@ -2395,18 +2395,17 @@ static const AvailableAttr *getDeprecated(const Decl *D) {
23952395
return getDeprecated(ED);
23962396
}
23972397

2398-
return nullptr;
2398+
return std::nullopt;
23992399
}
24002400

24012401
static void fixItAvailableAttrRename(InFlightDiagnostic &diag,
24022402
SourceRange referenceRange,
24032403
const ValueDecl *renamedDecl,
2404-
const AvailableAttr *attr,
2405-
const Expr *call) {
2404+
StringRef newName, const Expr *call) {
24062405
if (isa<AccessorDecl>(renamedDecl))
24072406
return;
24082407

2409-
ParsedDeclName parsed = swift::parseDeclName(attr->Rename);
2408+
ParsedDeclName parsed = swift::parseDeclName(newName);
24102409
if (!parsed)
24112410
return;
24122411

@@ -2514,7 +2513,6 @@ static void fixItAvailableAttrRename(InFlightDiagnostic &diag,
25142513
// let's just prepend it, otherwise we'll end up with an incorrect fix-it.
25152514
auto base = sourceMgr.extractText(selfExprRange);
25162515
if (!base.empty() && base.front() == '.') {
2517-
auto newName = attr->Rename;
25182516
// If this is not a rename, let's not
25192517
// even try to emit a fix-it because
25202518
// it's going to be invalid.
@@ -2764,9 +2762,9 @@ namespace {
27642762
} // end anonymous namespace
27652763

27662764
static std::optional<ReplacementDeclKind>
2767-
describeRename(ASTContext &ctx, const AvailableAttr *attr, const ValueDecl *D,
2765+
describeRename(ASTContext &ctx, StringRef newName, const ValueDecl *D,
27682766
SmallVectorImpl<char> &nameBuf) {
2769-
ParsedDeclName parsed = swift::parseDeclName(attr->Rename);
2767+
ParsedDeclName parsed = swift::parseDeclName(newName);
27702768
if (!parsed)
27712769
return std::nullopt;
27722770

@@ -2809,7 +2807,7 @@ static void diagnoseIfDeprecated(SourceRange ReferenceRange,
28092807
const ExportContext &Where,
28102808
const ValueDecl *DeprecatedDecl,
28112809
const Expr *Call) {
2812-
const AvailableAttr *Attr = getDeprecated(DeprecatedDecl);
2810+
auto Attr = getDeprecated(DeprecatedDecl);
28132811
if (!Attr)
28142812
return;
28152813

@@ -2835,57 +2833,60 @@ static void diagnoseIfDeprecated(SourceRange ReferenceRange,
28352833
if (shouldIgnoreDeprecationOfConcurrencyDecl(DeprecatedDecl, ReferenceDC))
28362834
return;
28372835

2838-
StringRef Platform = Attr->prettyPlatformString();
2836+
StringRef Platform = Attr->getDomain().getNameForDiagnostics();
28392837
llvm::VersionTuple DeprecatedVersion;
2840-
if (Attr->Deprecated)
2841-
DeprecatedVersion = Attr->Deprecated.value();
2842-
2843-
if (Attr->Message.empty() && Attr->Rename.empty()) {
2844-
Context.Diags.diagnose(
2845-
ReferenceRange.Start, diag::availability_deprecated,
2846-
DeprecatedDecl, Attr->hasPlatform(), Platform,
2847-
Attr->Deprecated.has_value(), DeprecatedVersion,
2848-
/*message*/ StringRef())
2849-
.highlight(Attr->getRange());
2838+
if (Attr->getDeprecated())
2839+
DeprecatedVersion = Attr->getDeprecated().value();
2840+
2841+
auto Message = Attr->getMessage();
2842+
auto NewName = Attr->getRename();
2843+
if (Message.empty() && NewName.empty()) {
2844+
Context.Diags
2845+
.diagnose(ReferenceRange.Start, diag::availability_deprecated,
2846+
DeprecatedDecl, Attr->isPlatformSpecific(), Platform,
2847+
Attr->getDeprecated().has_value(), DeprecatedVersion,
2848+
/*message*/ StringRef())
2849+
.highlight(Attr->getParsedAttr()->getRange());
28502850
return;
28512851
}
28522852

2853+
// FIXME: [availability] Remap before emitting diagnostic above.
28532854
llvm::VersionTuple RemappedDeprecatedVersion;
28542855
if (AvailabilityInference::updateDeprecatedPlatformForFallback(
2855-
Attr, Context, Platform, RemappedDeprecatedVersion))
2856+
Attr->getParsedAttr(), Context, Platform, RemappedDeprecatedVersion))
28562857
DeprecatedVersion = RemappedDeprecatedVersion;
28572858

28582859
SmallString<32> newNameBuf;
28592860
std::optional<ReplacementDeclKind> replacementDeclKind =
2860-
describeRename(Context, Attr, /*decl*/ nullptr, newNameBuf);
2861-
StringRef newName = replacementDeclKind ? newNameBuf.str() : Attr->Rename;
2862-
2863-
if (!Attr->Message.empty()) {
2864-
EncodedDiagnosticMessage EncodedMessage(Attr->Message);
2865-
Context.Diags.diagnose(
2866-
ReferenceRange.Start, diag::availability_deprecated,
2867-
DeprecatedDecl, Attr->hasPlatform(), Platform,
2868-
Attr->Deprecated.has_value(), DeprecatedVersion,
2869-
EncodedMessage.Message)
2870-
.highlight(Attr->getRange());
2861+
describeRename(Context, NewName, /*decl*/ nullptr, newNameBuf);
2862+
StringRef newName = replacementDeclKind ? newNameBuf.str() : NewName;
2863+
2864+
if (!Message.empty()) {
2865+
EncodedDiagnosticMessage EncodedMessage(Message);
2866+
Context.Diags
2867+
.diagnose(ReferenceRange.Start, diag::availability_deprecated,
2868+
DeprecatedDecl, Attr->isPlatformSpecific(), Platform,
2869+
Attr->getDeprecated().has_value(), DeprecatedVersion,
2870+
EncodedMessage.Message)
2871+
.highlight(Attr->getParsedAttr()->getRange());
28712872
} else {
28722873
unsigned rawReplaceKind = static_cast<unsigned>(
28732874
replacementDeclKind.value_or(ReplacementDeclKind::None));
2874-
Context.Diags.diagnose(
2875-
ReferenceRange.Start, diag::availability_deprecated_rename,
2876-
DeprecatedDecl, Attr->hasPlatform(), Platform,
2877-
Attr->Deprecated.has_value(), DeprecatedVersion,
2878-
replacementDeclKind.has_value(), rawReplaceKind, newName)
2879-
.highlight(Attr->getRange());
2875+
Context.Diags
2876+
.diagnose(ReferenceRange.Start, diag::availability_deprecated_rename,
2877+
DeprecatedDecl, Attr->isPlatformSpecific(), Platform,
2878+
Attr->getDeprecated().has_value(), DeprecatedVersion,
2879+
replacementDeclKind.has_value(), rawReplaceKind, newName)
2880+
.highlight(Attr->getParsedAttr()->getRange());
28802881
}
28812882

2882-
if (!Attr->Rename.empty() && !isa<AccessorDecl>(DeprecatedDecl)) {
2883+
if (!NewName.empty() && !isa<AccessorDecl>(DeprecatedDecl)) {
28832884
auto renameDiag = Context.Diags.diagnose(
28842885
ReferenceRange.Start,
28852886
diag::note_deprecated_rename,
28862887
newName);
28872888
fixItAvailableAttrRename(renameDiag, ReferenceRange, DeprecatedDecl,
2888-
Attr, Call);
2889+
NewName, Call);
28892890
}
28902891
}
28912892

@@ -2894,7 +2895,7 @@ static bool diagnoseIfDeprecated(SourceLoc loc,
28942895
const RootProtocolConformance *rootConf,
28952896
const ExtensionDecl *ext,
28962897
const ExportContext &where) {
2897-
const AvailableAttr *attr = getDeprecated(ext);
2898+
auto attr = getDeprecated(ext);
28982899
if (!attr)
28992900
return false;
29002901

@@ -2920,33 +2921,34 @@ static bool diagnoseIfDeprecated(SourceLoc loc,
29202921
auto type = rootConf->getType();
29212922
auto proto = rootConf->getProtocol()->getDeclaredInterfaceType();
29222923

2923-
StringRef platform = attr->prettyPlatformString();
2924+
StringRef platform = attr->getDomain().getNameForDiagnostics();
29242925
llvm::VersionTuple deprecatedVersion;
2925-
if (attr->Deprecated)
2926-
deprecatedVersion = attr->Deprecated.value();
2926+
if (attr->getDeprecated())
2927+
deprecatedVersion = attr->getDeprecated().value();
29272928

29282929
llvm::VersionTuple remappedDeprecatedVersion;
29292930
if (AvailabilityInference::updateDeprecatedPlatformForFallback(
2930-
attr, ctx, platform, remappedDeprecatedVersion))
2931+
attr->getParsedAttr(), ctx, platform, remappedDeprecatedVersion))
29312932
deprecatedVersion = remappedDeprecatedVersion;
29322933

2933-
if (attr->Message.empty()) {
2934-
ctx.Diags.diagnose(
2935-
loc, diag::conformance_availability_deprecated,
2936-
type, proto, attr->hasPlatform(), platform,
2937-
attr->Deprecated.has_value(), deprecatedVersion,
2938-
/*message*/ StringRef())
2939-
.highlight(attr->getRange());
2934+
auto message = attr->getMessage();
2935+
if (message.empty()) {
2936+
ctx.Diags
2937+
.diagnose(loc, diag::conformance_availability_deprecated, type, proto,
2938+
attr->isPlatformSpecific(), platform,
2939+
attr->getDeprecated().has_value(), deprecatedVersion,
2940+
/*message*/ StringRef())
2941+
.highlight(attr->getParsedAttr()->getRange());
29402942
return true;
29412943
}
29422944

2943-
EncodedDiagnosticMessage encodedMessage(attr->Message);
2944-
ctx.Diags.diagnose(
2945-
loc, diag::conformance_availability_deprecated,
2946-
type, proto, attr->hasPlatform(), platform,
2947-
attr->Deprecated.has_value(), deprecatedVersion,
2948-
encodedMessage.Message)
2949-
.highlight(attr->getRange());
2945+
EncodedDiagnosticMessage encodedMessage(message);
2946+
ctx.Diags
2947+
.diagnose(loc, diag::conformance_availability_deprecated, type, proto,
2948+
attr->isPlatformSpecific(), platform,
2949+
attr->getDeprecated().has_value(), deprecatedVersion,
2950+
encodedMessage.Message)
2951+
.highlight(attr->getParsedAttr()->getRange());
29502952
return true;
29512953
}
29522954

@@ -3003,7 +3005,7 @@ static bool diagnoseExplicitUnavailability(const ValueDecl *D, SourceRange R,
30033005
D, R, Where, Flags, [=](InFlightDiagnostic &diag) {
30043006
auto attr = D->getUnavailableAttr();
30053007
assert(attr);
3006-
fixItAvailableAttrRename(diag, R, D, attr->getParsedAttr(), call);
3008+
fixItAvailableAttrRename(diag, R, D, attr->getRename(), call);
30073009
});
30083010
}
30093011

@@ -3559,7 +3561,7 @@ bool diagnoseExplicitUnavailability(
35593561
if (!Attr->Rename.empty()) {
35603562
SmallString<32> newNameBuf;
35613563
std::optional<ReplacementDeclKind> replaceKind =
3562-
describeRename(ctx, Attr, D, newNameBuf);
3564+
describeRename(ctx, Attr->Rename, D, newNameBuf);
35633565
unsigned rawReplaceKind = static_cast<unsigned>(
35643566
replaceKind.value_or(ReplacementDeclKind::None));
35653567
StringRef newName = replaceKind ? newNameBuf.str() : Attr->Rename;
@@ -4168,7 +4170,7 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
41684170
diag.warnUntilSwiftVersion(6);
41694171

41704172
if (!attr->getRename().empty()) {
4171-
fixItAvailableAttrRename(diag, R, D, attr->getParsedAttr(), call);
4173+
fixItAvailableAttrRename(diag, R, D, attr->getRename(), call);
41724174
}
41734175
return true;
41744176
}

0 commit comments

Comments
 (0)