@@ -2828,6 +2828,23 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *Decl,
2828
2828
return AW.diagAvailability (const_cast <ValueDecl *>(Decl), R, nullptr , Flags);
2829
2829
}
2830
2830
2831
+ // / Should we warn that \p valueDecl needs an explicit availability annotation
2832
+ // / in -require-explicit-availaiblity mode?
2833
+ static bool declNeedsExplicitAvailability (const ValueDecl *valueDecl) {
2834
+ AccessScope scope =
2835
+ valueDecl->getFormalAccessScope (/* useDC*/ nullptr ,
2836
+ /* treatUsableFromInlineAsPublic*/ true );
2837
+ if (!scope.isPublic () ||
2838
+ valueDecl->getAttrs ().hasAttribute <AlwaysEmitIntoClientAttr>())
2839
+ return false ;
2840
+
2841
+ // Warn on decls without an introduction version.
2842
+ auto &ctx = valueDecl->getASTContext ();
2843
+ auto safeRangeUnderApprox = AvailabilityInference::availableRange (valueDecl, ctx);
2844
+ return !safeRangeUnderApprox.getOSVersion ().hasLowerEndpoint () &&
2845
+ !valueDecl->getAttrs ().isUnavailable (ctx);
2846
+ }
2847
+
2831
2848
void swift::checkExplicitAvailability (Decl *decl) {
2832
2849
// Check only if the command line option was set.
2833
2850
if (!decl->getASTContext ().LangOpts .RequireExplicitAvailability )
@@ -2840,27 +2857,14 @@ void swift::checkExplicitAvailability(Decl *decl) {
2840
2857
2841
2858
ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);
2842
2859
if (valueDecl == nullptr ) {
2843
- // decl should be either a ValueDecl or an ExtensionDecl
2860
+ // decl should be either a ValueDecl or an ExtensionDecl.
2844
2861
auto extension = cast<ExtensionDecl>(decl);
2845
2862
valueDecl = extension->getExtendedNominal ();
2846
2863
if (!valueDecl)
2847
2864
return ;
2848
2865
}
2849
2866
2850
- // Skip decls that are not public and not usable from inline.
2851
- AccessScope scope =
2852
- valueDecl->getFormalAccessScope (/* useDC*/ nullptr ,
2853
- /* treatUsableFromInlineAsPublic*/ true );
2854
- if (!scope.isPublic () ||
2855
- decl->getAttrs ().hasAttribute <AlwaysEmitIntoClientAttr>())
2856
- return ;
2857
-
2858
- // Warn on decls without an introduction version.
2859
- auto &ctx = decl->getASTContext ();
2860
- auto safeRangeUnderApprox = AvailabilityInference::availableRange (decl, ctx);
2861
- if (!safeRangeUnderApprox.getOSVersion ().hasLowerEndpoint () &&
2862
- !decl->getAttrs ().isUnavailable (ctx)) {
2863
-
2867
+ if (declNeedsExplicitAvailability (valueDecl)) {
2864
2868
auto diag = decl->diagnose (diag::public_decl_needs_availability);
2865
2869
2866
2870
auto suggestPlatform = decl->getASTContext ().LangOpts .RequireExplicitAvailabilityTarget ;
@@ -2876,6 +2880,7 @@ void swift::checkExplicitAvailability(Decl *decl) {
2876
2880
{
2877
2881
llvm::raw_string_ostream Out (AttrText);
2878
2882
2883
+ auto &ctx = valueDecl->getASTContext ();
2879
2884
StringRef OriginalIndent = Lexer::getIndentationForLine (
2880
2885
ctx.SourceMgr , InsertLoc);
2881
2886
Out << " @available(" << suggestPlatform << " , *)\n "
0 commit comments