3838using namespace swift ;
3939
4040ExportContext::ExportContext (DeclContext *DC, FragileFunctionKind kind,
41- bool spi, bool exported, bool deprecated)
41+ bool spi, bool exported, bool implicit,
42+ bool deprecated)
4243 : DC(DC), FragileKind(kind) {
4344 SPI = spi;
4445 Exported = exported;
46+ Implicit = implicit;
4547 Deprecated = deprecated;
4648 Reason = ExportabilityReason::General;
4749}
@@ -180,12 +182,10 @@ ExportContext ExportContext::forDeclSignature(Decl *D) {
180182
181183 bool exported = ::isExported (D);
182184
183- return ExportContext (DC, fragileKind, spi, exported, deprecated);
185+ return ExportContext (DC, fragileKind, spi, exported, implicit, deprecated);
184186}
185187
186188ExportContext ExportContext::forFunctionBody (DeclContext *DC) {
187- assert (DC && " Use ExportContext::forImplicit() for implicit decls" );
188-
189189 bool implicit = false ;
190190 bool deprecated = false ;
191191 forEachOuterDecl (DC,
@@ -204,12 +204,7 @@ ExportContext ExportContext::forFunctionBody(DeclContext *DC) {
204204 assert (fragileKind.kind == FragileFunctionKind::None);
205205 }
206206
207- return ExportContext (DC, fragileKind, spi, exported, deprecated);
208- }
209-
210- ExportContext ExportContext::forImplicit () {
211- return ExportContext (nullptr , FragileFunctionKind (),
212- false , false , false );
207+ return ExportContext (DC, fragileKind, spi, exported, implicit, deprecated);
213208}
214209
215210ExportContext ExportContext::withReason (ExportabilityReason reason) const {
@@ -1700,29 +1695,6 @@ someEnclosingDeclMatches(SourceRange ReferenceRange,
17001695 return Pred (abstractSyntaxDeclForAvailableAttribute (DeclToSearch));
17011696}
17021697
1703- // / Returns true if the reference or any of its parents is an
1704- // / implicit function.
1705- static bool isInsideImplicitFunction (SourceRange ReferenceRange,
1706- const DeclContext *DC) {
1707- auto IsInsideImplicitFunc = [](const Decl *D) {
1708- auto *AFD = dyn_cast<AbstractFunctionDecl>(D);
1709- return AFD && AFD->isImplicit ();
1710- };
1711-
1712- return someEnclosingDeclMatches (ReferenceRange, DC, IsInsideImplicitFunc);
1713- }
1714-
1715- // / Returns true if the reference or any of its parents is an
1716- // / unavailable (or obsoleted) declaration.
1717- static bool isInsideUnavailableDeclaration (SourceRange ReferenceRange,
1718- const DeclContext *ReferenceDC) {
1719- auto IsUnavailable = [](const Decl *D) {
1720- return D->getAttrs ().getUnavailable (D->getASTContext ());
1721- };
1722-
1723- return someEnclosingDeclMatches (ReferenceRange, ReferenceDC, IsUnavailable);
1724- }
1725-
17261698// / Returns true if the reference or any of its parents is an
17271699// / unconditional unavailable declaration for the same platform.
17281700static bool isInsideCompatibleUnavailableDeclaration (
@@ -2135,21 +2107,14 @@ void TypeChecker::diagnoseIfDeprecated(SourceRange ReferenceRange,
21352107 if (!Attr)
21362108 return ;
21372109
2138- auto *ReferenceDC = Where.getDeclContext ();
2139-
2140- // We don't want deprecated declarations to trigger warnings
2141- // in synthesized code.
2142- if (ReferenceRange.isInvalid () &&
2143- isInsideImplicitFunction (ReferenceRange, ReferenceDC)) {
2144- return ;
2145- }
21462110 // We match the behavior of clang to not report deprecation warnings
21472111 // inside declarations that are themselves deprecated on all deployment
21482112 // targets.
21492113 if (Where.isDeprecated ()) {
21502114 return ;
21512115 }
21522116
2117+ auto *ReferenceDC = Where.getDeclContext ();
21532118 auto &Context = ReferenceDC->getASTContext ();
21542119 if (!Context.LangOpts .DisableAvailabilityChecking ) {
21552120 AvailabilityContext RunningOSVersions =
@@ -2351,16 +2316,6 @@ bool swift::diagnoseExplicitUnavailability(
23512316 if (!Attr)
23522317 return false ;
23532318
2354- // Suppress the diagnostic if we are in synthesized code inside
2355- // a synthesized function and the reference is lexically
2356- // contained in a declaration that is itself marked unavailable.
2357- // The right thing to do here is to not synthesize that code in the
2358- // first place. rdar://problem/20491640
2359- if (R.isInvalid () && isInsideImplicitFunction (R, DC) &&
2360- isInsideUnavailableDeclaration (R, DC)) {
2361- return false ;
2362- }
2363-
23642319 // Calling unavailable code from within code with the same
23652320 // unavailability is OK -- the eventual caller can't call the
23662321 // enclosing code in the same situations it wouldn't be able to
@@ -2512,20 +2467,6 @@ class AvailabilityWalker : public ASTWalker {
25122467 SmallVector<const Expr *, 16 > ExprStack;
25132468 ExportContext Where;
25142469
2515- // / Returns true if DC is an \c init(rawValue:) declaration and it is marked
2516- // / implicit.
2517- bool inSynthesizedInitRawValue () {
2518- auto *DC = Where.getDeclContext ();
2519- auto init = dyn_cast_or_null<ConstructorDecl>(
2520- DC->getInnermostDeclarationDeclContext ());
2521-
2522- return init &&
2523- init->isImplicit () &&
2524- init->getParameters ()->size () == 1 &&
2525- init->getParameters ()->get (0 )->getArgumentName () ==
2526- Context.Id_rawValue ;
2527- }
2528-
25292470public:
25302471 AvailabilityWalker (ExportContext Where)
25312472 : Context(Where.getDeclContext()->getASTContext ()), Where(Where) {}
@@ -2548,20 +2489,8 @@ class AvailabilityWalker : public ASTWalker {
25482489 };
25492490
25502491 if (auto DR = dyn_cast<DeclRefExpr>(E)) {
2551- DeclAvailabilityFlags flags = None;
2552- if (inSynthesizedInitRawValue ())
2553- // HACK: If a raw-value enum has cases with `@available(introduced:)`
2554- // attributes, the auto-derived `init(rawValue:)` will contain
2555- // DeclRefExprs which reference those cases. It will also contain
2556- // appropriate `guard #available` statements to keep them from running
2557- // on older versions, but the availability checker can't verify that
2558- // because the synthesized code uses invalid SourceLocs. Don't diagnose
2559- // these errors; instead, take it on faith that
2560- // DerivedConformanceRawRepresentable will do the right thing.
2561- flags |= DeclAvailabilityFlag::AllowPotentiallyUnavailable;
2562-
25632492 diagAvailability (DR->getDeclRef (), DR->getSourceRange (),
2564- getEnclosingApplyExpr (), flags );
2493+ getEnclosingApplyExpr (), None );
25652494 maybeDiagStorageAccess (DR->getDecl (), DR->getSourceRange (), DC);
25662495 }
25672496 if (auto MR = dyn_cast<MemberRefExpr>(E)) {
@@ -2861,9 +2790,6 @@ AvailabilityWalker::diagAvailability(ConcreteDeclRef declRef, SourceRange R,
28612790 if (!isAccessorWithDeprecatedStorage)
28622791 TypeChecker::diagnoseIfDeprecated (R, Where, D, call);
28632792
2864- if (Flags.contains (DeclAvailabilityFlag::AllowPotentiallyUnavailable))
2865- return false ;
2866-
28672793 if (Flags.contains (DeclAvailabilityFlag::AllowPotentiallyUnavailableProtocol)
28682794 && isa<ProtocolDecl>(D))
28692795 return false ;
@@ -3041,7 +2967,10 @@ AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
30412967
30422968// / Diagnose uses of unavailable declarations.
30432969void swift::diagAvailability (const Expr *E, DeclContext *DC) {
3044- AvailabilityWalker walker (ExportContext::forFunctionBody (DC));
2970+ auto where = ExportContext::forFunctionBody (DC);
2971+ if (where.isImplicit ())
2972+ return ;
2973+ AvailabilityWalker walker (where);
30452974 const_cast <Expr*>(E)->walk (walker);
30462975}
30472976
@@ -3051,8 +2980,8 @@ class StmtAvailabilityWalker : public ASTWalker {
30512980 ExportContext Where;
30522981
30532982public:
3054- explicit StmtAvailabilityWalker (DeclContext *DC )
3055- : Where(ExportContext::forFunctionBody(DC) ) {}
2983+ explicit StmtAvailabilityWalker (ExportContext where )
2984+ : Where(where ) {}
30562985
30572986 // / We'll visit the expression from performSyntacticExprDiagnostics().
30582987 std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
@@ -3079,7 +3008,11 @@ void swift::diagAvailability(const Stmt *S, DeclContext *DC) {
30793008 if (isa<BraceStmt>(S))
30803009 return ;
30813010
3082- StmtAvailabilityWalker walker (DC);
3011+ auto where = ExportContext::forFunctionBody (DC);
3012+ if (where.isImplicit ())
3013+ return ;
3014+
3015+ StmtAvailabilityWalker walker (where);
30833016 const_cast <Stmt*>(S)->walk (walker);
30843017}
30853018
@@ -3281,6 +3214,7 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *Decl,
32813214 ExportContext Where,
32823215 DeclAvailabilityFlags Flags)
32833216{
3217+ assert (!Where.isImplicit ());
32843218 AvailabilityWalker AW (Where);
32853219 return AW.diagAvailability (const_cast <ValueDecl *>(Decl), R, nullptr , Flags);
32863220}
0 commit comments