Skip to content

Commit d8be24b

Browse files
committed
Sema: Fix -disable-availability-checking.
Suppression of diagnostics about use of unavailable declarations in equivalently unavailable contexts now relies on querying the `TypeRefinementContext` hierarchy. Generation of the TypeRefinementContext tree was suppressed when `-disable-availability-checking` was specified, though, causing some unavailability diagnostics to be emitted when they ought to be suppressed. Instead of refusing to generate a `TypeRefinementContext` hierarchy, instead just avoid populating nodes for `if #available` checks for OS versions since these checks are meant to have no effect when `-disable-availability-checking` is specified. Resolves rdar://138987918.
1 parent 09af72e commit d8be24b

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,8 @@ ExportContext ExportContext::forDeclSignature(Decl *D) {
238238

239239
auto *DC = D->getInnermostDeclContext();
240240
auto fragileKind = DC->getFragileFunctionKind();
241-
auto availabilityContext =
242-
(Ctx.LangOpts.DisableAvailabilityChecking
243-
? AvailabilityContext::getDefault(Ctx)
244-
: TypeChecker::availabilityAtLocation(D->getLoc(), DC));
241+
auto loc = D->getLoc();
242+
auto availabilityContext = TypeChecker::availabilityAtLocation(loc, DC);
245243
bool spi = Ctx.LangOpts.LibraryLevel == LibraryLevel::SPI;
246244
bool implicit = false;
247245
computeExportContextBits(Ctx, D, &spi, &implicit);
@@ -259,10 +257,7 @@ ExportContext ExportContext::forFunctionBody(DeclContext *DC, SourceLoc loc) {
259257
auto &Ctx = DC->getASTContext();
260258

261259
auto fragileKind = DC->getFragileFunctionKind();
262-
auto availabilityContext =
263-
(Ctx.LangOpts.DisableAvailabilityChecking
264-
? AvailabilityContext::getDefault(Ctx)
265-
: TypeChecker::availabilityAtLocation(loc, DC));
260+
auto availabilityContext = TypeChecker::availabilityAtLocation(loc, DC);
266261
bool spi = Ctx.LangOpts.LibraryLevel == LibraryLevel::SPI;
267262
bool implicit = false;
268263
forEachOuterDecl(
@@ -1075,6 +1070,8 @@ class TypeRefinementContextBuilder : private ASTWalker {
10751070
std::pair<std::optional<AvailabilityRange>,
10761071
std::optional<AvailabilityRange>>
10771072
buildStmtConditionRefinementContext(StmtCondition Cond) {
1073+
if (Context.LangOpts.DisableAvailabilityChecking)
1074+
return {};
10781075

10791076
// Any refinement contexts introduced in the statement condition
10801077
// will end at the end of the last condition element.
@@ -1346,8 +1343,18 @@ class TypeRefinementContextBuilder : private ASTWalker {
13461343
} // end anonymous namespace
13471344

13481345
void TypeChecker::buildTypeRefinementContextHierarchy(SourceFile &SF) {
1346+
switch (SF.Kind) {
1347+
case SourceFileKind::SIL:
1348+
// SIL doesn't support availability queries.
1349+
return;
1350+
case SourceFileKind::MacroExpansion:
1351+
case SourceFileKind::DefaultArgument:
1352+
case SourceFileKind::Library:
1353+
case SourceFileKind::Main:
1354+
case SourceFileKind::Interface:
1355+
break;
1356+
}
13491357
ASTContext &Context = SF.getASTContext();
1350-
assert(!Context.LangOpts.DisableAvailabilityChecking);
13511358

13521359
// If there's already a root node, then we're done.
13531360
if (SF.getTypeRefinementContext())
@@ -1376,9 +1383,6 @@ void TypeChecker::buildTypeRefinementContextHierarchy(SourceFile &SF) {
13761383

13771384
TypeRefinementContext *
13781385
TypeChecker::getOrBuildTypeRefinementContext(SourceFile *SF) {
1379-
if (SF->getASTContext().LangOpts.DisableAvailabilityChecking)
1380-
return nullptr;
1381-
13821386
TypeRefinementContext *TRC = SF->getTypeRefinementContext();
13831387
if (!TRC) {
13841388
buildTypeRefinementContextHierarchy(*SF);

lib/Sema/TypeChecker.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,9 @@ TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
276276
FrontendStatsTracer tracer(Ctx.Stats,
277277
"Type checking and Semantic analysis");
278278

279-
if (!Ctx.LangOpts.DisableAvailabilityChecking) {
280-
// Build the type refinement hierarchy for the primary
281-
// file before type checking.
282-
TypeChecker::buildTypeRefinementContextHierarchy(*SF);
283-
}
279+
// Build the type refinement hierarchy for the primary
280+
// file before type checking.
281+
TypeChecker::buildTypeRefinementContextHierarchy(*SF);
284282

285283
// Type check the top-level elements of the source file.
286284
for (auto D : SF->getTopLevelDecls()) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -emit-module %s -enable-library-evolution -swift-version 5 -module-name Library -emit-module-interface-path %t/Library.swiftinterface -o %t/Library.swiftmodule
2+
3+
// RUN: echo "import Library" > %t/Client.swift
4+
// RUN: %target-swift-frontend -emit-module %t/Client.swift -module-name Client -o %t/Client.swiftmodule -I %t
5+
6+
// RUN: rm %t/Library.swiftmodule
7+
// RUN: %target-sil-opt -enable-sil-verify-all %t/Client.swiftmodule -module-name Client
8+
9+
@available(*, unavailable)
10+
public struct Unavailable {}
11+
12+
@available(*, unavailable)
13+
public func usesUnavailable(_ u: Unavailable) {}

0 commit comments

Comments
 (0)