@@ -47,6 +47,31 @@ using namespace swift;
47
47
static const Decl *
48
48
concreteSyntaxDeclForAvailableAttribute (const Decl *AbstractSyntaxDecl);
49
49
50
+ // / Emit a diagnostic for references to declarations that have been
51
+ // / marked as unavailable, either through "unavailable" or "obsoleted:".
52
+ static bool diagnoseExplicitUnavailability (
53
+ SourceLoc loc, const RootProtocolConformance *rootConf,
54
+ const ExtensionDecl *ext, const ExportContext &where,
55
+ bool warnIfConformanceUnavailablePreSwift6 = false );
56
+
57
+ // / Emit a diagnostic for references to declarations that have been
58
+ // / marked as unavailable, either through "unavailable" or "obsoleted:".
59
+ static bool diagnoseExplicitUnavailability (
60
+ const ValueDecl *D, SourceRange R, const ExportContext &Where,
61
+ DeclAvailabilityFlags Flags,
62
+ llvm::function_ref<void (InFlightDiagnostic &)> attachRenameFixIts);
63
+
64
+ static bool diagnoseSubstitutionMapAvailability (
65
+ SourceLoc loc, SubstitutionMap subs, const ExportContext &where,
66
+ Type depTy = Type(), Type replacementTy = Type(),
67
+ bool warnIfConformanceUnavailablePreSwift6 = false,
68
+ bool suppressParameterizationCheckForOptional = false);
69
+
70
+ // / Diagnose uses of unavailable declarations in types.
71
+ static bool
72
+ diagnoseTypeReprAvailability (const TypeRepr *T, const ExportContext &where,
73
+ DeclAvailabilityFlags flags = std::nullopt);
74
+
50
75
ExportContext::ExportContext (DeclContext *DC, AvailabilityContext availability,
51
76
FragileFunctionKind kind, bool spi, bool exported,
52
77
bool implicit)
@@ -2057,7 +2082,7 @@ static void fixAvailability(SourceRange ReferenceRange,
2057
2082
}
2058
2083
}
2059
2084
2060
- void TypeChecker:: diagnosePotentialUnavailability (
2085
+ static void diagnosePotentialUnavailability (
2061
2086
SourceRange ReferenceRange, Diag<StringRef, llvm::VersionTuple> Diag,
2062
2087
const DeclContext *ReferenceDC, const AvailabilityRange &Availability) {
2063
2088
ASTContext &Context = ReferenceDC->getASTContext ();
@@ -2139,10 +2164,14 @@ static Diagnostic getPotentialUnavailabilityDiagnostic(
2139
2164
Availability.getRawMinimumVersion ());
2140
2165
}
2141
2166
2142
- bool TypeChecker::diagnosePotentialUnavailability (
2143
- const ValueDecl *D, SourceRange ReferenceRange,
2144
- const DeclContext *ReferenceDC, const AvailabilityRange &Availability,
2145
- bool WarnBeforeDeploymentTarget = false ) {
2167
+ // Emits a diagnostic for a reference to a declaration that is potentially
2168
+ // unavailable at the given source location. Returns true if an error diagnostic
2169
+ // was emitted.
2170
+ static bool
2171
+ diagnosePotentialUnavailability (const ValueDecl *D, SourceRange ReferenceRange,
2172
+ const DeclContext *ReferenceDC,
2173
+ const AvailabilityRange &Availability,
2174
+ bool WarnBeforeDeploymentTarget = false ) {
2146
2175
ASTContext &Context = ReferenceDC->getASTContext ();
2147
2176
2148
2177
bool IsError;
@@ -2162,7 +2191,9 @@ bool TypeChecker::diagnosePotentialUnavailability(
2162
2191
return IsError;
2163
2192
}
2164
2193
2165
- void TypeChecker::diagnosePotentialAccessorUnavailability (
2194
+ // / Emits a diagnostic for a reference to a storage accessor that is
2195
+ // / potentially unavailable.
2196
+ static void diagnosePotentialAccessorUnavailability (
2166
2197
const AccessorDecl *Accessor, SourceRange ReferenceRange,
2167
2198
const DeclContext *ReferenceDC, const AvailabilityRange &Availability,
2168
2199
bool ForInout) {
@@ -2207,10 +2238,13 @@ behaviorLimitForExplicitUnavailability(
2207
2238
return DiagnosticBehavior::Unspecified;
2208
2239
}
2209
2240
2210
- void TypeChecker::diagnosePotentialUnavailability (
2211
- const RootProtocolConformance *rootConf, const ExtensionDecl *ext,
2212
- SourceLoc loc, const DeclContext *dc,
2213
- const AvailabilityRange &availability) {
2241
+ // / Emits a diagnostic for a protocol conformance that is potentially
2242
+ // / unavailable at the given source location.
2243
+ static void
2244
+ diagnosePotentialUnavailability (const RootProtocolConformance *rootConf,
2245
+ const ExtensionDecl *ext, SourceLoc loc,
2246
+ const DeclContext *dc,
2247
+ const AvailabilityRange &availability) {
2214
2248
ASTContext &ctx = dc->getASTContext ();
2215
2249
2216
2250
{
@@ -2233,7 +2267,9 @@ void TypeChecker::diagnosePotentialUnavailability(
2233
2267
fixAvailability (loc, dc, availability, ctx);
2234
2268
}
2235
2269
2236
- const AvailableAttr *TypeChecker::getDeprecated (const Decl *D) {
2270
+ // / Returns the availability attribute indicating deprecation of the
2271
+ // / declaration is deprecated or null otherwise.
2272
+ static const AvailableAttr *getDeprecated (const Decl *D) {
2237
2273
auto &Ctx = D->getASTContext ();
2238
2274
if (auto *Attr = D->getAttrs ().getDeprecated (Ctx))
2239
2275
return Attr;
@@ -2661,11 +2697,12 @@ describeRename(ASTContext &ctx, const AvailableAttr *attr, const ValueDecl *D,
2661
2697
return ReplacementDeclKind::None;
2662
2698
}
2663
2699
2664
- void TypeChecker::diagnoseIfDeprecated (SourceRange ReferenceRange,
2665
- const ExportContext &Where,
2666
- const ValueDecl *DeprecatedDecl,
2667
- const Expr *Call) {
2668
- const AvailableAttr *Attr = TypeChecker::getDeprecated (DeprecatedDecl);
2700
+ // / Emits a diagnostic for a reference to a declaration that is deprecated.
2701
+ static void diagnoseIfDeprecated (SourceRange ReferenceRange,
2702
+ const ExportContext &Where,
2703
+ const ValueDecl *DeprecatedDecl,
2704
+ const Expr *Call) {
2705
+ const AvailableAttr *Attr = getDeprecated (DeprecatedDecl);
2669
2706
if (!Attr)
2670
2707
return ;
2671
2708
@@ -2745,11 +2782,12 @@ void TypeChecker::diagnoseIfDeprecated(SourceRange ReferenceRange,
2745
2782
}
2746
2783
}
2747
2784
2748
- bool TypeChecker::diagnoseIfDeprecated (SourceLoc loc,
2749
- const RootProtocolConformance *rootConf,
2750
- const ExtensionDecl *ext,
2751
- const ExportContext &where) {
2752
- const AvailableAttr *attr = TypeChecker::getDeprecated (ext);
2785
+ // / Emits a diagnostic for a reference to a conformance that is deprecated.
2786
+ static bool diagnoseIfDeprecated (SourceLoc loc,
2787
+ const RootProtocolConformance *rootConf,
2788
+ const ExtensionDecl *ext,
2789
+ const ExportContext &where) {
2790
+ const AvailableAttr *attr = getDeprecated (ext);
2753
2791
if (!attr)
2754
2792
return false ;
2755
2793
@@ -2971,7 +3009,7 @@ getExplicitUnavailabilityDiagnosticInfo(const Decl *decl,
2971
3009
}
2972
3010
}
2973
3011
2974
- bool swift:: diagnoseExplicitUnavailability (
3012
+ bool diagnoseExplicitUnavailability (
2975
3013
SourceLoc loc, const RootProtocolConformance *rootConf,
2976
3014
const ExtensionDecl *ext, const ExportContext &where,
2977
3015
bool warnIfConformanceUnavailablePreSwift6) {
@@ -3348,10 +3386,8 @@ static void checkFunctionConversionAvailability(Type srcType, Type destType,
3348
3386
}
3349
3387
}
3350
3388
3351
- bool swift::diagnoseExplicitUnavailability (
3352
- const ValueDecl *D,
3353
- SourceRange R,
3354
- const ExportContext &Where,
3389
+ bool diagnoseExplicitUnavailability (
3390
+ const ValueDecl *D, SourceRange R, const ExportContext &Where,
3355
3391
DeclAvailabilityFlags Flags,
3356
3392
llvm::function_ref<void (InFlightDiagnostic &)> attachRenameFixIts) {
3357
3393
auto diagnosticInfo = getExplicitUnavailabilityDiagnosticInfo (D, Where);
@@ -4030,11 +4066,11 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *D, SourceRange R,
4030
4066
// Make sure not to diagnose an accessor's deprecation if we already
4031
4067
// complained about the property/subscript.
4032
4068
bool isAccessorWithDeprecatedStorage =
4033
- accessor && TypeChecker:: getDeprecated (accessor->getStorage ());
4069
+ accessor && getDeprecated (accessor->getStorage ());
4034
4070
4035
4071
// Diagnose for deprecation
4036
4072
if (!isAccessorWithDeprecatedStorage)
4037
- TypeChecker:: diagnoseIfDeprecated (R, Where, D, call);
4073
+ diagnoseIfDeprecated (R, Where, D, call);
4038
4074
4039
4075
if (Flags.contains (DeclAvailabilityFlag::AllowPotentiallyUnavailableProtocol)
4040
4076
&& isa<ProtocolDecl>(D))
@@ -4056,11 +4092,10 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *D, SourceRange R,
4056
4092
4057
4093
if (accessor) {
4058
4094
bool forInout = Flags.contains (DeclAvailabilityFlag::ForInout);
4059
- TypeChecker:: diagnosePotentialAccessorUnavailability (
4060
- accessor, R, DC, requiredAvailability, forInout);
4095
+ diagnosePotentialAccessorUnavailability (accessor, R, DC,
4096
+ requiredAvailability, forInout);
4061
4097
} else {
4062
- if (!TypeChecker::diagnosePotentialUnavailability (D, R, DC,
4063
- requiredAvailability))
4098
+ if (!diagnosePotentialUnavailability (D, R, DC, requiredAvailability))
4064
4099
return false ;
4065
4100
}
4066
4101
@@ -4246,7 +4281,8 @@ class StmtAvailabilityWalker : public BaseDiagnosticWalker {
4246
4281
PreWalkResult<Pattern *> walkToPatternPre (Pattern *P) override {
4247
4282
if (auto *IP = dyn_cast<IsPattern>(P)) {
4248
4283
auto where = ExportContext::forFunctionBody (DC, P->getLoc ());
4249
- diagnoseTypeAvailability (IP->getCastType (), P->getLoc (), where);
4284
+ diagnoseTypeAvailability (IP->getCastTypeRepr (), IP->getCastType (),
4285
+ P->getLoc (), where, std::nullopt);
4250
4286
}
4251
4287
4252
4288
return Action::Continue (P);
@@ -4346,9 +4382,9 @@ class TypeReprAvailabilityWalker : public ASTWalker {
4346
4382
4347
4383
}
4348
4384
4349
- bool swift::diagnoseTypeReprAvailability ( const TypeRepr *T,
4350
- const ExportContext &where,
4351
- DeclAvailabilityFlags flags) {
4385
+ // / Diagnose uses of unavailable declarations in types.
4386
+ bool diagnoseTypeReprAvailability ( const TypeRepr *T, const ExportContext &where,
4387
+ DeclAvailabilityFlags flags) {
4352
4388
if (!T)
4353
4389
return false ;
4354
4390
TypeReprAvailabilityWalker walker (where, flags);
@@ -4441,20 +4477,15 @@ class ProblematicTypeFinder : public TypeDeclFinder {
4441
4477
4442
4478
}
4443
4479
4444
- void swift::diagnoseTypeAvailability (Type T, SourceLoc loc,
4445
- const ExportContext &where,
4446
- DeclAvailabilityFlags flags) {
4447
- if (!T)
4448
- return ;
4449
- T.walk (ProblematicTypeFinder (loc, where, flags));
4450
- }
4451
-
4452
4480
void swift::diagnoseTypeAvailability (const TypeRepr *TR, Type T, SourceLoc loc,
4453
4481
const ExportContext &where,
4454
4482
DeclAvailabilityFlags flags) {
4455
4483
if (diagnoseTypeReprAvailability (TR, where, flags))
4456
4484
return ;
4457
- diagnoseTypeAvailability (T, loc, where, flags);
4485
+
4486
+ if (!T)
4487
+ return ;
4488
+ T.walk (ProblematicTypeFinder (loc, where, flags));
4458
4489
}
4459
4490
4460
4491
static void diagnoseMissingConformance (
@@ -4534,14 +4565,14 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
4534
4565
auto maybeUnavail = TypeChecker::checkConformanceAvailability (
4535
4566
rootConf, ext, where);
4536
4567
if (maybeUnavail.has_value ()) {
4537
- TypeChecker:: diagnosePotentialUnavailability (rootConf, ext, loc, DC,
4538
- maybeUnavail.value ());
4568
+ diagnosePotentialUnavailability (rootConf, ext, loc, DC,
4569
+ maybeUnavail.value ());
4539
4570
maybeEmitAssociatedTypeNote ();
4540
4571
return true ;
4541
4572
}
4542
4573
4543
4574
// Diagnose for deprecation
4544
- if (TypeChecker:: diagnoseIfDeprecated (loc, rootConf, ext, where)) {
4575
+ if (diagnoseIfDeprecated (loc, rootConf, ext, where)) {
4545
4576
maybeEmitAssociatedTypeNote ();
4546
4577
4547
4578
// Deprecation is just a warning, so keep going with checking the
@@ -4559,13 +4590,10 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
4559
4590
return false ;
4560
4591
}
4561
4592
4562
- bool
4563
- swift::diagnoseSubstitutionMapAvailability (SourceLoc loc,
4564
- SubstitutionMap subs,
4565
- const ExportContext &where,
4566
- Type depTy, Type replacementTy,
4567
- bool warnIfConformanceUnavailablePreSwift6,
4568
- bool suppressParameterizationCheckForOptional) {
4593
+ bool diagnoseSubstitutionMapAvailability (
4594
+ SourceLoc loc, SubstitutionMap subs, const ExportContext &where, Type depTy,
4595
+ Type replacementTy, bool warnIfConformanceUnavailablePreSwift6,
4596
+ bool suppressParameterizationCheckForOptional) {
4569
4597
bool hadAnyIssues = false ;
4570
4598
for (ProtocolConformanceRef conformance : subs.getConformances ()) {
4571
4599
if (diagnoseConformanceAvailability (loc, conformance, where,
0 commit comments